widget.spec.ts 31 KB

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