widget.spec.ts 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { MessageLoop, Message } from '@lumino/messaging';
  5. import { Widget } from '@lumino/widgets';
  6. import { generate, simulate } from 'simulate-event';
  7. import {
  8. CodeCellModel,
  9. CodeCell,
  10. MarkdownCellModel,
  11. MarkdownCell,
  12. RawCellModel,
  13. RawCell,
  14. Cell
  15. } from '@jupyterlab/cells';
  16. import { INotebookModel, NotebookModel } from '../src';
  17. import { Notebook, StaticNotebook } from '../src';
  18. import { framePromise, signalToPromise } from '@jupyterlab/testutils';
  19. import { JupyterServer } from '@jupyterlab/testutils/lib/start_jupyter_server';
  20. import * as utils from './utils';
  21. const server = new JupyterServer();
  22. beforeAll(async () => {
  23. jest.setTimeout(20000);
  24. await server.start();
  25. });
  26. afterAll(async () => {
  27. await server.shutdown();
  28. });
  29. const contentFactory = utils.createNotebookFactory();
  30. const editorConfig = utils.defaultEditorConfig;
  31. const rendermime = utils.defaultRenderMime();
  32. const options: Notebook.IOptions = {
  33. rendermime,
  34. contentFactory,
  35. mimeTypeService: utils.mimeTypeService,
  36. editorConfig
  37. };
  38. function createWidget(): LogStaticNotebook {
  39. const model = new NotebookModel();
  40. const widget = new LogStaticNotebook(options);
  41. widget.model = model;
  42. return widget;
  43. }
  44. class LogStaticNotebook extends StaticNotebook {
  45. methods: string[] = [];
  46. protected onUpdateRequest(msg: Message): void {
  47. super.onUpdateRequest(msg);
  48. this.methods.push('onUpdateRequest');
  49. }
  50. protected onModelChanged(
  51. oldValue: INotebookModel,
  52. newValue: INotebookModel
  53. ): void {
  54. super.onModelChanged(oldValue, newValue);
  55. this.methods.push('onModelChanged');
  56. }
  57. protected onMetadataChanged(model: any, args: any): void {
  58. super.onMetadataChanged(model, args);
  59. this.methods.push('onMetadataChanged');
  60. }
  61. protected onCellInserted(index: number, cell: Cell): void {
  62. super.onCellInserted(index, cell);
  63. this.methods.push('onCellInserted');
  64. }
  65. protected onCellMoved(fromIndex: number, toIndex: number): void {
  66. super.onCellMoved(fromIndex, toIndex);
  67. this.methods.push('onCellMoved');
  68. }
  69. protected onCellRemoved(index: number, cell: Cell): void {
  70. super.onCellRemoved(index, cell);
  71. this.methods.push('onCellRemoved');
  72. }
  73. }
  74. class LogNotebook extends Notebook {
  75. events: string[] = [];
  76. methods: string[] = [];
  77. handleEvent(event: Event): void {
  78. this.events.push(event.type);
  79. super.handleEvent(event);
  80. }
  81. protected onAfterAttach(msg: Message): void {
  82. super.onAfterAttach(msg);
  83. this.methods.push('onAfterAttach');
  84. }
  85. protected onBeforeDetach(msg: Message): void {
  86. super.onBeforeDetach(msg);
  87. this.methods.push('onBeforeDetach');
  88. }
  89. protected onActivateRequest(msg: Message): void {
  90. super.onActivateRequest(msg);
  91. this.methods.push('onActivateRequest');
  92. }
  93. protected onUpdateRequest(msg: Message): void {
  94. super.onUpdateRequest(msg);
  95. this.methods.push('onUpdateRequest');
  96. }
  97. protected onCellInserted(index: number, cell: Cell): void {
  98. super.onCellInserted(index, cell);
  99. this.methods.push('onCellInserted');
  100. }
  101. protected onCellMoved(fromIndex: number, toIndex: number): void {
  102. super.onCellMoved(fromIndex, toIndex);
  103. this.methods.push('onCellMoved');
  104. }
  105. protected onCellRemoved(index: number, cell: Cell): void {
  106. super.onCellRemoved(index, cell);
  107. this.methods.push('onCellRemoved');
  108. }
  109. }
  110. function createActiveWidget(): LogNotebook {
  111. const model = new NotebookModel();
  112. const widget = new LogNotebook(options);
  113. widget.model = model;
  114. return widget;
  115. }
  116. function selected(nb: Notebook): number[] {
  117. const selected = [];
  118. const cells = nb.widgets;
  119. for (let i = 0; i < cells.length; i++) {
  120. if (nb.isSelected(cells[i])) {
  121. selected.push(i);
  122. }
  123. }
  124. return selected;
  125. }
  126. describe('@jupyter/notebook', () => {
  127. describe('StaticNotebook', () => {
  128. describe('#constructor()', () => {
  129. it('should create a notebook widget', () => {
  130. const widget = new StaticNotebook(options);
  131. expect(widget).toBeInstanceOf(StaticNotebook);
  132. });
  133. it('should add the `jp-Notebook` class', () => {
  134. const widget = new StaticNotebook(options);
  135. expect(widget.hasClass('jp-Notebook')).toBe(true);
  136. });
  137. it('should accept an optional render', () => {
  138. const widget = new StaticNotebook(options);
  139. expect(widget.contentFactory).toBe(contentFactory);
  140. });
  141. it('should accept an optional editor config', () => {
  142. const widget = new StaticNotebook(options);
  143. expect(widget.editorConfig).toBe(editorConfig);
  144. });
  145. });
  146. describe('#modelChanged', () => {
  147. it('should be emitted when the model changes', () => {
  148. const widget = new StaticNotebook(options);
  149. const model = new NotebookModel();
  150. let called = false;
  151. widget.modelChanged.connect((sender, args) => {
  152. expect(sender).toBe(widget);
  153. expect(args).toBeUndefined();
  154. called = true;
  155. });
  156. widget.model = model;
  157. expect(called).toBe(true);
  158. });
  159. });
  160. describe('#modelContentChanged', () => {
  161. it('should be emitted when a cell is added', () => {
  162. const widget = new StaticNotebook(options);
  163. widget.model = new NotebookModel();
  164. let called = false;
  165. widget.modelContentChanged.connect(() => {
  166. called = true;
  167. });
  168. const cell = widget.model!.contentFactory.createCodeCell({});
  169. widget.model!.cells.push(cell);
  170. expect(called).toBe(true);
  171. });
  172. it('should be emitted when metadata is set', () => {
  173. const widget = new StaticNotebook(options);
  174. widget.model = new NotebookModel();
  175. let called = false;
  176. widget.modelContentChanged.connect(() => {
  177. called = true;
  178. });
  179. widget.model!.metadata.set('foo', 1);
  180. expect(called).toBe(true);
  181. });
  182. });
  183. describe('#model', () => {
  184. it('should get the model for the widget', () => {
  185. const widget = new StaticNotebook(options);
  186. expect(widget.model).toBeNull();
  187. });
  188. it('should set the model for the widget', () => {
  189. const widget = new StaticNotebook(options);
  190. const model = new NotebookModel();
  191. widget.model = model;
  192. expect(widget.model).toBe(model);
  193. });
  194. it('should emit the `modelChanged` signal', () => {
  195. const widget = new StaticNotebook(options);
  196. const model = new NotebookModel();
  197. widget.model = model;
  198. let called = false;
  199. widget.modelChanged.connect(() => {
  200. called = true;
  201. });
  202. widget.model = new NotebookModel();
  203. expect(called).toBe(true);
  204. });
  205. it('should be a no-op if the value does not change', () => {
  206. const widget = new StaticNotebook(options);
  207. const model = new NotebookModel();
  208. widget.model = model;
  209. let called = false;
  210. widget.modelChanged.connect(() => {
  211. called = true;
  212. });
  213. widget.model = model;
  214. expect(called).toBe(false);
  215. });
  216. it('should add the model cells to the layout', () => {
  217. const widget = new LogStaticNotebook(options);
  218. const model = new NotebookModel();
  219. model.fromJSON(utils.DEFAULT_CONTENT);
  220. widget.model = model;
  221. expect(widget.widgets.length).toBe(6);
  222. });
  223. it('should add a default cell if the notebook model is empty', () => {
  224. const widget = new LogStaticNotebook(options);
  225. const model1 = new NotebookModel();
  226. expect(model1.cells.length).toBe(0);
  227. widget.model = model1;
  228. expect(model1.cells.length).toBe(1);
  229. expect(model1.cells.get(0).type).toBe('code');
  230. widget.notebookConfig = {
  231. ...widget.notebookConfig,
  232. defaultCell: 'markdown'
  233. };
  234. const model2 = new NotebookModel();
  235. expect(model2.cells.length).toBe(0);
  236. widget.model = model2;
  237. expect(model2.cells.length).toBe(1);
  238. expect(model2.cells.get(0).type).toBe('markdown');
  239. });
  240. it('should set the mime types of the cell widgets', () => {
  241. const widget = new LogStaticNotebook(options);
  242. const model = new NotebookModel();
  243. const value = { name: 'python', codemirror_mode: 'python' };
  244. model.metadata.set('language_info', value);
  245. widget.model = model;
  246. const child = widget.widgets[0];
  247. expect(child.model.mimeType).toBe('text/x-python');
  248. });
  249. describe('`cells.changed` signal', () => {
  250. let widget: LogStaticNotebook;
  251. beforeEach(() => {
  252. widget = createWidget();
  253. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  254. });
  255. afterEach(() => {
  256. widget.dispose();
  257. });
  258. it('should handle changes to the model cell list', async () => {
  259. widget = createWidget();
  260. widget.model!.cells.clear();
  261. await framePromise();
  262. expect(widget.widgets.length).toBe(1);
  263. });
  264. it('should handle a remove', () => {
  265. const cell = widget.model!.cells.get(1);
  266. const child = widget.widgets[1];
  267. widget.model!.cells.removeValue(cell);
  268. expect(cell.isDisposed).toBe(false);
  269. expect(child.isDisposed).toBe(true);
  270. });
  271. it('should handle an add', () => {
  272. const cell = widget.model!.contentFactory.createCodeCell({});
  273. widget.model!.cells.push(cell);
  274. expect(widget.widgets.length).toBe(7);
  275. const child = widget.widgets[0];
  276. expect(child.hasClass('jp-Notebook-cell')).toBe(true);
  277. });
  278. it('should initially render markdown cells with content', () => {
  279. const cell1 = widget.model!.contentFactory.createMarkdownCell({});
  280. const cell2 = widget.model!.contentFactory.createMarkdownCell({});
  281. cell1.value.text = '# Hello';
  282. widget.model!.cells.push(cell1);
  283. widget.model!.cells.push(cell2);
  284. expect(widget.widgets.length).toBe(8);
  285. const child1 = widget.widgets[6] as MarkdownCell;
  286. const child2 = widget.widgets[7] as MarkdownCell;
  287. expect(child1.rendered).toBe(true);
  288. expect(child2.rendered).toBe(false);
  289. });
  290. it('should handle a move', () => {
  291. const child = widget.widgets[1];
  292. widget.model!.cells.move(1, 2);
  293. expect(widget.widgets[2]).toBe(child);
  294. });
  295. it('should handle a clear', () => {
  296. const cell = widget.model!.contentFactory.createCodeCell({});
  297. widget.model!.cells.push(cell);
  298. widget.model!.cells.clear();
  299. expect(widget.widgets.length).toBe(0);
  300. });
  301. it('should add a new default cell when cells are cleared', async () => {
  302. const model = widget.model!;
  303. widget.notebookConfig = {
  304. ...widget.notebookConfig,
  305. defaultCell: 'raw'
  306. };
  307. const promise = signalToPromise(model.cells.changed);
  308. model.cells.clear();
  309. await promise;
  310. expect(model.cells.length).toBe(0);
  311. await signalToPromise(model.cells.changed);
  312. expect(model.cells.length).toBe(1);
  313. expect(model.cells.get(0)).toBeInstanceOf(RawCellModel);
  314. });
  315. });
  316. });
  317. describe('#rendermime', () => {
  318. it('should be the rendermime instance used by the widget', () => {
  319. const widget = new StaticNotebook(options);
  320. expect(widget.rendermime).toBe(rendermime);
  321. });
  322. });
  323. describe('#contentFactory', () => {
  324. it('should be the cell widget contentFactory used by the widget', () => {
  325. const widget = new StaticNotebook(options);
  326. expect(widget.contentFactory).toBeInstanceOf(
  327. StaticNotebook.ContentFactory
  328. );
  329. });
  330. });
  331. describe('#editorConfig', () => {
  332. it('should be the cell widget contentFactory used by the widget', () => {
  333. const widget = new StaticNotebook(options);
  334. expect(widget.editorConfig).toBe(options.editorConfig);
  335. });
  336. it('should be settable', () => {
  337. const widget = createWidget();
  338. expect(widget.widgets[0].editor.getOption('autoClosingBrackets')).toBe(
  339. true
  340. );
  341. const newConfig = {
  342. raw: editorConfig.raw,
  343. markdown: editorConfig.markdown,
  344. code: {
  345. ...editorConfig.code,
  346. autoClosingBrackets: false
  347. }
  348. };
  349. widget.editorConfig = newConfig;
  350. expect(widget.widgets[0].editor.getOption('autoClosingBrackets')).toBe(
  351. false
  352. );
  353. });
  354. });
  355. describe('#codeMimetype', () => {
  356. it('should get the mime type for code cells', () => {
  357. const widget = new StaticNotebook(options);
  358. expect(widget.codeMimetype).toBe('text/plain');
  359. });
  360. it('should be set from language metadata', () => {
  361. const widget = new LogStaticNotebook(options);
  362. const model = new NotebookModel();
  363. const value = { name: 'python', codemirror_mode: 'python' };
  364. model.metadata.set('language_info', value);
  365. widget.model = model;
  366. expect(widget.codeMimetype).toBe('text/x-python');
  367. });
  368. });
  369. describe('#widgets', () => {
  370. it('should get the child widget at a specified index', () => {
  371. const widget = createWidget();
  372. const child = widget.widgets[0];
  373. expect(child).toBeInstanceOf(CodeCell);
  374. });
  375. it('should return `undefined` if out of range', () => {
  376. const widget = createWidget();
  377. const child = widget.widgets[1];
  378. expect(child).toBeUndefined();
  379. });
  380. it('should get the number of child widgets', () => {
  381. const widget = createWidget();
  382. expect(widget.widgets.length).toBe(1);
  383. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  384. expect(widget.widgets.length).toBe(6);
  385. });
  386. });
  387. describe('#dispose()', () => {
  388. it('should dispose of the resources held by the widget', () => {
  389. const widget = createWidget();
  390. widget.dispose();
  391. expect(widget.isDisposed).toBe(true);
  392. });
  393. it('should be safe to call multiple times', () => {
  394. const widget = createWidget();
  395. widget.dispose();
  396. widget.dispose();
  397. expect(widget.isDisposed).toBe(true);
  398. });
  399. });
  400. describe('#onModelChanged()', () => {
  401. it('should be called when the model changes', () => {
  402. const widget = new LogStaticNotebook(options);
  403. widget.model = new NotebookModel();
  404. expect(widget.methods).toEqual(
  405. expect.arrayContaining(['onModelChanged'])
  406. );
  407. });
  408. it('should not be called if the model does not change', () => {
  409. const widget = createWidget();
  410. widget.methods = [];
  411. widget.model = widget.model; // eslint-disable-line
  412. expect(widget.methods).toEqual(
  413. expect.not.arrayContaining(['onModelChanged'])
  414. );
  415. });
  416. });
  417. describe('#onMetadataChanged()', () => {
  418. it('should be called when the metadata on the notebook changes', () => {
  419. const widget = createWidget();
  420. widget.model!.metadata.set('foo', 1);
  421. expect(widget.methods).toEqual(
  422. expect.arrayContaining(['onMetadataChanged'])
  423. );
  424. });
  425. it('should update the `codeMimetype`', () => {
  426. const widget = createWidget();
  427. const value = { name: 'python', codemirror_mode: 'python' };
  428. widget.model!.metadata.set('language_info', value);
  429. expect(widget.methods).toEqual(
  430. expect.arrayContaining(['onMetadataChanged'])
  431. );
  432. expect(widget.codeMimetype).toBe('text/x-python');
  433. });
  434. it('should update the cell widget mimetype', () => {
  435. const widget = createWidget();
  436. const value = { name: 'python', mimetype: 'text/x-python' };
  437. widget.model!.metadata.set('language_info', value);
  438. expect(widget.methods).toEqual(
  439. expect.arrayContaining(['onMetadataChanged'])
  440. );
  441. const child = widget.widgets[0];
  442. expect(child.model.mimeType).toBe('text/x-python');
  443. });
  444. });
  445. describe('#onCellInserted()', () => {
  446. it('should be called when a cell is inserted', () => {
  447. const widget = createWidget();
  448. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  449. expect(widget.methods).toEqual(
  450. expect.arrayContaining(['onCellInserted'])
  451. );
  452. });
  453. });
  454. describe('#onCellMoved()', () => {
  455. it('should be called when a cell is moved', () => {
  456. const widget = createWidget();
  457. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  458. widget.model!.cells.move(0, 1);
  459. expect(widget.methods).toEqual(expect.arrayContaining(['onCellMoved']));
  460. });
  461. });
  462. describe('#onCellRemoved()', () => {
  463. it('should be called when a cell is removed', () => {
  464. const widget = createWidget();
  465. const cell = widget.model!.cells.get(0);
  466. widget.model!.cells.removeValue(cell);
  467. expect(widget.methods).toEqual(
  468. expect.arrayContaining(['onCellRemoved'])
  469. );
  470. });
  471. });
  472. describe('.ContentFactory', () => {
  473. describe('#constructor', () => {
  474. it('should create a new ContentFactory', () => {
  475. const editorFactory = utils.editorFactory;
  476. const factory = new StaticNotebook.ContentFactory({ editorFactory });
  477. expect(factory).toBeInstanceOf(StaticNotebook.ContentFactory);
  478. });
  479. });
  480. describe('#createCodeCell({})', () => {
  481. it('should create a `CodeCell`', () => {
  482. const contentFactory = new StaticNotebook.ContentFactory();
  483. const model = new CodeCellModel({});
  484. const codeOptions = { model, rendermime, contentFactory };
  485. const parent = new StaticNotebook(options);
  486. const widget = contentFactory.createCodeCell(codeOptions, parent);
  487. expect(widget).toBeInstanceOf(CodeCell);
  488. });
  489. });
  490. describe('#createMarkdownCell({})', () => {
  491. it('should create a `MarkdownCell`', () => {
  492. const contentFactory = new StaticNotebook.ContentFactory();
  493. const model = new MarkdownCellModel({});
  494. const mdOptions = { model, rendermime, contentFactory };
  495. const parent = new StaticNotebook(options);
  496. const widget = contentFactory.createMarkdownCell(mdOptions, parent);
  497. expect(widget).toBeInstanceOf(MarkdownCell);
  498. });
  499. });
  500. describe('#createRawCell()', () => {
  501. it('should create a `RawCell`', () => {
  502. const contentFactory = new StaticNotebook.ContentFactory();
  503. const model = new RawCellModel({});
  504. const rawOptions = { model, contentFactory };
  505. const parent = new StaticNotebook(options);
  506. const widget = contentFactory.createRawCell(rawOptions, parent);
  507. expect(widget).toBeInstanceOf(RawCell);
  508. });
  509. });
  510. });
  511. });
  512. describe('Notebook', () => {
  513. describe('#stateChanged', () => {
  514. it('should be emitted when the state of the notebook changes', () => {
  515. const widget = createActiveWidget();
  516. let called = false;
  517. widget.stateChanged.connect((sender, args) => {
  518. expect(sender).toBe(widget);
  519. expect(args.name).toBe('mode');
  520. expect(args.oldValue).toBe('command');
  521. expect(args.newValue).toBe('edit');
  522. called = true;
  523. });
  524. widget.mode = 'edit';
  525. expect(called).toBe(true);
  526. });
  527. });
  528. describe('#activeCellChanged', () => {
  529. it('should be emitted when the active cell changes', () => {
  530. const widget = createActiveWidget();
  531. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  532. let called = false;
  533. widget.activeCellChanged.connect((sender, args) => {
  534. expect(sender).toBe(widget);
  535. expect(args).toBe(widget.activeCell);
  536. called = true;
  537. });
  538. widget.activeCellIndex++;
  539. expect(called).toBe(true);
  540. });
  541. it('should not be emitted when the active cell does not change', () => {
  542. const widget = createActiveWidget();
  543. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  544. let called = false;
  545. widget.activeCellChanged.connect(() => {
  546. called = true;
  547. });
  548. widget.activeCellIndex = widget.activeCellIndex; // eslint-disable-line
  549. expect(called).toBe(false);
  550. });
  551. });
  552. describe('#selectionChanged', () => {
  553. it('should be emitted when the selection changes', () => {
  554. const widget = createActiveWidget();
  555. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  556. let called = false;
  557. widget.selectionChanged.connect((sender, args) => {
  558. expect(sender).toBe(widget);
  559. expect(args).toBeUndefined();
  560. called = true;
  561. });
  562. widget.select(widget.widgets[1]);
  563. expect(called).toBe(true);
  564. });
  565. it('should not be emitted when the selection does not change', () => {
  566. const widget = createActiveWidget();
  567. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  568. let called = false;
  569. widget.select(widget.widgets[1]);
  570. widget.selectionChanged.connect(() => {
  571. called = true;
  572. });
  573. widget.select(widget.widgets[1]);
  574. expect(called).toBe(false);
  575. });
  576. });
  577. describe('#mode', () => {
  578. it('should get the interactivity mode of the notebook', () => {
  579. const widget = createActiveWidget();
  580. expect(widget.mode).toBe('command');
  581. });
  582. it('should set the interactivity mode of the notebook', () => {
  583. const widget = createActiveWidget();
  584. widget.mode = 'edit';
  585. expect(widget.mode).toBe('edit');
  586. });
  587. it('should emit the `stateChanged` signal', () => {
  588. const widget = createActiveWidget();
  589. let called = false;
  590. widget.stateChanged.connect((sender, args) => {
  591. expect(sender).toBe(widget);
  592. expect(args.name).toBe('mode');
  593. expect(args.oldValue).toBe('command');
  594. expect(args.newValue).toBe('edit');
  595. called = true;
  596. });
  597. widget.mode = 'edit';
  598. expect(called).toBe(true);
  599. });
  600. it('should be a no-op if the value does not change', () => {
  601. const widget = createActiveWidget();
  602. let called = false;
  603. widget.stateChanged.connect(() => {
  604. called = true;
  605. });
  606. widget.mode = 'command';
  607. expect(called).toBe(false);
  608. });
  609. it('should post an update request', async () => {
  610. const widget = createActiveWidget();
  611. widget.mode = 'edit';
  612. await framePromise();
  613. expect(widget.methods).toEqual(
  614. expect.arrayContaining(['onUpdateRequest'])
  615. );
  616. });
  617. it('should deselect all cells if switching to edit mode', async () => {
  618. const widget = createActiveWidget();
  619. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  620. Widget.attach(widget, document.body);
  621. await framePromise();
  622. widget.extendContiguousSelectionTo(widget.widgets.length - 1);
  623. const selectedRange = Array.from(Array(widget.widgets.length).keys());
  624. expect(selected(widget)).toEqual(selectedRange);
  625. widget.mode = 'edit';
  626. expect(selected(widget)).toEqual([]);
  627. widget.dispose();
  628. });
  629. it('should unrender a markdown cell when switching to edit mode', () => {
  630. const widget = createActiveWidget();
  631. Widget.attach(widget, document.body);
  632. MessageLoop.sendMessage(widget, Widget.Msg.ActivateRequest);
  633. const cell = widget.model!.contentFactory.createMarkdownCell({});
  634. cell.value.text = '# Hello'; // Should be rendered with content.
  635. widget.model!.cells.push(cell);
  636. const child = widget.widgets[widget.widgets.length - 1] as MarkdownCell;
  637. expect(child.rendered).toBe(true);
  638. widget.activeCellIndex = widget.widgets.length - 1;
  639. widget.mode = 'edit';
  640. expect(child.rendered).toBe(false);
  641. });
  642. });
  643. describe('#activeCellIndex', () => {
  644. it('should get the active cell index of the notebook', () => {
  645. const widget = createActiveWidget();
  646. expect(widget.activeCellIndex).toBe(0);
  647. });
  648. it('should set the active cell index of the notebook', () => {
  649. const widget = createActiveWidget();
  650. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  651. widget.activeCellIndex = 1;
  652. expect(widget.activeCellIndex).toBe(1);
  653. });
  654. it('should clamp the index to the bounds of the notebook cells', () => {
  655. const widget = createActiveWidget();
  656. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  657. widget.activeCellIndex = -2;
  658. expect(widget.activeCellIndex).toBe(0);
  659. widget.activeCellIndex = 100;
  660. expect(widget.activeCellIndex).toBe(5);
  661. });
  662. it('should emit the `stateChanged` signal', () => {
  663. const widget = createActiveWidget();
  664. let called = false;
  665. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  666. widget.stateChanged.connect((sender, args) => {
  667. expect(sender).toBe(widget);
  668. expect(args.name).toBe('activeCellIndex');
  669. expect(args.oldValue).toBe(0);
  670. expect(args.newValue).toBe(1);
  671. called = true;
  672. });
  673. widget.activeCellIndex = 1;
  674. expect(called).toBe(true);
  675. });
  676. it('should be a no-op if the value does not change', () => {
  677. const widget = createActiveWidget();
  678. let called = false;
  679. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  680. widget.stateChanged.connect(() => {
  681. called = true;
  682. });
  683. widget.activeCellIndex = 0;
  684. expect(called).toBe(false);
  685. });
  686. it('should post an update request', async () => {
  687. const widget = createActiveWidget();
  688. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  689. await framePromise();
  690. expect(widget.methods).toEqual(
  691. expect.arrayContaining(['onUpdateRequest'])
  692. );
  693. widget.activeCellIndex = 1;
  694. });
  695. it('should update the active cell if necessary', () => {
  696. const widget = createActiveWidget();
  697. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  698. widget.activeCellIndex = 1;
  699. expect(widget.activeCell).toBe(widget.widgets[1]);
  700. });
  701. });
  702. describe('#activeCell', () => {
  703. it('should get the active cell widget', () => {
  704. const widget = createActiveWidget();
  705. expect(widget.activeCell).toBe(widget.widgets[0]);
  706. });
  707. });
  708. describe('#select()', () => {
  709. it('should select a cell widget', () => {
  710. const widget = createActiveWidget();
  711. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  712. const cell = widget.widgets[0];
  713. widget.select(cell);
  714. expect(widget.isSelected(cell)).toBe(true);
  715. });
  716. it('should allow multiple widgets to be selected', () => {
  717. const widget = createActiveWidget();
  718. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  719. widget.widgets.forEach(cell => {
  720. widget.select(cell);
  721. });
  722. const expectSelected = Array.from(Array(widget.widgets.length).keys());
  723. expect(selected(widget)).toEqual(expectSelected);
  724. });
  725. });
  726. describe('#deselect()', () => {
  727. it('should deselect a cell', () => {
  728. const widget = createActiveWidget();
  729. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  730. for (let i = 0; i < widget.widgets.length; i++) {
  731. const cell = widget.widgets[i];
  732. widget.select(cell);
  733. expect(widget.isSelected(cell)).toBe(true);
  734. widget.deselect(cell);
  735. expect(widget.isSelected(cell)).toBe(false);
  736. }
  737. });
  738. it('should const the active cell be deselected', () => {
  739. const widget = createActiveWidget();
  740. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  741. const cell = widget.activeCell!;
  742. widget.select(cell);
  743. expect(widget.isSelected(cell)).toBe(true);
  744. widget.deselect(cell);
  745. expect(widget.isSelected(cell)).toBe(false);
  746. });
  747. });
  748. describe('#isSelected()', () => {
  749. it('should get whether the cell is selected', () => {
  750. const widget = createActiveWidget();
  751. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  752. widget.select(widget.widgets[0]);
  753. widget.select(widget.widgets[2]);
  754. expect(selected(widget)).toEqual([0, 2]);
  755. });
  756. it('reports selection whether or not cell is active', () => {
  757. const widget = createActiveWidget();
  758. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  759. expect(selected(widget)).toEqual([]);
  760. widget.select(widget.activeCell!);
  761. expect(selected(widget)).toEqual([widget.activeCellIndex]);
  762. });
  763. });
  764. describe('#deselectAll()', () => {
  765. it('should deselect all cells', () => {
  766. const widget = createActiveWidget();
  767. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  768. widget.select(widget.widgets[0]);
  769. widget.select(widget.widgets[2]);
  770. widget.select(widget.widgets[3]);
  771. widget.select(widget.widgets[4]);
  772. expect(selected(widget)).toEqual([0, 2, 3, 4]);
  773. widget.deselectAll();
  774. expect(selected(widget)).toEqual([]);
  775. });
  776. });
  777. describe('#extendContiguousSelectionTo()', () => {
  778. // Test a permutation for extending a selection.
  779. const checkSelection = (
  780. widget: Notebook,
  781. anchor: number,
  782. head: number,
  783. index: number,
  784. select = true
  785. ) => {
  786. if (!select && anchor !== head) {
  787. throw new Error('anchor must equal head if select is false');
  788. }
  789. // Set up the test by pre-selecting appropriate cells if select is true.
  790. if (select) {
  791. for (
  792. let i = Math.min(anchor, head);
  793. i <= Math.max(anchor, head);
  794. i++
  795. ) {
  796. widget.select(widget.widgets[i]);
  797. }
  798. }
  799. // Set the active cell to indicate the head of the selection.
  800. widget.activeCellIndex = head;
  801. // Set up a selection event listener.
  802. let selectionChanged = 0;
  803. const countSelectionChanged = (sender: Notebook, args: void) => {
  804. selectionChanged += 1;
  805. };
  806. widget.selectionChanged.connect(countSelectionChanged);
  807. // Check the contiguous selection.
  808. let selection = widget.getContiguousSelection();
  809. if (select) {
  810. expect(selection.anchor).toBe(anchor);
  811. expect(selection.head).toBe(head);
  812. } else {
  813. expect(selection.anchor).toBeNull();
  814. expect(selection.head).toBeNull();
  815. }
  816. // Extend the selection.
  817. widget.extendContiguousSelectionTo(index);
  818. // Clip index to fall within the cell index range.
  819. index = Math.max(0, Math.min(widget.widgets.length - 1, index));
  820. // Check the active cell is now at the index.
  821. expect(widget.activeCellIndex).toBe(index);
  822. // Check the contiguous selection.
  823. selection = widget.getContiguousSelection();
  824. // Check the selection changed signal was emitted once if necessary.
  825. if (head === index) {
  826. if (index === anchor && select) {
  827. // we should have collapsed the single cell selection
  828. expect(selectionChanged).toBe(1);
  829. } else {
  830. expect(selectionChanged).toBe(0);
  831. }
  832. } else {
  833. expect(selectionChanged).toBe(1);
  834. }
  835. if (anchor !== index) {
  836. expect(selection.anchor).toBe(anchor);
  837. expect(selection.head).toBe(index);
  838. } else {
  839. // If the anchor and index are the same, the selection is collapsed.
  840. expect(selection.anchor).toBe(null);
  841. expect(selection.head).toBe(null);
  842. }
  843. // Clean up widget
  844. widget.selectionChanged.disconnect(countSelectionChanged);
  845. widget.activeCellIndex = 0;
  846. widget.deselectAll();
  847. };
  848. // Lists are of the form [anchor, head, index].
  849. const permutations = [
  850. // Anchor, head, and index are distinct
  851. [1, 3, 5],
  852. [1, 5, 3],
  853. [3, 1, 5],
  854. [3, 5, 1],
  855. [5, 1, 3],
  856. [5, 3, 1],
  857. // Two of anchor, head, and index are equal
  858. [1, 3, 3],
  859. [3, 1, 3],
  860. [3, 3, 1],
  861. // Anchor, head, and index all equal
  862. [3, 3, 3]
  863. ];
  864. it('should work in each permutation of anchor, head, and index', () => {
  865. const widget = createActiveWidget();
  866. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  867. permutations.forEach(p => {
  868. checkSelection(widget, p[0], p[1], p[2]);
  869. });
  870. });
  871. it('should work when we only have an active cell, with no existing selection', () => {
  872. const widget = createActiveWidget();
  873. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  874. permutations.forEach(p => {
  875. if (p[0] === p[1]) {
  876. checkSelection(widget, p[0], p[1], p[2], false);
  877. }
  878. });
  879. });
  880. it('should clip when the index is greater than the last index', () => {
  881. const widget = createActiveWidget();
  882. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  883. permutations.forEach(p => {
  884. checkSelection(widget, p[0], p[1], Number.MAX_SAFE_INTEGER);
  885. });
  886. });
  887. it('should clip when the index is greater than the last index with no existing selection', () => {
  888. const widget = createActiveWidget();
  889. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  890. permutations.forEach(p => {
  891. if (p[0] === p[1]) {
  892. checkSelection(widget, p[0], p[1], Number.MAX_SAFE_INTEGER, false);
  893. }
  894. });
  895. });
  896. it('should clip when the index is less than 0', () => {
  897. const widget = createActiveWidget();
  898. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  899. permutations.forEach(p => {
  900. checkSelection(widget, p[0], p[1], -10);
  901. });
  902. });
  903. it('should clip when the index is less than 0 with no existing selection', () => {
  904. const widget = createActiveWidget();
  905. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  906. permutations.forEach(p => {
  907. if (p[0] === p[1]) {
  908. checkSelection(widget, p[0], p[1], -10, false);
  909. }
  910. });
  911. });
  912. it('handles the case of no cells', () => {
  913. const widget = createActiveWidget();
  914. widget.model!.cells.clear();
  915. expect(widget.widgets.length).toBe(0);
  916. // Set up a selection event listener.
  917. let selectionChanged = 0;
  918. widget.selectionChanged.connect((sender, args) => {
  919. selectionChanged += 1;
  920. });
  921. widget.extendContiguousSelectionTo(3);
  922. expect(widget.activeCellIndex).toBe(-1);
  923. expect(selectionChanged).toBe(0);
  924. });
  925. });
  926. describe('#getContiguousSelection()', () => {
  927. it('throws an error when the selection is not contiguous', () => {
  928. const widget = createActiveWidget();
  929. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  930. widget.select(widget.widgets[1]);
  931. widget.select(widget.widgets[3]);
  932. widget.activeCellIndex = 3;
  933. expect(() => widget.getContiguousSelection()).toThrowError(
  934. /Selection not contiguous/
  935. );
  936. });
  937. it('throws an error if the active cell is not at an endpoint', () => {
  938. const widget = createActiveWidget();
  939. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  940. widget.select(widget.widgets[1]);
  941. widget.select(widget.widgets[2]);
  942. widget.select(widget.widgets[3]);
  943. // Check if active cell is outside selection.
  944. widget.activeCellIndex = 0;
  945. expect(() => widget.getContiguousSelection()).toThrowError(
  946. /Active cell not at endpoint of selection/
  947. );
  948. // Check if active cell is inside selection.
  949. widget.activeCellIndex = 2;
  950. expect(() => widget.getContiguousSelection()).toThrowError(
  951. /Active cell not at endpoint of selection/
  952. );
  953. });
  954. it('returns null values if there is no selection', () => {
  955. const widget = createActiveWidget();
  956. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  957. const selection = widget.getContiguousSelection();
  958. expect(selection).toEqual({ head: null, anchor: null });
  959. });
  960. it('handles the case of no cells', () => {
  961. const widget = createActiveWidget();
  962. widget.model!.cells.clear();
  963. expect(widget.widgets.length).toBe(0);
  964. const selection = widget.getContiguousSelection();
  965. expect(selection).toEqual({ head: null, anchor: null });
  966. });
  967. it('works if head is before the anchor', () => {
  968. const widget = createActiveWidget();
  969. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  970. widget.select(widget.widgets[1]);
  971. widget.select(widget.widgets[2]);
  972. widget.select(widget.widgets[3]);
  973. widget.activeCellIndex = 1;
  974. const selection = widget.getContiguousSelection();
  975. expect(selection).toEqual({ head: 1, anchor: 3 });
  976. });
  977. it('works if head is after the anchor', () => {
  978. const widget = createActiveWidget();
  979. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  980. widget.select(widget.widgets[1]);
  981. widget.select(widget.widgets[2]);
  982. widget.select(widget.widgets[3]);
  983. widget.activeCellIndex = 3;
  984. const selection = widget.getContiguousSelection();
  985. expect(selection).toEqual({ head: 3, anchor: 1 });
  986. });
  987. it('works if head and anchor are the same', () => {
  988. const widget = createActiveWidget();
  989. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  990. widget.select(widget.widgets[3]);
  991. widget.activeCellIndex = 3;
  992. const selection = widget.getContiguousSelection();
  993. expect(selection).toEqual({ head: 3, anchor: 3 });
  994. });
  995. });
  996. describe('#handleEvent()', () => {
  997. let widget: LogNotebook;
  998. beforeEach(async () => {
  999. widget = createActiveWidget();
  1000. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1001. Widget.attach(widget, document.body);
  1002. await framePromise();
  1003. });
  1004. afterEach(() => {
  1005. widget.dispose();
  1006. });
  1007. describe('mousedown', () => {
  1008. it('should set the active cell index', () => {
  1009. const child = widget.widgets[1];
  1010. simulate(child.node, 'mousedown');
  1011. expect(widget.events).toEqual(expect.arrayContaining(['mousedown']));
  1012. expect(widget.isSelected(widget.widgets[0])).toBe(false);
  1013. expect(widget.activeCellIndex).toBe(1);
  1014. });
  1015. it('should be a no-op if not not a cell', () => {
  1016. simulate(widget.node, 'mousedown');
  1017. expect(widget.events).toEqual(expect.arrayContaining(['mousedown']));
  1018. expect(widget.activeCellIndex).toBe(0);
  1019. });
  1020. it('should preserve "command" mode if in a markdown cell', () => {
  1021. const cell = widget.model!.contentFactory.createMarkdownCell({});
  1022. cell.value.text = '# Hello'; // Should be rendered with content.
  1023. widget.model!.cells.push(cell);
  1024. const count = widget.widgets.length;
  1025. const child = widget.widgets[count - 1] as MarkdownCell;
  1026. expect(child.rendered).toBe(true);
  1027. simulate(child.node, 'mousedown');
  1028. expect(child.rendered).toBe(true);
  1029. expect(widget.activeCell).toBe(child);
  1030. });
  1031. it('should extend selection if invoked with shift', () => {
  1032. widget.activeCellIndex = 3;
  1033. // shift click below
  1034. simulate(widget.widgets[4].node, 'mousedown', { shiftKey: true });
  1035. expect(widget.activeCellIndex).toBe(4);
  1036. expect(selected(widget)).toEqual([3, 4]);
  1037. // shift click above
  1038. simulate(widget.widgets[1].node, 'mousedown', { shiftKey: true });
  1039. expect(widget.activeCellIndex).toBe(1);
  1040. expect(selected(widget)).toEqual([1, 2, 3]);
  1041. // shift click expand
  1042. simulate(widget.widgets[0].node, 'mousedown', { shiftKey: true });
  1043. expect(widget.activeCellIndex).toBe(0);
  1044. expect(selected(widget)).toEqual([0, 1, 2, 3]);
  1045. // shift click contract
  1046. simulate(widget.widgets[2].node, 'mousedown', { shiftKey: true });
  1047. expect(widget.activeCellIndex).toBe(2);
  1048. expect(selected(widget)).toEqual([2, 3]);
  1049. });
  1050. it('should not extend a selection if there is text selected in the output', () => {
  1051. widget.activeCellIndex = 2;
  1052. // Set a selection in the active cell outputs.
  1053. const selection = window.getSelection()!;
  1054. selection.selectAllChildren(
  1055. (widget.activeCell as CodeCell).outputArea.node
  1056. );
  1057. // Shift click below, which should not extend cells selection.
  1058. simulate(widget.widgets[4].node, 'mousedown', { shiftKey: true });
  1059. expect(widget.activeCellIndex).toBe(2);
  1060. expect(selected(widget)).toEqual([]);
  1061. });
  1062. it('should leave a markdown cell rendered', () => {
  1063. const code = widget.model!.contentFactory.createCodeCell({});
  1064. const md = widget.model!.contentFactory.createMarkdownCell({});
  1065. md.value.text = '# Hello'; // Should be rendered with content.
  1066. widget.model!.cells.push(code);
  1067. widget.model!.cells.push(md);
  1068. const count = widget.widgets.length;
  1069. const codeChild = widget.widgets[count - 2];
  1070. const mdChild = widget.widgets[count - 1] as MarkdownCell;
  1071. widget.select(codeChild);
  1072. widget.select(mdChild);
  1073. widget.activeCellIndex = count - 2;
  1074. expect(mdChild.rendered).toBe(true);
  1075. simulate(codeChild.editorWidget.node, 'mousedown');
  1076. simulate(codeChild.editorWidget.node, 'focusin');
  1077. expect(mdChild.rendered).toBe(true);
  1078. expect(widget.activeCell).toBe(codeChild);
  1079. expect(widget.mode).toBe('edit');
  1080. });
  1081. it('should remove selection and switch to command mode', () => {
  1082. const code = widget.model!.contentFactory.createCodeCell({});
  1083. const md = widget.model!.contentFactory.createMarkdownCell({});
  1084. widget.model!.cells.push(code);
  1085. widget.model!.cells.push(md);
  1086. const count = widget.widgets.length;
  1087. const codeChild = widget.widgets[count - 2];
  1088. const mdChild = widget.widgets[count - 1] as MarkdownCell;
  1089. widget.select(codeChild);
  1090. widget.select(mdChild);
  1091. widget.activeCellIndex = count - 2;
  1092. simulate(codeChild.editorWidget.node, 'mousedown');
  1093. simulate(codeChild.editorWidget.node, 'focusin');
  1094. expect(widget.mode).toBe('edit');
  1095. simulate(codeChild.editorWidget.node, 'mousedown', { button: 2 });
  1096. expect(widget.isSelected(mdChild)).toBe(false);
  1097. expect(widget.mode).toBe('command');
  1098. });
  1099. it('should have no effect on shift right click', () => {
  1100. const code = widget.model!.contentFactory.createCodeCell({});
  1101. const md = widget.model!.contentFactory.createMarkdownCell({});
  1102. widget.model!.cells.push(code);
  1103. widget.model!.cells.push(md);
  1104. const count = widget.widgets.length;
  1105. const codeChild = widget.widgets[count - 2];
  1106. const mdChild = widget.widgets[count - 1] as MarkdownCell;
  1107. widget.select(codeChild);
  1108. widget.select(mdChild);
  1109. widget.activeCellIndex = count - 2;
  1110. simulate(codeChild.editorWidget.node, 'mousedown', {
  1111. shiftKey: true,
  1112. button: 2
  1113. });
  1114. expect(widget.isSelected(mdChild)).toBe(true);
  1115. expect(widget.mode).toBe('command');
  1116. });
  1117. });
  1118. describe('dblclick', () => {
  1119. it('should unrender a markdown cell', () => {
  1120. const cell = widget.model!.contentFactory.createMarkdownCell({});
  1121. cell.value.text = '# Hello'; // Should be rendered with content.
  1122. widget.model!.cells.push(cell);
  1123. const child = widget.widgets[
  1124. widget.widgets.length - 1
  1125. ] as MarkdownCell;
  1126. expect(child.rendered).toBe(true);
  1127. expect(widget.mode).toBe('command');
  1128. simulate(child.node, 'dblclick');
  1129. expect(widget.mode).toBe('command');
  1130. expect(child.rendered).toBe(false);
  1131. });
  1132. });
  1133. describe('focusin', () => {
  1134. it('should change to edit mode if a child cell takes focus', () => {
  1135. const child = widget.widgets[0];
  1136. simulate(child.editorWidget.node, 'focusin');
  1137. expect(widget.events).toEqual(expect.arrayContaining(['focusin']));
  1138. expect(widget.mode).toBe('edit');
  1139. });
  1140. it('should change to command mode if the widget takes focus', () => {
  1141. const child = widget.widgets[0];
  1142. simulate(child.editorWidget.node, 'focusin');
  1143. expect(widget.events).toEqual(expect.arrayContaining(['focusin']));
  1144. expect(widget.mode).toBe('edit');
  1145. widget.events = [];
  1146. simulate(widget.node, 'focusin');
  1147. expect(widget.events).toEqual(expect.arrayContaining(['focusin']));
  1148. expect(widget.mode).toBe('command');
  1149. });
  1150. });
  1151. describe('focusout', () => {
  1152. it('should switch to command mode', () => {
  1153. simulate(widget.node, 'focusin');
  1154. widget.mode = 'edit';
  1155. const event = generate('focusout');
  1156. (event as any).relatedTarget = document.body;
  1157. widget.node.dispatchEvent(event);
  1158. expect(widget.mode).toBe('command');
  1159. MessageLoop.sendMessage(widget, Widget.Msg.ActivateRequest);
  1160. expect(widget.mode).toBe('command');
  1161. expect(widget.activeCell!.editor.hasFocus()).toBe(false);
  1162. });
  1163. it('should set command mode', () => {
  1164. simulate(widget.node, 'focusin');
  1165. widget.mode = 'edit';
  1166. const evt = generate('focusout');
  1167. (evt as any).relatedTarget = widget.activeCell!.node;
  1168. widget.node.dispatchEvent(evt);
  1169. expect(widget.mode).toBe('command');
  1170. });
  1171. });
  1172. });
  1173. describe('#onAfterAttach()', () => {
  1174. it('should add event listeners', async () => {
  1175. const widget = createActiveWidget();
  1176. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1177. Widget.attach(widget, document.body);
  1178. const child = widget.widgets[0];
  1179. await framePromise();
  1180. expect(widget.methods).toEqual(
  1181. expect.arrayContaining(['onAfterAttach'])
  1182. );
  1183. simulate(widget.node, 'mousedown');
  1184. expect(widget.events).toEqual(expect.arrayContaining(['mousedown']));
  1185. simulate(widget.node, 'dblclick');
  1186. expect(widget.events).toEqual(expect.arrayContaining(['dblclick']));
  1187. simulate(child.node, 'focusin');
  1188. expect(widget.events).toEqual(expect.arrayContaining(['focusin']));
  1189. widget.dispose();
  1190. });
  1191. it('should post an update request', async () => {
  1192. const widget = createActiveWidget();
  1193. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1194. Widget.attach(widget, document.body);
  1195. await framePromise();
  1196. expect(widget.methods).toEqual(
  1197. expect.arrayContaining(['onAfterAttach'])
  1198. );
  1199. await framePromise();
  1200. expect(widget.methods).toEqual(
  1201. expect.arrayContaining(['onUpdateRequest'])
  1202. );
  1203. widget.dispose();
  1204. });
  1205. });
  1206. describe('#onBeforeDetach()', () => {
  1207. it('should remove event listeners', async () => {
  1208. const widget = createActiveWidget();
  1209. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1210. Widget.attach(widget, document.body);
  1211. const child = widget.widgets[0];
  1212. await framePromise();
  1213. Widget.detach(widget);
  1214. expect(widget.methods).toEqual(
  1215. expect.arrayContaining(['onBeforeDetach'])
  1216. );
  1217. widget.events = [];
  1218. simulate(widget.node, 'mousedown');
  1219. expect(widget.events).toEqual(
  1220. expect.not.arrayContaining(['mousedown'])
  1221. );
  1222. simulate(widget.node, 'dblclick');
  1223. expect(widget.events).toEqual(expect.not.arrayContaining(['dblclick']));
  1224. simulate(child.node, 'focusin');
  1225. expect(widget.events).toEqual(expect.not.arrayContaining(['focusin']));
  1226. widget.dispose();
  1227. });
  1228. });
  1229. describe('#onActivateRequest()', () => {
  1230. it('should focus the node after an update', async () => {
  1231. const widget = createActiveWidget();
  1232. Widget.attach(widget, document.body);
  1233. MessageLoop.sendMessage(widget, Widget.Msg.ActivateRequest);
  1234. expect(widget.methods).toEqual(
  1235. expect.arrayContaining(['onActivateRequest'])
  1236. );
  1237. await framePromise();
  1238. expect(document.activeElement).toBe(widget.node);
  1239. widget.dispose();
  1240. });
  1241. });
  1242. describe('#onUpdateRequest()', () => {
  1243. let widget: LogNotebook;
  1244. beforeEach(async () => {
  1245. widget = createActiveWidget();
  1246. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1247. Widget.attach(widget, document.body);
  1248. await framePromise();
  1249. });
  1250. afterEach(() => {
  1251. widget.dispose();
  1252. });
  1253. it('should apply the command class if in command mode', () => {
  1254. expect(widget.methods).toEqual(
  1255. expect.arrayContaining(['onUpdateRequest'])
  1256. );
  1257. expect(widget.hasClass('jp-mod-commandMode')).toBe(true);
  1258. });
  1259. it('should apply the edit class if in edit mode', async () => {
  1260. widget.mode = 'edit';
  1261. await framePromise();
  1262. expect(widget.hasClass('jp-mod-editMode')).toBe(true);
  1263. });
  1264. it('should add the active class to the active widget', () => {
  1265. const cell = widget.widgets[widget.activeCellIndex];
  1266. expect(cell.hasClass('jp-mod-active')).toBe(true);
  1267. });
  1268. it('should set the selected class on the selected widgets', async () => {
  1269. widget.select(widget.widgets[1]);
  1270. await framePromise();
  1271. for (let i = 0; i < 2; i++) {
  1272. const cell = widget.widgets[i];
  1273. expect(cell.hasClass('jp-mod-selected')).toBe(true);
  1274. }
  1275. });
  1276. it('should add the multi select class if there is more than one widget', async () => {
  1277. widget.select(widget.widgets[1]);
  1278. expect(widget.hasClass('jp-mod-multSelected')).toBe(false);
  1279. await framePromise();
  1280. expect(widget.hasClass('jp-mod-multSelected')).toBe(false);
  1281. });
  1282. });
  1283. describe('#onCellInserted()', () => {
  1284. it('should post an `update-request', async () => {
  1285. const widget = createActiveWidget();
  1286. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1287. expect(widget.methods).toEqual(
  1288. expect.arrayContaining(['onCellInserted'])
  1289. );
  1290. await framePromise();
  1291. expect(widget.methods).toEqual(
  1292. expect.arrayContaining(['onUpdateRequest'])
  1293. );
  1294. });
  1295. it('should update the active cell if necessary', () => {
  1296. const widget = createActiveWidget();
  1297. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1298. expect(widget.activeCell).toBe(widget.widgets[0]);
  1299. });
  1300. it('should keep the currently active cell active', () => {
  1301. const widget = createActiveWidget();
  1302. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1303. widget.activeCellIndex = 1;
  1304. const cell = widget.model!.contentFactory.createCodeCell({});
  1305. widget.model!.cells.insert(1, cell);
  1306. expect(widget.activeCell).toBe(widget.widgets[2]);
  1307. });
  1308. describe('`edgeRequested` signal', () => {
  1309. it('should activate the previous cell if top is requested', () => {
  1310. const widget = createActiveWidget();
  1311. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1312. widget.activeCellIndex = 1;
  1313. const child = widget.widgets[widget.activeCellIndex];
  1314. (child.editor.edgeRequested as any).emit('top');
  1315. expect(widget.activeCellIndex).toBe(0);
  1316. });
  1317. it('should activate the next cell if bottom is requested', () => {
  1318. const widget = createActiveWidget();
  1319. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1320. const child = widget.widgets[widget.activeCellIndex];
  1321. (child.editor.edgeRequested as any).emit('bottom');
  1322. expect(widget.activeCellIndex).toBe(1);
  1323. });
  1324. });
  1325. });
  1326. describe('#onCellMoved()', () => {
  1327. it('should update the active cell index if necessary', () => {
  1328. const widget = createActiveWidget();
  1329. // [fromIndex, toIndex, activeIndex], starting with activeIndex=3.
  1330. const moves = [
  1331. [0, 2, 3],
  1332. [0, 3, 2],
  1333. [0, 4, 2],
  1334. [3, 2, 2],
  1335. [3, 3, 3],
  1336. [3, 4, 4],
  1337. [4, 2, 4],
  1338. [4, 3, 4],
  1339. [4, 5, 3]
  1340. ];
  1341. moves.forEach(m => {
  1342. const [fromIndex, toIndex, activeIndex] = m;
  1343. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1344. const cell = widget.widgets[3];
  1345. widget.activeCellIndex = 3;
  1346. widget.model!.cells.move(fromIndex, toIndex);
  1347. expect(widget.activeCellIndex).toBe(activeIndex);
  1348. expect(widget.widgets[activeIndex]).toBe(cell);
  1349. });
  1350. });
  1351. });
  1352. describe('#onCellRemoved()', () => {
  1353. it('should post an `update-request', async () => {
  1354. const widget = createActiveWidget();
  1355. const cell = widget.model!.cells.get(0);
  1356. widget.model!.cells.removeValue(cell);
  1357. expect(widget.methods).toEqual(
  1358. expect.arrayContaining(['onCellRemoved'])
  1359. );
  1360. await framePromise();
  1361. expect(widget.methods).toEqual(
  1362. expect.arrayContaining(['onUpdateRequest'])
  1363. );
  1364. });
  1365. it('should update the active cell if necessary', () => {
  1366. const widget = createActiveWidget();
  1367. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1368. widget.model!.cells.remove(0);
  1369. expect(widget.activeCell).toBe(widget.widgets[0]);
  1370. });
  1371. it('should keep the currently active cell active', () => {
  1372. const widget = createActiveWidget();
  1373. widget.model!.fromJSON(utils.DEFAULT_CONTENT);
  1374. widget.activeCellIndex = 2;
  1375. widget.model!.cells.remove(1);
  1376. expect(widget.activeCell).toBe(widget.widgets[1]);
  1377. });
  1378. });
  1379. });
  1380. });