model.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. RawCellModel,
  8. MarkdownCellModel,
  9. CodeCellModel
  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. class TestModel extends CellModel {
  16. get type(): 'raw' {
  17. return 'raw';
  18. }
  19. }
  20. describe('cells/model', () => {
  21. describe('CellModel', () => {
  22. describe('#constructor()', () => {
  23. it('should create a cell model', () => {
  24. const model = new CellModel({});
  25. expect(model).toBeInstanceOf(CellModel);
  26. });
  27. it('should accept a base cell argument', () => {
  28. const cell: nbformat.IRawCell = {
  29. cell_type: 'raw',
  30. source: 'foo',
  31. metadata: { trusted: false }
  32. };
  33. const model = new CellModel({ cell });
  34. expect(model).toBeInstanceOf(CellModel);
  35. expect(model.value.text).toBe(cell.source);
  36. });
  37. it('should accept a base cell argument with a multiline source', () => {
  38. const cell: nbformat.IRawCell = {
  39. cell_type: 'raw',
  40. source: ['foo\n', 'bar\n', 'baz'],
  41. metadata: { trusted: false }
  42. };
  43. const model = new CellModel({ cell });
  44. expect(model).toBeInstanceOf(CellModel);
  45. expect(model.value.text).toBe((cell.source as string[]).join(''));
  46. });
  47. });
  48. describe('#contentChanged', () => {
  49. it('should signal when model content has changed', () => {
  50. const model = new CellModel({});
  51. let called = false;
  52. model.contentChanged.connect(() => {
  53. called = true;
  54. });
  55. expect(called).toBe(false);
  56. model.value.text = 'foo';
  57. expect(called).toBe(true);
  58. });
  59. });
  60. describe('#stateChanged', () => {
  61. it('should signal when model state has changed', () => {
  62. const model = new CodeCellModel({});
  63. let called = false;
  64. const listener = (sender: any, args: IChangedArgs<any>) => {
  65. expect(args.newValue).toBe(1);
  66. called = true;
  67. };
  68. model.stateChanged.connect(listener);
  69. model.executionCount = 1;
  70. expect(called).toBe(true);
  71. });
  72. it('should not signal when model state has not changed', () => {
  73. const model = new CodeCellModel({});
  74. let called = 0;
  75. model.stateChanged.connect(() => {
  76. called++;
  77. });
  78. expect(called).toBe(0);
  79. model.executionCount = 1;
  80. expect(called).toBe(1);
  81. model.executionCount = 1;
  82. expect(called).toBe(1);
  83. });
  84. });
  85. describe('#trusted', () => {
  86. it('should be the trusted state of the cell', () => {
  87. const model = new CodeCellModel({});
  88. expect(model.trusted).toBe(false);
  89. model.trusted = true;
  90. expect(model.trusted).toBe(true);
  91. const other = new CodeCellModel({ cell: model.toJSON() });
  92. expect(other.trusted).toBe(true);
  93. });
  94. it('should update the trusted state of the output models', () => {
  95. const model = new CodeCellModel({});
  96. model.outputs.add(NBTestUtils.DEFAULT_OUTPUTS[0]);
  97. expect(model.outputs.get(0).trusted).toBe(false);
  98. model.trusted = true;
  99. expect(model.outputs.get(0).trusted).toBe(true);
  100. });
  101. });
  102. describe('#metadataChanged', () => {
  103. it('should signal when model metadata has changed', () => {
  104. const model = new TestModel({});
  105. const listener = (sender: any, args: any) => {
  106. value = args.newValue;
  107. };
  108. let value = '';
  109. model.metadata.changed.connect(listener);
  110. expect(Object.keys(value)).toHaveLength(0);
  111. model.metadata.set('foo', 'bar');
  112. expect(value).toBe('bar');
  113. });
  114. it('should not signal when model metadata has not changed', () => {
  115. const model = new TestModel({});
  116. let called = 0;
  117. model.metadata.changed.connect(() => {
  118. called++;
  119. });
  120. expect(called).toBe(0);
  121. model.metadata.set('foo', 'bar');
  122. expect(called).toBe(1);
  123. model.metadata.set('foo', 'bar');
  124. expect(called).toBe(1);
  125. });
  126. });
  127. describe('#source', () => {
  128. it('should default to an empty string', () => {
  129. const model = new CellModel({});
  130. expect(model.value.text).toHaveLength(0);
  131. });
  132. it('should be settable', () => {
  133. const model = new CellModel({});
  134. expect(model.value.text).toHaveLength(0);
  135. model.value.text = 'foo';
  136. expect(model.value.text).toBe('foo');
  137. });
  138. });
  139. describe('#isDisposed', () => {
  140. it('should be false by default', () => {
  141. const model = new CellModel({});
  142. expect(model.isDisposed).toBe(false);
  143. });
  144. it('should be true after model is disposed', () => {
  145. const model = new CellModel({});
  146. model.dispose();
  147. expect(model.isDisposed).toBe(true);
  148. });
  149. });
  150. describe('#dispose()', () => {
  151. it('should dispose of the resources held by the model', () => {
  152. const model = new TestModel({});
  153. model.dispose();
  154. expect(model.isDisposed).toBe(true);
  155. });
  156. it('should be safe to call multiple times', () => {
  157. const model = new CellModel({});
  158. model.dispose();
  159. model.dispose();
  160. expect(model.isDisposed).toBe(true);
  161. });
  162. });
  163. describe('#toJSON()', () => {
  164. it('should return a base cell encapsulation of the model value', () => {
  165. const cell: nbformat.IRawCell = {
  166. cell_type: 'raw',
  167. source: 'foo',
  168. metadata: { trusted: false }
  169. };
  170. const model = new TestModel({ cell });
  171. expect(model.toJSON()).not.toBe(cell);
  172. expect(model.toJSON()).toEqual(cell);
  173. });
  174. it('should always return a string source', () => {
  175. const cell: nbformat.IRawCell = {
  176. cell_type: 'raw',
  177. source: ['foo\n', 'bar\n', 'baz'],
  178. metadata: { trusted: false }
  179. };
  180. const model = new TestModel({ cell });
  181. cell.source = (cell.source as string[]).join('');
  182. expect(model.toJSON()).not.toBe(cell);
  183. expect(model.toJSON()).toEqual(cell);
  184. });
  185. });
  186. describe('#metadata', () => {
  187. it('should handle a metadata for the cell', () => {
  188. const model = new CellModel({});
  189. expect(model.metadata.get('foo')).toBeUndefined();
  190. model.metadata.set('foo', 1);
  191. expect(model.metadata.get('foo')).toBe(1);
  192. });
  193. it('should get a list of user metadata keys', () => {
  194. const model = new CellModel({});
  195. expect(toArray(model.metadata.keys())).toHaveLength(0);
  196. model.metadata.set('foo', 1);
  197. expect(model.metadata.keys()).toEqual(['foo']);
  198. });
  199. it('should trigger changed signal', () => {
  200. const model = new CellModel({});
  201. let called = false;
  202. model.metadata.changed.connect(() => {
  203. called = true;
  204. });
  205. model.metadata.set('foo', 1);
  206. expect(called).toBe(true);
  207. });
  208. });
  209. });
  210. describe('RawCellModel', () => {
  211. describe('#type', () => {
  212. it('should be set with type "raw"', () => {
  213. const model = new RawCellModel({});
  214. expect(model.type).toBe('raw');
  215. });
  216. });
  217. });
  218. describe('MarkdownCellModel', () => {
  219. describe('#type', () => {
  220. it('should be set with type "markdown"', () => {
  221. const model = new MarkdownCellModel({});
  222. expect(model.type).toBe('markdown');
  223. });
  224. });
  225. });
  226. describe('CodeCellModel', () => {
  227. describe('#constructor()', () => {
  228. it('should create a code cell model', () => {
  229. const model = new CodeCellModel({});
  230. expect(model).toBeInstanceOf(CodeCellModel);
  231. });
  232. it('should accept a code cell argument', () => {
  233. const cell: nbformat.ICodeCell = {
  234. cell_type: 'code',
  235. execution_count: 1,
  236. outputs: [
  237. {
  238. output_type: 'display_data',
  239. data: { 'text/plain': 'foo' },
  240. metadata: {}
  241. } as nbformat.IDisplayData
  242. ],
  243. source: 'foo',
  244. metadata: { trusted: false }
  245. };
  246. const model = new CodeCellModel({ cell });
  247. expect(model).toBeInstanceOf(CodeCellModel);
  248. expect(model.value.text).toBe(cell.source);
  249. });
  250. it('should connect the outputs changes to content change signal', () => {
  251. const data = {
  252. output_type: 'display_data',
  253. data: { 'text/plain': 'foo' },
  254. metadata: {}
  255. } as nbformat.IDisplayData;
  256. const model = new CodeCellModel({});
  257. let called = false;
  258. model.contentChanged.connect(() => {
  259. called = true;
  260. });
  261. expect(called).toBe(false);
  262. model.outputs.add(data);
  263. expect(called).toBe(true);
  264. });
  265. it('should sync collapsed and jupyter.outputs_hidden metadata on construction', () => {
  266. let model: CodeCellModel;
  267. let jupyter: JSONObject | undefined;
  268. // Setting `collapsed` works
  269. model = new CodeCellModel({
  270. cell: {
  271. cell_type: 'code',
  272. source: '',
  273. metadata: { collapsed: true }
  274. }
  275. });
  276. expect(model.metadata.get('collapsed')).toBe(true);
  277. jupyter = model.metadata.get('jupyter') as JSONObject;
  278. expect(jupyter.outputs_hidden).toBe(true);
  279. // Setting `jupyter.outputs_hidden` works
  280. model = new CodeCellModel({
  281. cell: {
  282. cell_type: 'code',
  283. source: '',
  284. metadata: { jupyter: { outputs_hidden: true } }
  285. }
  286. });
  287. expect(model.metadata.get('collapsed')).toBe(true);
  288. jupyter = model.metadata.get('jupyter') as JSONObject;
  289. expect(jupyter.outputs_hidden).toBe(true);
  290. // `collapsed` takes precedence
  291. model = new CodeCellModel({
  292. cell: {
  293. cell_type: 'code',
  294. source: '',
  295. metadata: { collapsed: false, jupyter: { outputs_hidden: true } }
  296. }
  297. });
  298. expect(model.metadata.get('collapsed')).toBe(false);
  299. jupyter = model.metadata.get('jupyter') as JSONObject;
  300. expect(jupyter.outputs_hidden).toBe(false);
  301. });
  302. });
  303. describe('#type', () => {
  304. it('should be set with type "code"', () => {
  305. const model = new CodeCellModel({});
  306. expect(model.type).toBe('code');
  307. });
  308. });
  309. describe('#executionCount', () => {
  310. it('should show the execution count of the cell', () => {
  311. const cell: nbformat.ICodeCell = {
  312. cell_type: 'code',
  313. execution_count: 1,
  314. outputs: [],
  315. source: 'foo',
  316. metadata: { trusted: false }
  317. };
  318. const model = new CodeCellModel({ cell });
  319. expect(model.executionCount).toBe(1);
  320. });
  321. it('should be settable', () => {
  322. const model = new CodeCellModel({});
  323. expect(model.executionCount).toBeNull();
  324. model.executionCount = 1;
  325. expect(model.executionCount).toBe(1);
  326. });
  327. it('should emit a state change signal when set', () => {
  328. const model = new CodeCellModel({});
  329. let called = false;
  330. model.stateChanged.connect(() => {
  331. called = true;
  332. });
  333. expect(model.executionCount).toBeNull();
  334. expect(called).toBe(false);
  335. model.executionCount = 1;
  336. expect(model.executionCount).toBe(1);
  337. expect(called).toBe(true);
  338. });
  339. it('should not signal when state has not changed', () => {
  340. const model = new CodeCellModel({});
  341. let called = 0;
  342. model.stateChanged.connect(() => {
  343. called++;
  344. });
  345. expect(model.executionCount).toBeNull();
  346. expect(called).toBe(0);
  347. model.executionCount = 1;
  348. expect(model.executionCount).toBe(1);
  349. model.executionCount = 1;
  350. expect(called).toBe(1);
  351. });
  352. });
  353. describe('#outputs', () => {
  354. it('should be an output area model', () => {
  355. const model = new CodeCellModel({});
  356. expect(model.outputs).toBeInstanceOf(OutputAreaModel);
  357. });
  358. });
  359. describe('#dispose()', () => {
  360. it('should dispose of the resources held by the model', () => {
  361. const model = new CodeCellModel({});
  362. expect(model.outputs).toBeInstanceOf(OutputAreaModel);
  363. model.dispose();
  364. expect(model.isDisposed).toBe(true);
  365. expect(model.outputs).toBeNull();
  366. });
  367. it('should be safe to call multiple times', () => {
  368. const model = new CodeCellModel({});
  369. model.dispose();
  370. model.dispose();
  371. expect(model.isDisposed).toBe(true);
  372. });
  373. });
  374. describe('#toJSON()', () => {
  375. it('should return a code cell encapsulation of the model value', () => {
  376. const cell: nbformat.ICodeCell = {
  377. cell_type: 'code',
  378. execution_count: 1,
  379. outputs: [
  380. {
  381. output_type: 'display_data',
  382. data: {
  383. 'text/plain': 'foo',
  384. 'application/json': { bar: 1 }
  385. },
  386. metadata: {}
  387. } as nbformat.IDisplayData
  388. ],
  389. source: 'foo',
  390. metadata: { trusted: false }
  391. };
  392. const model = new CodeCellModel({ cell });
  393. const serialized = model.toJSON();
  394. expect(serialized).not.toBe(cell);
  395. expect(serialized).toEqual(cell);
  396. const output = serialized.outputs[0] as any;
  397. expect(output.data['application/json']['bar']).toBe(1);
  398. });
  399. });
  400. describe('.metadata', () => {
  401. it('should sync collapsed and jupyter.outputs_hidden metadata when changed', () => {
  402. const metadata = new CodeCellModel({}).metadata;
  403. expect(metadata.get('collapsed')).toBeUndefined();
  404. expect(metadata.get('jupyter')).toBeUndefined();
  405. // Setting collapsed sets jupyter.outputs_hidden
  406. metadata.set('collapsed', true);
  407. expect(metadata.get('collapsed')).toBe(true);
  408. expect(metadata.get('jupyter')).toEqual({
  409. outputs_hidden: true
  410. });
  411. metadata.set('collapsed', false);
  412. expect(metadata.get('collapsed')).toBe(false);
  413. expect(metadata.get('jupyter')).toEqual({
  414. outputs_hidden: false
  415. });
  416. metadata.delete('collapsed');
  417. expect(metadata.get('collapsed')).toBeUndefined();
  418. expect(metadata.get('jupyter')).toBeUndefined();
  419. // Setting jupyter.outputs_hidden sets collapsed
  420. metadata.set('jupyter', { outputs_hidden: true });
  421. expect(metadata.get('collapsed')).toBe(true);
  422. expect(metadata.get('jupyter')).toEqual({
  423. outputs_hidden: true
  424. });
  425. metadata.set('jupyter', { outputs_hidden: false });
  426. expect(metadata.get('collapsed')).toBe(false);
  427. expect(metadata.get('jupyter')).toEqual({
  428. outputs_hidden: false
  429. });
  430. metadata.delete('jupyter');
  431. expect(metadata.get('collapsed')).toBeUndefined();
  432. expect(metadata.get('jupyter')).toBeUndefined();
  433. // Deleting jupyter.outputs_hidden preserves other jupyter fields
  434. metadata.set('jupyter', { outputs_hidden: true, other: true });
  435. expect(metadata.get('collapsed')).toBe(true);
  436. expect(metadata.get('jupyter')).toEqual({
  437. outputs_hidden: true,
  438. other: true
  439. });
  440. metadata.set('jupyter', { other: true });
  441. expect(metadata.get('collapsed')).toBeUndefined();
  442. expect(metadata.get('jupyter')).toEqual({
  443. other: true
  444. });
  445. // Deleting collapsed preserves other jupyter fields
  446. metadata.set('jupyter', { outputs_hidden: true, other: true });
  447. expect(metadata.get('collapsed')).toBe(true);
  448. expect(metadata.get('jupyter')).toEqual({
  449. outputs_hidden: true,
  450. other: true
  451. });
  452. metadata.delete('collapsed');
  453. expect(metadata.get('collapsed')).toBeUndefined();
  454. expect(metadata.get('jupyter')).toEqual({
  455. other: true
  456. });
  457. });
  458. });
  459. describe('.ContentFactory', () => {
  460. describe('#constructor()', () => {
  461. it('should create a new output area factory', () => {
  462. const factory = new CodeCellModel.ContentFactory();
  463. expect(factory).toBeInstanceOf(CodeCellModel.ContentFactory);
  464. });
  465. });
  466. describe('#createOutputArea()', () => {
  467. it('should create an output area model', () => {
  468. const factory = new CodeCellModel.ContentFactory();
  469. expect(factory.createOutputArea({ trusted: true })).toBeInstanceOf(
  470. OutputAreaModel
  471. );
  472. });
  473. });
  474. });
  475. describe('.defaultContentFactory', () => {
  476. it('should be an ContentFactory', () => {
  477. expect(CodeCellModel.defaultContentFactory).toBeInstanceOf(
  478. CodeCellModel.ContentFactory
  479. );
  480. });
  481. });
  482. });
  483. });