widget.spec.ts 55 KB

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