widget.spec.ts 54 KB

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