model.spec.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { toArray } from '@lumino/algorithm';
  4. import { IChangedArgs } from '@jupyterlab/coreutils';
  5. import {
  6. CellModel,
  7. CodeCellModel,
  8. MarkdownCellModel,
  9. RawCellModel
  10. } from '@jupyterlab/cells';
  11. import * as nbformat from '@jupyterlab/nbformat';
  12. import { OutputAreaModel } from '@jupyterlab/outputarea';
  13. import { NBTestUtils } from '@jupyterlab/testutils';
  14. import { JSONObject } from '@lumino/coreutils';
  15. import { YCodeCell } from '@jupyterlab/shared-models';
  16. class TestModel extends CellModel {
  17. get type(): 'raw' {
  18. return 'raw';
  19. }
  20. }
  21. describe('cells/model', () => {
  22. describe('CellModel', () => {
  23. describe('#constructor()', () => {
  24. it('should create a cell model', () => {
  25. const model = new CellModel({});
  26. expect(model).toBeInstanceOf(CellModel);
  27. });
  28. it('should accept a base cell argument', () => {
  29. const cell: nbformat.IRawCell = {
  30. cell_type: 'raw',
  31. source: 'foo',
  32. metadata: { trusted: false }
  33. };
  34. const model = new CellModel({ cell });
  35. expect(model).toBeInstanceOf(CellModel);
  36. expect(model.value.text).toBe(cell.source);
  37. });
  38. it('should accept a base cell argument with a multiline source', () => {
  39. const cell: nbformat.IRawCell = {
  40. cell_type: 'raw',
  41. source: ['foo\n', 'bar\n', 'baz'],
  42. metadata: { trusted: false },
  43. id: 'cell_id'
  44. };
  45. const model = new CellModel({ cell });
  46. expect(model).toBeInstanceOf(CellModel);
  47. expect(model.value.text).toBe((cell.source as string[]).join(''));
  48. });
  49. it('should use the id argument', () => {
  50. const cell: nbformat.IRawCell = {
  51. cell_type: 'raw',
  52. source: ['foo\n', 'bar\n', 'baz'],
  53. metadata: { trusted: false },
  54. id: 'cell_id'
  55. };
  56. const model = new CellModel({ cell, id: 'my_id' });
  57. expect(model).toBeInstanceOf(CellModel);
  58. expect(model.id).toBe('my_id');
  59. });
  60. it('should use the cell id if an id is not supplied', () => {
  61. const cell: nbformat.IRawCell = {
  62. cell_type: 'raw',
  63. source: ['foo\n', 'bar\n', 'baz'],
  64. metadata: { trusted: false },
  65. id: 'cell_id'
  66. };
  67. const model = new CellModel({ cell });
  68. expect(model).toBeInstanceOf(CellModel);
  69. expect(model.id).toBe('cell_id');
  70. });
  71. it('should generate an id if an id or cell id is not supplied', () => {
  72. const cell = {
  73. cell_type: 'raw',
  74. source: ['foo\n', 'bar\n', 'baz'],
  75. metadata: { trusted: false }
  76. };
  77. const model = new CellModel({ cell });
  78. expect(model).toBeInstanceOf(CellModel);
  79. expect(model.id.length).toBeGreaterThan(0);
  80. });
  81. });
  82. describe('#contentChanged', () => {
  83. it('should signal when model content has changed', () => {
  84. const model = new CellModel({});
  85. let called = false;
  86. model.contentChanged.connect(() => {
  87. called = true;
  88. });
  89. expect(called).toBe(false);
  90. model.value.text = 'foo';
  91. expect(called).toBe(true);
  92. });
  93. });
  94. describe('#stateChanged', () => {
  95. it('should signal when model state has changed', () => {
  96. const model = new CodeCellModel({});
  97. let called = false;
  98. const listener = (sender: any, args: IChangedArgs<any>) => {
  99. if (args.name == 'executionCount') {
  100. expect(args.newValue).toBe(1);
  101. called = true;
  102. }
  103. };
  104. model.stateChanged.connect(listener);
  105. model.executionCount = 1;
  106. expect(called).toBe(true);
  107. });
  108. it('should not signal when model state has not changed', () => {
  109. const model = new CodeCellModel({});
  110. let called = 0;
  111. model.stateChanged.connect((model, args) => {
  112. if (args.name == 'executionCount') {
  113. called++;
  114. }
  115. });
  116. expect(called).toBe(0);
  117. model.executionCount = 1;
  118. expect(called).toBe(1);
  119. model.executionCount = 1;
  120. expect(called).toBe(1);
  121. });
  122. });
  123. describe('#trusted', () => {
  124. it('should be the trusted state of the cell', () => {
  125. const model = new CodeCellModel({});
  126. expect(model.trusted).toBe(false);
  127. model.trusted = true;
  128. expect(model.trusted).toBe(true);
  129. const other = new CodeCellModel({ cell: model.toJSON() });
  130. expect(other.trusted).toBe(true);
  131. });
  132. it('should update the trusted state of the output models', () => {
  133. const model = new CodeCellModel({});
  134. model.outputs.add(NBTestUtils.DEFAULT_OUTPUTS[0]);
  135. expect(model.outputs.get(0).trusted).toBe(false);
  136. model.trusted = true;
  137. expect(model.outputs.get(0).trusted).toBe(true);
  138. });
  139. });
  140. describe('#metadataChanged', () => {
  141. it('should signal when model metadata has changed', () => {
  142. const model = new TestModel({});
  143. const listener = (sender: any, args: any) => {
  144. value = args.newValue;
  145. };
  146. let value = '';
  147. model.metadata.changed.connect(listener);
  148. expect(Object.keys(value)).toHaveLength(0);
  149. model.metadata.set('foo', 'bar');
  150. expect(value).toBe('bar');
  151. });
  152. it('should not signal when model metadata has not changed', () => {
  153. const model = new TestModel({});
  154. let called = 0;
  155. model.metadata.changed.connect(() => {
  156. called++;
  157. });
  158. expect(called).toBe(0);
  159. model.metadata.set('foo', 'bar');
  160. expect(called).toBe(1);
  161. model.metadata.set('foo', 'bar');
  162. expect(called).toBe(1);
  163. });
  164. });
  165. describe('#source', () => {
  166. it('should default to an empty string', () => {
  167. const model = new CellModel({});
  168. expect(model.value.text).toHaveLength(0);
  169. });
  170. it('should be settable', () => {
  171. const model = new CellModel({});
  172. expect(model.value.text).toHaveLength(0);
  173. model.value.text = 'foo';
  174. expect(model.value.text).toBe('foo');
  175. });
  176. });
  177. describe('#isDisposed', () => {
  178. it('should be false by default', () => {
  179. const model = new CellModel({});
  180. expect(model.isDisposed).toBe(false);
  181. });
  182. it('should be true after model is disposed', () => {
  183. const model = new CellModel({});
  184. model.dispose();
  185. expect(model.isDisposed).toBe(true);
  186. });
  187. });
  188. describe('#dispose()', () => {
  189. it('should dispose of the resources held by the model', () => {
  190. const model = new TestModel({});
  191. model.dispose();
  192. expect(model.isDisposed).toBe(true);
  193. });
  194. it('should be safe to call multiple times', () => {
  195. const model = new CellModel({});
  196. model.dispose();
  197. model.dispose();
  198. expect(model.isDisposed).toBe(true);
  199. });
  200. });
  201. describe('#toJSON()', () => {
  202. it('should return a base cell encapsulation of the model value', () => {
  203. const cell: nbformat.IRawCell = {
  204. cell_type: 'raw',
  205. source: 'foo',
  206. metadata: { trusted: false }
  207. };
  208. const model = new TestModel({ cell });
  209. expect(model.toJSON()).not.toBe(cell);
  210. expect(model.toJSON()).toEqual(cell);
  211. });
  212. it('should always return a string source', () => {
  213. const cell: nbformat.IRawCell = {
  214. cell_type: 'raw',
  215. source: ['foo\n', 'bar\n', 'baz'],
  216. metadata: { trusted: false }
  217. };
  218. const model = new TestModel({ cell });
  219. cell.source = (cell.source as string[]).join('');
  220. expect(model.toJSON()).not.toBe(cell);
  221. expect(model.toJSON()).toEqual(cell);
  222. });
  223. });
  224. describe('#metadata', () => {
  225. it('should handle a metadata for the cell', () => {
  226. const model = new CellModel({});
  227. expect(model.metadata.get('foo')).toBeUndefined();
  228. model.metadata.set('foo', 1);
  229. expect(model.metadata.get('foo')).toBe(1);
  230. });
  231. it('should get a list of user metadata keys', () => {
  232. const model = new CellModel({});
  233. expect(toArray(model.metadata.keys())).toHaveLength(0);
  234. model.metadata.set('foo', 1);
  235. expect(model.metadata.keys()).toEqual(['foo']);
  236. });
  237. it('should trigger changed signal', () => {
  238. const model = new CellModel({});
  239. let called = false;
  240. model.metadata.changed.connect(() => {
  241. called = true;
  242. });
  243. model.metadata.set('foo', 1);
  244. expect(called).toBe(true);
  245. });
  246. });
  247. });
  248. describe('RawCellModel', () => {
  249. describe('#type', () => {
  250. it('should be set with type "raw"', () => {
  251. const model = new RawCellModel({});
  252. expect(model.type).toBe('raw');
  253. });
  254. });
  255. describe('#toJSON()', () => {
  256. it('should return a raw cell encapsulation of the model value', () => {
  257. const cell: nbformat.IRawCell = {
  258. cell_type: 'raw',
  259. source: 'foo',
  260. metadata: {},
  261. id: 'cell_id'
  262. };
  263. const model = new RawCellModel({ cell });
  264. const serialized = model.toJSON();
  265. expect(serialized).not.toBe(cell);
  266. expect(serialized).toEqual(cell);
  267. });
  268. });
  269. });
  270. describe('MarkdownCellModel', () => {
  271. describe('#type', () => {
  272. it('should be set with type "markdown"', () => {
  273. const model = new MarkdownCellModel({});
  274. expect(model.type).toBe('markdown');
  275. });
  276. });
  277. describe('#toJSON()', () => {
  278. it('should return a markdown cell encapsulation of the model value', () => {
  279. const cell: nbformat.IMarkdownCell = {
  280. cell_type: 'markdown',
  281. source: 'foo',
  282. metadata: {},
  283. id: 'cell_id'
  284. };
  285. const model = new MarkdownCellModel({ cell });
  286. const serialized = model.toJSON();
  287. expect(serialized).not.toBe(cell);
  288. expect(serialized).toEqual(cell);
  289. });
  290. });
  291. });
  292. describe('CodeCellModel', () => {
  293. describe('#constructor()', () => {
  294. it('should create a code cell model', () => {
  295. const model = new CodeCellModel({});
  296. expect(model).toBeInstanceOf(CodeCellModel);
  297. });
  298. it('should accept a code cell argument', () => {
  299. const cell: nbformat.ICodeCell = {
  300. cell_type: 'code',
  301. execution_count: 1,
  302. outputs: [
  303. {
  304. output_type: 'display_data',
  305. data: { 'text/plain': 'foo' },
  306. metadata: {}
  307. } as nbformat.IDisplayData
  308. ],
  309. source: 'foo',
  310. metadata: { trusted: false }
  311. };
  312. const model = new CodeCellModel({ cell });
  313. expect(model).toBeInstanceOf(CodeCellModel);
  314. expect(model.value.text).toBe(cell.source);
  315. });
  316. it('should connect the outputs changes to content change signal', () => {
  317. const data = {
  318. output_type: 'display_data',
  319. data: { 'text/plain': 'foo' },
  320. metadata: {}
  321. } as nbformat.IDisplayData;
  322. const model = new CodeCellModel({});
  323. let called = false;
  324. model.contentChanged.connect(() => {
  325. called = true;
  326. });
  327. expect(called).toBe(false);
  328. model.outputs.add(data);
  329. expect(called).toBe(true);
  330. });
  331. it('should sync collapsed and jupyter.outputs_hidden metadata on construction', () => {
  332. let model: CodeCellModel;
  333. let jupyter: JSONObject | undefined;
  334. // Setting `collapsed` works
  335. model = new CodeCellModel({
  336. cell: {
  337. cell_type: 'code',
  338. source: '',
  339. metadata: { collapsed: true }
  340. }
  341. });
  342. expect(model.metadata.get('collapsed')).toBe(true);
  343. jupyter = model.metadata.get('jupyter') as JSONObject;
  344. expect(jupyter.outputs_hidden).toBe(true);
  345. // Setting `jupyter.outputs_hidden` works
  346. model = new CodeCellModel({
  347. cell: {
  348. cell_type: 'code',
  349. source: '',
  350. metadata: { jupyter: { outputs_hidden: true } }
  351. }
  352. });
  353. expect(model.metadata.get('collapsed')).toBe(true);
  354. jupyter = model.metadata.get('jupyter') as JSONObject;
  355. expect(jupyter.outputs_hidden).toBe(true);
  356. // `collapsed` takes precedence
  357. model = new CodeCellModel({
  358. cell: {
  359. cell_type: 'code',
  360. source: '',
  361. metadata: { collapsed: false, jupyter: { outputs_hidden: true } }
  362. }
  363. });
  364. expect(model.metadata.get('collapsed')).toBe(false);
  365. jupyter = model.metadata.get('jupyter') as JSONObject;
  366. expect(jupyter.outputs_hidden).toBe(false);
  367. });
  368. });
  369. describe('#type', () => {
  370. it('should be set with type "code"', () => {
  371. const model = new CodeCellModel({});
  372. expect(model.type).toBe('code');
  373. });
  374. });
  375. describe('#executionCount', () => {
  376. it('should show the execution count of the cell', () => {
  377. const cell: nbformat.ICodeCell = {
  378. cell_type: 'code',
  379. execution_count: 1,
  380. outputs: [],
  381. source: 'foo',
  382. metadata: { trusted: false }
  383. };
  384. const model = new CodeCellModel({ cell });
  385. expect(model.executionCount).toBe(1);
  386. });
  387. it('should be settable', () => {
  388. const model = new CodeCellModel({});
  389. expect(model.executionCount).toBeNull();
  390. model.executionCount = 1;
  391. expect(model.executionCount).toBe(1);
  392. });
  393. it('should emit a state change signal when set', () => {
  394. const model = new CodeCellModel({});
  395. let called = false;
  396. model.stateChanged.connect(() => {
  397. called = true;
  398. });
  399. expect(model.executionCount).toBeNull();
  400. expect(called).toBe(false);
  401. model.executionCount = 1;
  402. expect(model.executionCount).toBe(1);
  403. expect(called).toBe(true);
  404. });
  405. it('should not signal when state has not changed', () => {
  406. const model = new CodeCellModel({});
  407. let called = 0;
  408. model.stateChanged.connect((model, args) => {
  409. if (args.name == 'executionCount') {
  410. called++;
  411. }
  412. });
  413. expect(model.executionCount).toBeNull();
  414. expect(called).toBe(0);
  415. model.executionCount = 1;
  416. expect(model.executionCount).toBe(1);
  417. model.executionCount = 1;
  418. expect(called).toBe(1);
  419. });
  420. it('should set dirty flag and signal', () => {
  421. const model = new CodeCellModel({});
  422. let called = 0;
  423. model.stateChanged.connect((model, args) => {
  424. if (args.name == 'isDirty') {
  425. called++;
  426. }
  427. });
  428. expect(model.executionCount).toBeNull();
  429. expect(model.isDirty).toBe(false);
  430. expect(called).toBe(0);
  431. model.executionCount = 1;
  432. expect(model.isDirty).toBe(false);
  433. expect(called).toBe(0);
  434. model.value.text = 'foo';
  435. expect(model.isDirty).toBe(true);
  436. expect(called).toBe(1);
  437. model.executionCount = 2;
  438. expect(model.isDirty).toBe(false);
  439. expect(called).toBe(2);
  440. });
  441. });
  442. describe('#outputs', () => {
  443. it('should be an output area model', () => {
  444. const model = new CodeCellModel({});
  445. expect(model.outputs).toBeInstanceOf(OutputAreaModel);
  446. });
  447. });
  448. describe('#dispose()', () => {
  449. it('should dispose of the resources held by the model', () => {
  450. const model = new CodeCellModel({});
  451. expect(model.outputs).toBeInstanceOf(OutputAreaModel);
  452. model.dispose();
  453. expect(model.isDisposed).toBe(true);
  454. expect(model.outputs).toBeNull();
  455. });
  456. it('should be safe to call multiple times', () => {
  457. const model = new CodeCellModel({});
  458. model.dispose();
  459. model.dispose();
  460. expect(model.isDisposed).toBe(true);
  461. });
  462. });
  463. describe('#toJSON()', () => {
  464. it('should return a code cell encapsulation of the model value', () => {
  465. const cell: nbformat.ICodeCell = {
  466. cell_type: 'code',
  467. execution_count: 1,
  468. outputs: [
  469. {
  470. output_type: 'display_data',
  471. data: {
  472. 'text/plain': 'foo',
  473. 'application/json': { bar: 1 }
  474. },
  475. metadata: {}
  476. } as nbformat.IDisplayData
  477. ],
  478. source: 'foo',
  479. metadata: { trusted: false },
  480. id: 'cell_id'
  481. };
  482. const model = new CodeCellModel({ cell });
  483. const serialized = model.toJSON();
  484. expect(serialized).not.toBe(cell);
  485. expect(serialized).toEqual(cell);
  486. const output = serialized.outputs[0] as any;
  487. expect(output.data['application/json']['bar']).toBe(1);
  488. });
  489. });
  490. describe('#onModelDBOutputsChange()', () => {
  491. const output0 = {
  492. output_type: 'display_data',
  493. data: {
  494. 'text/plain': 'foo',
  495. 'application/json': { foo: 1 }
  496. },
  497. metadata: {}
  498. } as nbformat.IDisplayData;
  499. const output1 = {
  500. output_type: 'display_data',
  501. data: {
  502. 'text/plain': 'bar',
  503. 'application/json': { bar: 2 }
  504. },
  505. metadata: {}
  506. } as nbformat.IDisplayData;
  507. const output2 = {
  508. output_type: 'display_data',
  509. data: {
  510. 'text/plain': 'foobar',
  511. 'application/json': { foobar: 2 }
  512. },
  513. metadata: {}
  514. } as nbformat.IDisplayData;
  515. const cell: nbformat.ICodeCell = {
  516. cell_type: 'code',
  517. execution_count: 1,
  518. outputs: [output0, output1],
  519. source: 'foo',
  520. metadata: { trusted: false },
  521. id: 'cell_id'
  522. };
  523. it('should add new items correctly', () => {
  524. const model = new CodeCellModel({});
  525. const sharedModel = model.sharedModel as YCodeCell;
  526. expect(sharedModel.ymodel.get('outputs').length).toBe(0);
  527. const newEvent0 = {
  528. type: 'add',
  529. newValues: [{ toJSON: () => output0 }],
  530. oldValues: [],
  531. oldIndex: -1,
  532. newIndex: 0
  533. } as any;
  534. model['onModelDBOutputsChange'](null as any, newEvent0);
  535. expect(sharedModel.ymodel.get('outputs').length).toBe(1);
  536. expect(sharedModel.ymodel.get('outputs').get(0)).toEqual(output0);
  537. const newEvent1 = {
  538. type: 'add',
  539. newValues: [{ toJSON: () => output1 }],
  540. oldValues: [],
  541. oldIndex: -1,
  542. newIndex: 1
  543. } as any;
  544. model['onModelDBOutputsChange'](null as any, newEvent1);
  545. expect(sharedModel.ymodel.get('outputs').length).toBe(2);
  546. expect(sharedModel.ymodel.get('outputs').get(1)).toEqual(output1);
  547. });
  548. it('should set new items correctly', () => {
  549. const model = new CodeCellModel({ cell });
  550. const sharedModel = model.sharedModel as YCodeCell;
  551. expect(sharedModel.ymodel.get('outputs').length).toBe(2);
  552. const newEvent0 = {
  553. type: 'set',
  554. newValues: [{ toJSON: () => output2 }],
  555. oldValues: [output0],
  556. oldIndex: 0,
  557. newIndex: 0
  558. } as any;
  559. model['onModelDBOutputsChange'](null as any, newEvent0);
  560. expect(sharedModel.ymodel.get('outputs').length).toBe(2);
  561. expect(sharedModel.ymodel.get('outputs').get(0)).toEqual(output2);
  562. const newEvent1 = {
  563. type: 'set',
  564. newValues: [{ toJSON: () => output2 }],
  565. oldValues: [output1],
  566. oldIndex: 1,
  567. newIndex: 1
  568. } as any;
  569. model['onModelDBOutputsChange'](null as any, newEvent1);
  570. expect(sharedModel.ymodel.get('outputs').length).toBe(2);
  571. expect(sharedModel.ymodel.get('outputs').get(1)).toEqual(output2);
  572. });
  573. it('should remove items correctly', () => {
  574. const model = new CodeCellModel({ cell });
  575. const sharedModel = model.sharedModel as YCodeCell;
  576. expect(sharedModel.ymodel.get('outputs').length).toBe(2);
  577. const newEvent0 = {
  578. type: 'remove',
  579. newValues: [],
  580. oldValues: [output0, output1],
  581. oldIndex: 0,
  582. newIndex: 0
  583. } as any;
  584. model['onModelDBOutputsChange'](null as any, newEvent0);
  585. expect(sharedModel.ymodel.get('outputs').length).toBe(0);
  586. });
  587. });
  588. describe('.metadata', () => {
  589. it('should sync collapsed and jupyter.outputs_hidden metadata when changed', () => {
  590. const metadata = new CodeCellModel({}).metadata;
  591. expect(metadata.get('collapsed')).toBeUndefined();
  592. expect(metadata.get('jupyter')).toBeUndefined();
  593. // Setting collapsed sets jupyter.outputs_hidden
  594. metadata.set('collapsed', true);
  595. expect(metadata.get('collapsed')).toBe(true);
  596. expect(metadata.get('jupyter')).toEqual({
  597. outputs_hidden: true
  598. });
  599. metadata.set('collapsed', false);
  600. expect(metadata.get('collapsed')).toBe(false);
  601. expect(metadata.get('jupyter')).toEqual({
  602. outputs_hidden: false
  603. });
  604. metadata.delete('collapsed');
  605. expect(metadata.get('collapsed')).toBeUndefined();
  606. expect(metadata.get('jupyter')).toBeUndefined();
  607. // Setting jupyter.outputs_hidden sets collapsed
  608. metadata.set('jupyter', { outputs_hidden: true });
  609. expect(metadata.get('collapsed')).toBe(true);
  610. expect(metadata.get('jupyter')).toEqual({
  611. outputs_hidden: true
  612. });
  613. metadata.set('jupyter', { outputs_hidden: false });
  614. expect(metadata.get('collapsed')).toBe(false);
  615. expect(metadata.get('jupyter')).toEqual({
  616. outputs_hidden: false
  617. });
  618. metadata.delete('jupyter');
  619. expect(metadata.get('collapsed')).toBeUndefined();
  620. expect(metadata.get('jupyter')).toBeUndefined();
  621. // Deleting jupyter.outputs_hidden preserves other jupyter fields
  622. metadata.set('jupyter', { outputs_hidden: true, other: true });
  623. expect(metadata.get('collapsed')).toBe(true);
  624. expect(metadata.get('jupyter')).toEqual({
  625. outputs_hidden: true,
  626. other: true
  627. });
  628. metadata.set('jupyter', { other: true });
  629. expect(metadata.get('collapsed')).toBeUndefined();
  630. expect(metadata.get('jupyter')).toEqual({
  631. other: true
  632. });
  633. // Deleting collapsed preserves other jupyter fields
  634. metadata.set('jupyter', { outputs_hidden: true, other: true });
  635. expect(metadata.get('collapsed')).toBe(true);
  636. expect(metadata.get('jupyter')).toEqual({
  637. outputs_hidden: true,
  638. other: true
  639. });
  640. metadata.delete('collapsed');
  641. expect(metadata.get('collapsed')).toBeUndefined();
  642. expect(metadata.get('jupyter')).toEqual({
  643. other: true
  644. });
  645. });
  646. });
  647. describe('.ContentFactory', () => {
  648. describe('#constructor()', () => {
  649. it('should create a new output area factory', () => {
  650. const factory = new CodeCellModel.ContentFactory();
  651. expect(factory).toBeInstanceOf(CodeCellModel.ContentFactory);
  652. });
  653. });
  654. describe('#createOutputArea()', () => {
  655. it('should create an output area model', () => {
  656. const factory = new CodeCellModel.ContentFactory();
  657. expect(factory.createOutputArea({ trusted: true })).toBeInstanceOf(
  658. OutputAreaModel
  659. );
  660. });
  661. });
  662. });
  663. describe('.defaultContentFactory', () => {
  664. it('should be an ContentFactory', () => {
  665. expect(CodeCellModel.defaultContentFactory).toBeInstanceOf(
  666. CodeCellModel.ContentFactory
  667. );
  668. });
  669. });
  670. });
  671. });