widget.spec.ts 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. // Distributed under the terms of the Modified BSD License.
  5. import { Message, MessageLoop } from '@lumino/messaging';
  6. import { Widget } from '@lumino/widgets';
  7. import { SessionContext, ISessionContext } from '@jupyterlab/apputils';
  8. import { CodeEditor, CodeEditorWrapper } from '@jupyterlab/codeeditor';
  9. import {
  10. Cell,
  11. CellModel,
  12. InputPrompt,
  13. CodeCell,
  14. CodeCellModel,
  15. MarkdownCell,
  16. RawCell,
  17. RawCellModel,
  18. MarkdownCellModel,
  19. CellFooter,
  20. CellHeader,
  21. InputArea
  22. } from '@jupyterlab/cells';
  23. import { OutputArea, OutputPrompt } from '@jupyterlab/outputarea';
  24. import {
  25. createSessionContext,
  26. framePromise,
  27. NBTestUtils,
  28. JupyterServer
  29. } from '@jupyterlab/testutils';
  30. const RENDERED_CLASS = 'jp-mod-rendered';
  31. const rendermime = NBTestUtils.defaultRenderMime();
  32. class LogBaseCell extends Cell {
  33. methods: string[] = [];
  34. constructor() {
  35. super({
  36. model: new CellModel({}),
  37. contentFactory: NBTestUtils.createBaseCellFactory()
  38. });
  39. }
  40. protected onAfterAttach(msg: Message): void {
  41. super.onAfterAttach(msg);
  42. this.methods.push('onAfterAttach');
  43. }
  44. protected onActivateRequest(msg: Message): void {
  45. super.onActivateRequest(msg);
  46. this.methods.push('onActivateRequest');
  47. }
  48. protected onUpdateRequest(msg: Message): void {
  49. super.onUpdateRequest(msg);
  50. this.methods.push('onUpdateRequest');
  51. }
  52. }
  53. class LogCodeCell extends CodeCell {
  54. methods: string[] = [];
  55. constructor() {
  56. super({
  57. model: new CodeCellModel({}),
  58. contentFactory: NBTestUtils.createCodeCellFactory(),
  59. rendermime
  60. });
  61. }
  62. protected onUpdateRequest(msg: Message): void {
  63. super.onAfterAttach(msg);
  64. this.methods.push('onUpdateRequest');
  65. }
  66. protected onMetadataChanged(model: any, args: any): void {
  67. super.onMetadataChanged(model, args);
  68. this.methods.push('onMetadataChanged');
  69. }
  70. }
  71. class LogMarkdownCell extends MarkdownCell {
  72. methods: string[] = [];
  73. protected onUpdateRequest(msg: Message): void {
  74. super.onAfterAttach(msg);
  75. this.methods.push('onUpdateRequest');
  76. }
  77. }
  78. const server = new JupyterServer();
  79. beforeAll(async () => {
  80. await server.start();
  81. });
  82. afterAll(async () => {
  83. await server.shutdown();
  84. });
  85. describe('cells/widget', () => {
  86. const editorFactory = NBTestUtils.editorFactory;
  87. describe('Cell', () => {
  88. const contentFactory = NBTestUtils.createBaseCellFactory();
  89. const model = new CellModel({});
  90. describe('#constructor()', () => {
  91. it('should create a base cell widget', () => {
  92. const widget = new Cell({ model, contentFactory }).initializeState();
  93. expect(widget).toBeInstanceOf(Cell);
  94. });
  95. it('should accept a custom contentFactory', () => {
  96. const contentFactory = NBTestUtils.createBaseCellFactory();
  97. const widget = new Cell({ model, contentFactory }).initializeState();
  98. expect(widget).toBeInstanceOf(Cell);
  99. });
  100. it('shoule accept a custom editorConfig', () => {
  101. const editorConfig: Partial<CodeEditor.IConfig> = {
  102. insertSpaces: false,
  103. matchBrackets: false
  104. };
  105. const widget = new Cell({
  106. editorConfig,
  107. model,
  108. contentFactory
  109. }).initializeState();
  110. expect(widget.editor.getOption('insertSpaces')).toEqual(false);
  111. expect(widget.editor.getOption('matchBrackets')).toEqual(false);
  112. expect(widget.editor.getOption('lineNumbers')).toEqual(
  113. CodeEditor.defaultConfig.lineNumbers
  114. );
  115. });
  116. });
  117. describe('#model', () => {
  118. it('should be the model used by the widget', () => {
  119. const model = new CellModel({});
  120. const widget = new Cell({ model, contentFactory }).initializeState();
  121. expect(widget.model).toEqual(model);
  122. });
  123. });
  124. describe('#editorWidget', () => {
  125. it('should be a code editor widget', () => {
  126. const widget = new Cell({ model, contentFactory }).initializeState();
  127. expect(widget.editorWidget).toBeInstanceOf(CodeEditorWrapper);
  128. });
  129. });
  130. describe('#editor', () => {
  131. it('should be a cell editor', () => {
  132. const widget = new Cell({ model, contentFactory }).initializeState();
  133. expect(widget.editor.uuid).toBeTruthy();
  134. });
  135. });
  136. describe('#inputArea', () => {
  137. it('should be the input area for the cell', () => {
  138. const widget = new Cell({ model }).initializeState();
  139. expect(widget.inputArea).toBeInstanceOf(InputArea);
  140. });
  141. });
  142. describe('#readOnly', () => {
  143. it('should be a boolean', () => {
  144. const widget = new Cell({ model, contentFactory }).initializeState();
  145. expect(typeof widget.readOnly).toEqual('boolean');
  146. });
  147. it('should default to false', () => {
  148. const widget = new Cell({ model, contentFactory }).initializeState();
  149. expect(widget.readOnly).toEqual(false);
  150. });
  151. it('should be settable', () => {
  152. const widget = new Cell({
  153. model,
  154. contentFactory
  155. }).initializeState();
  156. widget.readOnly = true;
  157. expect(widget.readOnly).toEqual(true);
  158. });
  159. it('should ignore being set to the same value', async () => {
  160. const widget = new LogBaseCell().initializeState();
  161. widget.readOnly = true;
  162. widget.readOnly = true;
  163. await framePromise();
  164. expect(widget.methods).toEqual(['onUpdateRequest']);
  165. });
  166. it('should reflect model metadata', () => {
  167. model.metadata.set('editable', false);
  168. const widget = new Cell({
  169. model,
  170. contentFactory
  171. }).initializeState();
  172. expect(widget.readOnly).toEqual(true);
  173. });
  174. });
  175. describe('#inputCollapsed', () => {
  176. it('should be the view state of the input being collapsed', () => {
  177. const widget = new LogBaseCell().initializeState();
  178. expect(widget.inputHidden).toEqual(false);
  179. widget.inputHidden = true;
  180. expect(widget.inputHidden).toEqual(true);
  181. });
  182. });
  183. describe('#loadEditableState()', () => {
  184. it('should load the editable state from the model', () => {
  185. const model = new CellModel({});
  186. const widget = new Cell({ model, contentFactory }).initializeState();
  187. expect(widget.readOnly).toEqual(false);
  188. model.metadata.set('editable', false);
  189. widget.loadEditableState();
  190. expect(widget.readOnly).toEqual(true);
  191. model.metadata.set('editable', true);
  192. widget.loadEditableState();
  193. expect(widget.readOnly).toEqual(false);
  194. });
  195. });
  196. describe('#saveEditableState()', () => {
  197. it('should save the editable state to the model', () => {
  198. const model = new CellModel({});
  199. const widget = new Cell({ model, contentFactory }).initializeState();
  200. expect(widget.readOnly).toEqual(false);
  201. widget.readOnly = true;
  202. widget.saveEditableState();
  203. expect(model.metadata.get('editable')).toEqual(false);
  204. widget.readOnly = false;
  205. widget.saveEditableState();
  206. // Default values are not saved explicitly
  207. expect(model.metadata.get('editable')).toEqual(undefined);
  208. });
  209. });
  210. describe('#syncEditable', () => {
  211. it('should control automatic syncing of editable state with model', () => {
  212. const model = new CellModel({});
  213. const widget = new Cell({ model, contentFactory }).initializeState();
  214. expect(widget.syncEditable).toEqual(false);
  215. expect(widget.readOnly).toEqual(false);
  216. // Not synced if setting widget attribute
  217. widget.readOnly = true;
  218. expect(model.metadata.get('editable')).toEqual(undefined);
  219. // Not synced if setting metadata attribute
  220. model.metadata.set('editable', true);
  221. expect(widget.readOnly).toEqual(true);
  222. widget.syncEditable = true;
  223. // Setting sync does an initial sync from model to view. This also sets
  224. // the metadata to undefined if it is the default value.
  225. expect(model.metadata.get('editable')).toEqual(undefined);
  226. expect(widget.readOnly).toEqual(false);
  227. // Synced if setting widget attribute
  228. widget.readOnly = true;
  229. expect(model.metadata.get('editable')).toEqual(false);
  230. // Synced if setting metadata attribute
  231. model.metadata.set('editable', true);
  232. expect(widget.readOnly).toEqual(false);
  233. });
  234. });
  235. describe('#loadCollapseState()', () => {
  236. it('should load the input collapse state from the model', () => {
  237. const model = new CellModel({});
  238. const widget = new Cell({ model, contentFactory }).initializeState();
  239. expect(widget.inputHidden).toEqual(false);
  240. model.metadata.set('jupyter', { source_hidden: true });
  241. widget.loadCollapseState();
  242. expect(widget.inputHidden).toEqual(true);
  243. model.metadata.set('jupyter', { source_hidden: false });
  244. widget.loadCollapseState();
  245. expect(widget.inputHidden).toEqual(false);
  246. });
  247. });
  248. describe('#saveCollapseState()', () => {
  249. it('should save the collapse state to the model', () => {
  250. const model = new CellModel({});
  251. const widget = new Cell({ model, contentFactory }).initializeState();
  252. expect(widget.inputHidden).toEqual(false);
  253. widget.inputHidden = true;
  254. widget.saveCollapseState();
  255. expect(model.metadata.get('jupyter')).toEqual({
  256. source_hidden: true
  257. });
  258. widget.inputHidden = false;
  259. widget.saveCollapseState();
  260. // Default values are not saved explicitly
  261. expect(model.metadata.get('jupyter')).toEqual(undefined);
  262. });
  263. });
  264. describe('#syncCollapse', () => {
  265. it('should control automatic syncing of collapse state with model', () => {
  266. const model = new CellModel({});
  267. const widget = new Cell({ model, contentFactory }).initializeState();
  268. expect(widget.syncCollapse).toEqual(false);
  269. expect(widget.inputHidden).toEqual(false);
  270. // Not synced if setting widget attribute
  271. widget.inputHidden = true;
  272. expect(model.metadata.get('jupyter')).toEqual(undefined);
  273. // Not synced if setting metadata attribute
  274. model.metadata.set('jupyter', { source_hidden: false });
  275. expect(widget.inputHidden).toEqual(true);
  276. widget.syncCollapse = true;
  277. // Setting sync does an initial sync from model to view. This also sets
  278. // the metadata to undefined if it is the default value.
  279. expect(model.metadata.get('jupyter')).toEqual(undefined);
  280. expect(widget.inputHidden).toEqual(false);
  281. // Synced if setting widget attribute
  282. widget.inputHidden = true;
  283. expect(model.metadata.get('jupyter')).toEqual({
  284. source_hidden: true
  285. });
  286. // Synced if setting metadata attribute
  287. model.metadata.set('jupyter', {});
  288. expect(widget.inputHidden).toEqual(false);
  289. });
  290. });
  291. describe('#onActivateRequest()', () => {
  292. it('should focus the cell editor', async () => {
  293. const widget = new LogBaseCell().initializeState();
  294. Widget.attach(widget, document.body);
  295. widget.activate();
  296. await framePromise();
  297. expect(widget.methods).toContain('onActivateRequest');
  298. await framePromise();
  299. expect(widget.editor.hasFocus()).toEqual(true);
  300. widget.dispose();
  301. });
  302. });
  303. describe('#setPrompt()', () => {
  304. it('should not throw an error (full test in input area)', () => {
  305. const widget = new Cell({ model, contentFactory }).initializeState();
  306. expect(() => {
  307. widget.setPrompt('');
  308. }).not.toThrow();
  309. expect(() => {
  310. widget.setPrompt('null');
  311. }).not.toThrow();
  312. expect(() => {
  313. widget.setPrompt('test');
  314. }).not.toThrow();
  315. });
  316. });
  317. describe('#dispose()', () => {
  318. it('should dispose of the resources held by the widget', () => {
  319. const widget = new Cell({ model, contentFactory }).initializeState();
  320. widget.dispose();
  321. expect(widget.isDisposed).toEqual(true);
  322. });
  323. it('should be safe to call multiple times', () => {
  324. const widget = new Cell({ model, contentFactory }).initializeState();
  325. widget.dispose();
  326. widget.dispose();
  327. expect(widget.isDisposed).toEqual(true);
  328. });
  329. });
  330. describe('#onAfterAttach()', () => {
  331. it('should run when widget is attached', () => {
  332. const widget = new LogBaseCell().initializeState();
  333. expect(widget.methods).not.toContain('onAfterAttach');
  334. Widget.attach(widget, document.body);
  335. expect(widget.methods).toContain('onAfterAttach');
  336. widget.dispose();
  337. });
  338. });
  339. describe('#onUpdateRequest()', () => {
  340. it('should update the widget', () => {
  341. const widget = new LogBaseCell().initializeState();
  342. expect(widget.methods).not.toContain('onUpdateRequest');
  343. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  344. expect(widget.methods).toContain('onUpdateRequest');
  345. });
  346. });
  347. describe('#.defaultContentFactory', () => {
  348. it('should be a contentFactory', () => {
  349. expect(Cell.defaultContentFactory).toBeInstanceOf(Cell.ContentFactory);
  350. });
  351. });
  352. describe('.ContentFactory', () => {
  353. describe('#constructor', () => {
  354. it('should create a ContentFactory', () => {
  355. const factory = new Cell.ContentFactory({ editorFactory });
  356. expect(factory).toBeInstanceOf(Cell.ContentFactory);
  357. });
  358. });
  359. describe('#editorFactory', () => {
  360. it('should be the editor factory used by the content factory', () => {
  361. const factory = new Cell.ContentFactory({ editorFactory });
  362. expect(factory.editorFactory).toEqual(editorFactory);
  363. });
  364. });
  365. describe('#createCellHeader()', () => {
  366. it('should create a new cell header', () => {
  367. const factory = new Cell.ContentFactory();
  368. expect(factory.createCellHeader()).toBeInstanceOf(CellHeader);
  369. });
  370. });
  371. describe('#createCellFooter()', () => {
  372. it('should create a new cell footer', () => {
  373. const factory = new Cell.ContentFactory();
  374. expect(factory.createCellFooter()).toBeInstanceOf(CellFooter);
  375. });
  376. });
  377. describe('#createOutputPrompt()', () => {
  378. it('should create a new output prompt', () => {
  379. const factory = new Cell.ContentFactory();
  380. expect(factory.createOutputPrompt()).toBeInstanceOf(OutputPrompt);
  381. });
  382. });
  383. describe('#createInputPrompt()', () => {
  384. it('should create a new input prompt', () => {
  385. const factory = new Cell.ContentFactory();
  386. expect(factory.createInputPrompt()).toBeInstanceOf(InputPrompt);
  387. });
  388. });
  389. });
  390. });
  391. describe('CodeCell', () => {
  392. const contentFactory = NBTestUtils.createCodeCellFactory();
  393. const model = new CodeCellModel({});
  394. describe('#constructor()', () => {
  395. it('should create a code cell widget', () => {
  396. const widget = new CodeCell({ model, rendermime, contentFactory });
  397. widget.initializeState();
  398. expect(widget).toBeInstanceOf(CodeCell);
  399. });
  400. it('should accept a custom contentFactory', () => {
  401. const contentFactory = NBTestUtils.createCodeCellFactory();
  402. const widget = new CodeCell({ model, contentFactory, rendermime });
  403. widget.initializeState();
  404. expect(widget).toBeInstanceOf(CodeCell);
  405. });
  406. });
  407. describe('#outputArea', () => {
  408. it('should be the output area used by the cell', () => {
  409. const widget = new CodeCell({ model, rendermime });
  410. widget.initializeState();
  411. expect(widget.outputArea).toBeInstanceOf(OutputArea);
  412. });
  413. });
  414. describe('#outputCollapsed', () => {
  415. it('should initialize from the model', () => {
  416. const collapsedModel = new CodeCellModel({});
  417. let widget = new CodeCell({ model: collapsedModel, rendermime });
  418. widget.initializeState();
  419. expect(widget.outputHidden).toEqual(false);
  420. collapsedModel.metadata.set('collapsed', true);
  421. widget = new CodeCell({ model: collapsedModel, rendermime });
  422. widget.initializeState();
  423. expect(widget.outputHidden).toEqual(true);
  424. collapsedModel.metadata.delete('collapsed');
  425. collapsedModel.metadata.set('jupyter', { outputs_hidden: true });
  426. widget = new CodeCell({ model: collapsedModel, rendermime });
  427. widget.initializeState();
  428. expect(widget.outputHidden).toEqual(true);
  429. });
  430. it('should be the view state of the output being collapsed', () => {
  431. const widget = new CodeCell({ model, rendermime });
  432. widget.initializeState();
  433. expect(widget.outputHidden).toEqual(false);
  434. widget.outputHidden = true;
  435. expect(widget.outputHidden).toEqual(true);
  436. });
  437. });
  438. describe('#outputsScrolled', () => {
  439. it('should initialize from the model', () => {
  440. const model = new CodeCellModel({});
  441. let widget = new CodeCell({ model, rendermime });
  442. widget.initializeState();
  443. expect(widget.outputsScrolled).toEqual(false);
  444. model.metadata.set('scrolled', false);
  445. widget = new CodeCell({ model, rendermime });
  446. widget.initializeState();
  447. expect(widget.outputsScrolled).toEqual(false);
  448. model.metadata.set('scrolled', 'auto');
  449. widget = new CodeCell({ model, rendermime });
  450. widget.initializeState();
  451. expect(widget.outputsScrolled).toEqual(false);
  452. model.metadata.set('scrolled', true);
  453. widget = new CodeCell({ model, rendermime });
  454. widget.initializeState();
  455. expect(widget.outputsScrolled).toEqual(true);
  456. });
  457. });
  458. describe('#loadScrolledState()', () => {
  459. it('should load the output scrolled state from the model', () => {
  460. const model = new CodeCellModel({});
  461. const widget = new CodeCell({ model, rendermime });
  462. widget.initializeState();
  463. expect(widget.outputsScrolled).toEqual(false);
  464. model.metadata.set('scrolled', true);
  465. widget.loadScrolledState();
  466. expect(widget.outputsScrolled).toEqual(true);
  467. model.metadata.set('scrolled', false);
  468. widget.loadScrolledState();
  469. expect(widget.outputsScrolled).toEqual(false);
  470. });
  471. });
  472. describe('#saveScrolledState()', () => {
  473. it('should save the collapse state to the model', () => {
  474. const model = new CodeCellModel({});
  475. const widget = new CodeCell({ model, rendermime });
  476. widget.initializeState();
  477. expect(widget.outputsScrolled).toEqual(false);
  478. widget.outputsScrolled = true;
  479. widget.saveScrolledState();
  480. expect(model.metadata.get('scrolled')).toEqual(true);
  481. widget.outputsScrolled = false;
  482. widget.saveScrolledState();
  483. // Default values are not saved explicitly
  484. expect(model.metadata.get('scrolled')).toEqual(undefined);
  485. });
  486. });
  487. describe('#syncScrolled', () => {
  488. it('should control automatic syncing of scrolled state with model', () => {
  489. const model = new CodeCellModel({});
  490. const widget = new CodeCell({ model, rendermime });
  491. widget.initializeState();
  492. expect(widget.syncScrolled).toEqual(false);
  493. expect(widget.outputsScrolled).toEqual(false);
  494. // Not synced if setting widget attribute
  495. widget.outputsScrolled = true;
  496. expect(model.metadata.get('scrolled')).toEqual(undefined);
  497. // Not synced if setting metadata attribute
  498. model.metadata.set('scrolled', false);
  499. expect(widget.outputsScrolled).toEqual(true);
  500. widget.syncScrolled = true;
  501. // Setting sync does an initial sync from model to view. This also sets
  502. // the metadata to undefined if it is the default value.
  503. expect(model.metadata.get('scrolled')).toEqual(undefined);
  504. expect(widget.outputsScrolled).toEqual(false);
  505. // Synced if setting widget attribute
  506. widget.outputsScrolled = true;
  507. expect(model.metadata.get('scrolled')).toEqual(true);
  508. // Synced if setting metadata attribute
  509. model.metadata.set('scrolled', false);
  510. expect(widget.outputsScrolled).toEqual(false);
  511. });
  512. });
  513. describe('#loadCollapseState()', () => {
  514. it('should load the output collapse state from the model', () => {
  515. const model = new CodeCellModel({});
  516. const widget = new CodeCell({ model, rendermime });
  517. widget.initializeState();
  518. widget.loadCollapseState();
  519. expect(widget.outputHidden).toEqual(false);
  520. model.metadata.set('collapsed', true);
  521. widget.loadCollapseState();
  522. expect(widget.outputHidden).toEqual(true);
  523. model.metadata.set('collapsed', false);
  524. widget.loadCollapseState();
  525. expect(widget.outputHidden).toEqual(false);
  526. });
  527. });
  528. describe('#saveCollapseState()', () => {
  529. it('should save the collapse state to the model `collapsed` metadata', () => {
  530. const model = new CodeCellModel({});
  531. const widget = new CodeCell({ model, rendermime });
  532. widget.initializeState();
  533. expect(widget.outputHidden).toEqual(false);
  534. widget.outputHidden = true;
  535. widget.saveCollapseState();
  536. expect(model.metadata.get('collapsed')).toEqual(true);
  537. // Default values are not saved explicitly
  538. widget.outputHidden = false;
  539. widget.saveCollapseState();
  540. expect(model.metadata.get('collapsed')).toEqual(undefined);
  541. // Default values are explicitly deleted
  542. model.metadata.set('collapsed', false);
  543. widget.outputHidden = false;
  544. widget.saveCollapseState();
  545. expect(model.metadata.get('collapsed')).toEqual(undefined);
  546. });
  547. });
  548. describe('#syncCollapse', () => {
  549. it('should control automatic syncing of collapse state with model', () => {
  550. const model = new CodeCellModel({});
  551. const widget = new CodeCell({ model, rendermime });
  552. widget.initializeState();
  553. expect(widget.syncCollapse).toEqual(false);
  554. expect(widget.outputHidden).toEqual(false);
  555. // Not synced if setting widget attribute
  556. widget.outputHidden = true;
  557. expect(model.metadata.get('collapsed')).toEqual(undefined);
  558. // Not synced if setting metadata attribute
  559. model.metadata.set('collapsed', false);
  560. expect(widget.outputHidden).toEqual(true);
  561. widget.syncCollapse = true;
  562. // Setting sync does an initial sync from model to view.
  563. expect(model.metadata.get('collapsed')).toEqual(undefined);
  564. expect(widget.outputHidden).toEqual(false);
  565. // Synced if setting widget attribute
  566. widget.outputHidden = true;
  567. expect(model.metadata.get('collapsed')).toEqual(true);
  568. // Synced if setting metadata attribute
  569. model.metadata.set('collapsed', false);
  570. expect(widget.outputHidden).toEqual(false);
  571. // Synced if deleting collapsed metadata attribute
  572. widget.outputHidden = true;
  573. expect(model.metadata.get('collapsed')).toEqual(true);
  574. model.metadata.delete('collapsed');
  575. expect(widget.outputHidden).toEqual(false);
  576. });
  577. });
  578. describe('#dispose()', () => {
  579. it('should dispose of the resources held by the widget', () => {
  580. const widget = new CodeCell({ model, rendermime, contentFactory });
  581. widget.initializeState();
  582. widget.dispose();
  583. expect(widget.isDisposed).toEqual(true);
  584. });
  585. it('should be safe to call multiple times', () => {
  586. const widget = new CodeCell({ model, rendermime, contentFactory });
  587. widget.initializeState();
  588. widget.dispose();
  589. widget.dispose();
  590. expect(widget.isDisposed).toEqual(true);
  591. });
  592. });
  593. describe('#onUpdateRequest()', () => {
  594. it('should update the widget', () => {
  595. const widget = new LogCodeCell().initializeState();
  596. expect(widget.methods).not.toContain('onUpdateRequest');
  597. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  598. expect(widget.methods).toContain('onUpdateRequest');
  599. });
  600. });
  601. describe('#onMetadataChanged()', () => {
  602. it('should fire when model metadata changes', () => {
  603. const method = 'onMetadataChanged';
  604. const widget = new LogCodeCell().initializeState();
  605. expect(widget.methods).not.toContain(method);
  606. widget.model.metadata.set('foo', 1);
  607. expect(widget.methods).toContain(method);
  608. });
  609. });
  610. describe('.execute()', () => {
  611. let sessionContext: ISessionContext;
  612. beforeEach(async () => {
  613. sessionContext = await createSessionContext();
  614. await (sessionContext as SessionContext).initialize();
  615. await sessionContext.session?.kernel?.info;
  616. });
  617. afterEach(() => {
  618. return sessionContext.shutdown();
  619. });
  620. it('should fulfill a promise if there is no code to execute', async () => {
  621. const widget = new CodeCell({ model, rendermime, contentFactory });
  622. widget.initializeState();
  623. await CodeCell.execute(widget, sessionContext);
  624. });
  625. it('should fulfill a promise if there is code to execute', async () => {
  626. const widget = new CodeCell({ model, rendermime, contentFactory });
  627. widget.initializeState();
  628. let originalCount: number;
  629. widget.model.value.text = 'foo';
  630. originalCount = widget.model.executionCount!;
  631. await CodeCell.execute(widget, sessionContext);
  632. const executionCount = widget.model.executionCount;
  633. expect(executionCount).not.toEqual(originalCount);
  634. });
  635. const TIMING_KEYS = [
  636. 'iopub.execute_input',
  637. 'shell.execute_reply.started',
  638. 'shell.execute_reply',
  639. 'iopub.status.busy',
  640. 'iopub.status.idle'
  641. ];
  642. it('should not save timing info by default', async () => {
  643. const widget = new CodeCell({ model, rendermime, contentFactory });
  644. await CodeCell.execute(widget, sessionContext);
  645. expect(widget.model.metadata.get('execution')).toBeUndefined();
  646. });
  647. it('should save timing info if requested', async () => {
  648. const widget = new CodeCell({ model, rendermime, contentFactory });
  649. await CodeCell.execute(widget, sessionContext, { recordTiming: true });
  650. expect(widget.model.metadata.get('execution')).toBeDefined();
  651. const timingInfo = widget.model.metadata.get('execution') as any;
  652. for (const key of TIMING_KEYS) {
  653. expect(timingInfo[key]).toBeDefined();
  654. }
  655. });
  656. it('should set the cell prompt properly while executing', async () => {
  657. const widget = new CodeCell({ model, rendermime, contentFactory });
  658. widget.initializeState();
  659. widget.model.value.text = 'foo';
  660. const future1 = CodeCell.execute(widget, sessionContext);
  661. expect(widget.promptNode.textContent).toEqual('[*]:');
  662. const future2 = CodeCell.execute(widget, sessionContext);
  663. expect(widget.promptNode.textContent).toEqual('[*]:');
  664. await expect(future1).rejects.toThrow('Canceled');
  665. expect(widget.promptNode.textContent).toEqual('[*]:');
  666. const msg = await future2;
  667. expect(msg).not.toBeUndefined();
  668. // The `if` is a Typescript type guard so that msg.content works below.
  669. if (msg) {
  670. expect(widget.promptNode.textContent).toEqual(
  671. `[${msg.content.execution_count}]:`
  672. );
  673. }
  674. });
  675. });
  676. });
  677. describe('MarkdownCell', () => {
  678. const contentFactory = NBTestUtils.createBaseCellFactory();
  679. const model = new MarkdownCellModel({});
  680. describe('#constructor()', () => {
  681. it('should create a markdown cell widget', () => {
  682. const widget = new MarkdownCell({ model, rendermime, contentFactory });
  683. widget.initializeState();
  684. expect(widget).toBeInstanceOf(MarkdownCell);
  685. });
  686. it('should accept a custom contentFactory', () => {
  687. const widget = new MarkdownCell({ model, rendermime, contentFactory });
  688. widget.initializeState();
  689. expect(widget).toBeInstanceOf(MarkdownCell);
  690. });
  691. it('should set the default mimetype to text/x-ipythongfm', () => {
  692. const widget = new MarkdownCell({ model, rendermime, contentFactory });
  693. widget.initializeState();
  694. expect(widget.model.mimeType).toEqual('text/x-ipythongfm');
  695. });
  696. });
  697. describe('#rendered', () => {
  698. it('should default to true', async () => {
  699. const widget = new MarkdownCell({ model, rendermime, contentFactory });
  700. widget.initializeState();
  701. Widget.attach(widget, document.body);
  702. expect(widget.rendered).toEqual(true);
  703. await framePromise();
  704. expect(widget.node.classList.contains(RENDERED_CLASS)).toEqual(true);
  705. });
  706. it('should unrender the widget', async () => {
  707. const widget = new MarkdownCell({ model, rendermime, contentFactory });
  708. widget.initializeState();
  709. Widget.attach(widget, document.body);
  710. widget.rendered = false;
  711. await framePromise();
  712. expect(widget.node.classList.contains(RENDERED_CLASS)).toEqual(false);
  713. widget.dispose();
  714. });
  715. });
  716. describe('#dispose()', () => {
  717. it('should dispose of the resources held by the widget', () => {
  718. const widget = new MarkdownCell({ model, rendermime, contentFactory });
  719. widget.initializeState();
  720. widget.dispose();
  721. expect(widget.isDisposed).toEqual(true);
  722. });
  723. it('should be safe to call multiple times', () => {
  724. const widget = new MarkdownCell({ model, rendermime, contentFactory });
  725. widget.initializeState();
  726. widget.dispose();
  727. widget.dispose();
  728. expect(widget.isDisposed).toEqual(true);
  729. });
  730. });
  731. describe('#onUpdateRequest()', () => {
  732. it('should update the widget', () => {
  733. const widget = new LogMarkdownCell({
  734. model,
  735. rendermime,
  736. contentFactory
  737. }).initializeState();
  738. expect(widget.methods).not.toContain('onUpdateRequest');
  739. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  740. expect(widget.methods).toContain('onUpdateRequest');
  741. });
  742. });
  743. });
  744. describe('RawCell', () => {
  745. const contentFactory = NBTestUtils.createBaseCellFactory();
  746. describe('#constructor()', () => {
  747. it('should create a raw cell widget', () => {
  748. const model = new RawCellModel({});
  749. const widget = new RawCell({ model, contentFactory }).initializeState();
  750. expect(widget).toBeInstanceOf(RawCell);
  751. });
  752. });
  753. });
  754. describe('CellHeader', () => {
  755. describe('#constructor()', () => {
  756. it('should create a new cell header', () => {
  757. expect(new CellHeader()).toBeInstanceOf(CellHeader);
  758. });
  759. });
  760. });
  761. describe('CellFooter', () => {
  762. describe('#constructor()', () => {
  763. it('should create a new cell footer', () => {
  764. expect(new CellFooter()).toBeInstanceOf(CellFooter);
  765. });
  766. });
  767. });
  768. });