model.spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { CodeCellModel } from '@jupyterlab/cells';
  4. import * as nbformat from '@jupyterlab/nbformat';
  5. import { ModelDB } from '@jupyterlab/observables';
  6. import { acceptDialog } from '@jupyterlab/testutils';
  7. import { ArrayExt, toArray } from '@lumino/algorithm';
  8. import { NotebookModel } from '../src';
  9. import * as utils from './utils';
  10. describe('@jupyterlab/notebook', () => {
  11. describe('NotebookModel', () => {
  12. describe('#constructor()', () => {
  13. it('should create a notebook model', () => {
  14. const model = new NotebookModel();
  15. expect(model).toBeInstanceOf(NotebookModel);
  16. });
  17. it('should accept an optional language preference', () => {
  18. const model = new NotebookModel({ languagePreference: 'python' });
  19. const lang = model.metadata.get(
  20. 'language_info'
  21. ) as nbformat.ILanguageInfoMetadata;
  22. expect(lang.name).toBe('python');
  23. });
  24. it('should accept an optional factory', () => {
  25. const contentFactory = new NotebookModel.ContentFactory({});
  26. const model = new NotebookModel({ contentFactory });
  27. expect(model.contentFactory.codeCellContentFactory).toBe(
  28. contentFactory.codeCellContentFactory
  29. );
  30. });
  31. });
  32. describe('#metadataChanged', () => {
  33. it('should be emitted when a metadata field changes', () => {
  34. const model = new NotebookModel();
  35. let called = false;
  36. model.metadata.changed.connect((sender, args) => {
  37. expect(sender).toBe(model.metadata);
  38. expect(args.key).toBe('foo');
  39. expect(args.oldValue).toBeUndefined();
  40. expect(args.newValue).toBe(1);
  41. called = true;
  42. });
  43. model.metadata.set('foo', 1);
  44. expect(called).toBe(true);
  45. });
  46. it('should not be emitted when the value does not change', () => {
  47. const model = new NotebookModel();
  48. let called = false;
  49. model.metadata.set('foo', 1);
  50. model.metadata.changed.connect(() => {
  51. called = true;
  52. });
  53. model.metadata.set('foo', 1);
  54. expect(called).toBe(false);
  55. });
  56. });
  57. describe('#cells', () => {
  58. it('should be reset when loading from disk', () => {
  59. const model = new NotebookModel();
  60. const cell = model.contentFactory.createCodeCell({});
  61. model.cells.push(cell);
  62. model.fromJSON(utils.DEFAULT_CONTENT);
  63. expect(ArrayExt.firstIndexOf(toArray(model.cells), cell)).toBe(-1);
  64. expect(model.cells.length).toBe(7);
  65. });
  66. it('should allow undoing a change', () => {
  67. const model = new NotebookModel();
  68. const cell = model.contentFactory.createCodeCell({});
  69. cell.value.text = 'foo';
  70. const cellJSON = cell.toJSON();
  71. model.cells.push(cell);
  72. model.cells.clearUndo();
  73. model.cells.remove(model.cells.length - 1);
  74. model.cells.undo();
  75. expect(model.cells.length).toBe(1);
  76. expect(model.cells.get(0).value.text).toBe('foo');
  77. // Previous model matches the restored model
  78. expect(model.cells.get(0).toJSON()).toEqual(cellJSON);
  79. });
  80. describe('cells `changed` signal', () => {
  81. it('should emit a `contentChanged` signal upon cell addition', () => {
  82. const model = new NotebookModel();
  83. const cell = model.contentFactory.createCodeCell({});
  84. let called = false;
  85. model.contentChanged.connect(() => {
  86. called = true;
  87. });
  88. model.cells.push(cell);
  89. expect(called).toBe(true);
  90. });
  91. it('should emit a `contentChanged` signal upon cell removal', () => {
  92. const model = new NotebookModel();
  93. const cell = model.contentFactory.createCodeCell({});
  94. model.cells.push(cell);
  95. let called = false;
  96. model.contentChanged.connect(() => {
  97. called = true;
  98. });
  99. model.cells.remove(0);
  100. expect(called).toBe(true);
  101. });
  102. it('should emit a `contentChanged` signal upon cell move', () => {
  103. const model = new NotebookModel();
  104. const cell0 = model.contentFactory.createCodeCell({});
  105. const cell1 = model.contentFactory.createCodeCell({});
  106. model.cells.push(cell0);
  107. model.cells.push(cell1);
  108. let called = false;
  109. model.contentChanged.connect(() => {
  110. called = true;
  111. });
  112. model.cells.move(0, 1);
  113. expect(called).toBe(true);
  114. });
  115. it('should set the dirty flag', () => {
  116. const model = new NotebookModel();
  117. const cell = model.contentFactory.createCodeCell({});
  118. model.cells.push(cell);
  119. expect(model.dirty).toBe(true);
  120. });
  121. });
  122. describe('cell `changed` signal', () => {
  123. it('should be called when a cell content changes', () => {
  124. const model = new NotebookModel();
  125. const cell = model.contentFactory.createCodeCell({});
  126. model.cells.push(cell);
  127. cell.value.text = 'foo';
  128. });
  129. it('should emit the `contentChanged` signal', () => {
  130. const model = new NotebookModel();
  131. const cell = model.contentFactory.createCodeCell({});
  132. model.cells.push(cell);
  133. let called = false;
  134. model.contentChanged.connect(() => {
  135. called = true;
  136. });
  137. model.metadata.set('foo', 'bar');
  138. expect(called).toBe(true);
  139. });
  140. it('should set the dirty flag', () => {
  141. const model = new NotebookModel();
  142. const cell = model.contentFactory.createCodeCell({});
  143. model.cells.push(cell);
  144. model.dirty = false;
  145. cell.value.text = 'foo';
  146. expect(model.dirty).toBe(true);
  147. });
  148. });
  149. });
  150. describe('#contentFactory', () => {
  151. it('should be the cell model factory used by the model', () => {
  152. const model = new NotebookModel();
  153. expect(model.contentFactory.codeCellContentFactory).toBe(
  154. NotebookModel.defaultContentFactory.codeCellContentFactory
  155. );
  156. });
  157. });
  158. describe('#nbformat', () => {
  159. it('should get the major version number of the nbformat', () => {
  160. const model = new NotebookModel();
  161. model.fromJSON(utils.DEFAULT_CONTENT);
  162. expect(model.nbformat).toBe(utils.DEFAULT_CONTENT.nbformat);
  163. });
  164. it('should present a dialog when the format changes', () => {
  165. const model = new NotebookModel();
  166. const content = {
  167. ...utils.DEFAULT_CONTENT,
  168. metadata: {
  169. ...utils.DEFAULT_CONTENT.metadata,
  170. orig_nbformat: 1
  171. }
  172. };
  173. model.fromJSON(content);
  174. expect(model.nbformat).toBe(nbformat.MAJOR_VERSION);
  175. return acceptDialog();
  176. });
  177. });
  178. describe('#nbformatMinor', () => {
  179. it('should get the minor version number of the nbformat', () => {
  180. const model = new NotebookModel();
  181. model.fromJSON(utils.DEFAULT_CONTENT);
  182. expect(model.nbformatMinor).toBe(nbformat.MINOR_VERSION);
  183. });
  184. });
  185. describe('#defaultKernelName()', () => {
  186. it('should get the default kernel name of the document', () => {
  187. const model = new NotebookModel();
  188. model.metadata.set('kernelspec', { name: 'python3' });
  189. expect(model.defaultKernelName).toBe('python3');
  190. });
  191. it('should default to an empty string', () => {
  192. const model = new NotebookModel();
  193. expect(model.defaultKernelName).toBe('');
  194. });
  195. });
  196. describe('#defaultKernelLanguage', () => {
  197. it('should get the default kernel language of the document', () => {
  198. const model = new NotebookModel();
  199. model.metadata.set('language_info', { name: 'python' });
  200. expect(model.defaultKernelLanguage).toBe('python');
  201. });
  202. it('should default to an empty string', () => {
  203. const model = new NotebookModel();
  204. expect(model.defaultKernelLanguage).toBe('');
  205. });
  206. it('should be set from the constructor arg', () => {
  207. const model = new NotebookModel({ languagePreference: 'foo' });
  208. expect(model.defaultKernelLanguage).toBe('foo');
  209. });
  210. });
  211. describe('#dispose()', () => {
  212. it('should dispose of the resources held by the model', () => {
  213. const model = new NotebookModel();
  214. model.fromJSON(utils.DEFAULT_CONTENT);
  215. model.dispose();
  216. expect(model.cells).toBeNull();
  217. expect(model.isDisposed).toBe(true);
  218. });
  219. it('should be safe to call multiple times', () => {
  220. const model = new NotebookModel();
  221. model.dispose();
  222. model.dispose();
  223. expect(model.isDisposed).toBe(true);
  224. });
  225. });
  226. describe('#toString()', () => {
  227. it('should serialize the model to a string', () => {
  228. const model = new NotebookModel();
  229. model.fromJSON(utils.DEFAULT_CONTENT);
  230. const text = model.toString();
  231. const data = JSON.parse(text);
  232. expect(data.cells.length).toBe(7);
  233. });
  234. });
  235. describe('#fromString()', () => {
  236. it('should deserialize the model from a string', () => {
  237. const model = new NotebookModel();
  238. model.fromString(JSON.stringify(utils.DEFAULT_CONTENT));
  239. expect(model.cells.length).toBe(7);
  240. });
  241. it('should set the dirty flag', () => {
  242. const model = new NotebookModel();
  243. model.dirty = false;
  244. model.fromString(JSON.stringify(utils.DEFAULT_CONTENT));
  245. expect(model.dirty).toBe(true);
  246. });
  247. });
  248. describe('#toJSON()', () => {
  249. it('should serialize the model to JSON', () => {
  250. const model = new NotebookModel();
  251. model.fromJSON(utils.DEFAULT_CONTENT);
  252. const data = model.toJSON();
  253. expect(data.cells.length).toBe(7);
  254. });
  255. it('should serialize format 4.4 or earlier without cell ids', () => {
  256. const model = new NotebookModel();
  257. model.fromJSON(utils.DEFAULT_CONTENT);
  258. const data = model.toJSON();
  259. expect(data.nbformat).toBe(4);
  260. expect(data.nbformat_minor).toBeLessThanOrEqual(4);
  261. expect(data.cells.length).toBe(7);
  262. expect(data.cells[0].id).toBeUndefined();
  263. });
  264. it('should serialize format 4.5 or later with cell ids', () => {
  265. const model = new NotebookModel();
  266. model.fromJSON(utils.DEFAULT_CONTENT_45);
  267. const data = model.toJSON();
  268. expect(data.cells.length).toBe(7);
  269. expect(data.cells[0].id).toBe('cell_1');
  270. });
  271. });
  272. describe('#fromJSON()', () => {
  273. it('should serialize the model from format<=4.4 JSON', () => {
  274. const model = new NotebookModel();
  275. model.fromJSON(utils.DEFAULT_CONTENT);
  276. expect(model.cells.length).toBe(7);
  277. expect(model.nbformat).toBe(utils.DEFAULT_CONTENT.nbformat);
  278. expect(model.nbformatMinor).toBe(nbformat.MINOR_VERSION);
  279. });
  280. it('should serialize the model from format 4.5 JSON', () => {
  281. const model = new NotebookModel();
  282. const json = utils.DEFAULT_CONTENT_45;
  283. model.fromJSON(json);
  284. expect(model.cells.length).toBe(7);
  285. expect(model.nbformat).toBe(json.nbformat);
  286. expect(model.nbformatMinor).toBe(json.nbformat_minor);
  287. expect(model.cells.get(0).id).toBe('cell_1');
  288. });
  289. it('should set the dirty flag', () => {
  290. const model = new NotebookModel();
  291. model.dirty = false;
  292. model.fromJSON(utils.DEFAULT_CONTENT);
  293. expect(model.dirty).toBe(true);
  294. });
  295. });
  296. describe('#metadata', () => {
  297. it('should have default values', () => {
  298. const model = new NotebookModel();
  299. const metadata = model.metadata;
  300. expect(metadata.has('kernelspec')).toBeTruthy();
  301. expect(metadata.has('language_info')).toBeTruthy();
  302. expect(metadata.size).toBe(2);
  303. });
  304. it('should set the dirty flag when changed', () => {
  305. const model = new NotebookModel();
  306. expect(model.dirty).toBe(false);
  307. model.metadata.set('foo', 'bar');
  308. expect(model.dirty).toBe(true);
  309. });
  310. it('should emit the `contentChanged` signal', () => {
  311. const model = new NotebookModel();
  312. let called = false;
  313. model.contentChanged.connect(() => {
  314. called = true;
  315. });
  316. model.metadata.set('foo', 'bar');
  317. expect(called).toBe(true);
  318. });
  319. it('should emit the `metadataChanged` signal', () => {
  320. const model = new NotebookModel();
  321. let called = false;
  322. model.metadata.changed.connect((sender, args) => {
  323. expect(sender).toBe(model.metadata);
  324. expect(args.key).toBe('foo');
  325. expect(args.oldValue).toBeUndefined();
  326. expect(args.newValue).toBe('bar');
  327. called = true;
  328. });
  329. model.metadata.set('foo', 'bar');
  330. expect(called).toBe(true);
  331. });
  332. });
  333. describe('#initialize()', () => {
  334. it('should add one code cell if the model is empty', () => {
  335. const model = new NotebookModel();
  336. expect(model.cells.length).toBe(0);
  337. model.initialize();
  338. expect(model.cells.length).toBe(1);
  339. expect(model.cells.get(0).type).toBe('code');
  340. });
  341. it('should clear undo state', () => {
  342. const model = new NotebookModel();
  343. const cell = model.contentFactory.createCodeCell({});
  344. cell.value.text = 'foo';
  345. model.cells.push(cell);
  346. expect(model.cells.canUndo).toBe(true);
  347. model.initialize();
  348. expect(model.cells.canUndo).toBe(false);
  349. });
  350. });
  351. describe('.ContentFactory', () => {
  352. let factory = new NotebookModel.ContentFactory({});
  353. describe('#codeCellContentFactory', () => {
  354. it('should be a code cell content factory', () => {
  355. expect(factory.codeCellContentFactory).toBe(
  356. CodeCellModel.defaultContentFactory
  357. );
  358. });
  359. it('should be settable in the constructor', () => {
  360. const codeCellContentFactory = new CodeCellModel.ContentFactory();
  361. factory = new NotebookModel.ContentFactory({
  362. codeCellContentFactory
  363. });
  364. expect(factory.codeCellContentFactory).toBe(codeCellContentFactory);
  365. });
  366. });
  367. describe('#createCell()', () => {
  368. it('should create a new code cell', () => {
  369. const cell = factory.createCell('code', {});
  370. expect(cell.type).toBe('code');
  371. });
  372. it('should create a new markdown cell', () => {
  373. const cell = factory.createCell('markdown', {});
  374. expect(cell.type).toBe('markdown');
  375. });
  376. it('should create a new raw cell', () => {
  377. const cell = factory.createCell('raw', {});
  378. expect(cell.type).toBe('raw');
  379. });
  380. });
  381. describe('#createCodeCell()', () => {
  382. it('should create a new code cell', () => {
  383. const cell = factory.createCodeCell({});
  384. expect(cell.type).toBe('code');
  385. });
  386. it('should clone an existing code cell', () => {
  387. const orig = factory.createCodeCell({});
  388. orig.value.text = 'foo';
  389. const cell = orig.toJSON();
  390. const newCell = factory.createCodeCell({ cell });
  391. expect(newCell.value.text).toBe('foo');
  392. });
  393. it('should clone an existing raw cell', () => {
  394. const orig = factory.createRawCell({});
  395. orig.value.text = 'foo';
  396. const cell = orig.toJSON();
  397. const newCell = factory.createCodeCell({ cell });
  398. expect(newCell.value.text).toBe('foo');
  399. });
  400. });
  401. describe('#createRawCell()', () => {
  402. it('should create a new raw cell', () => {
  403. const cell = factory.createRawCell({});
  404. expect(cell.type).toBe('raw');
  405. });
  406. it('should clone an existing raw cell', () => {
  407. const orig = factory.createRawCell({});
  408. orig.value.text = 'foo';
  409. const cell = orig.toJSON();
  410. const newCell = factory.createRawCell({ cell });
  411. expect(newCell.value.text).toBe('foo');
  412. });
  413. it('should clone an existing code cell', () => {
  414. const orig = factory.createCodeCell({});
  415. orig.value.text = 'foo';
  416. const cell = orig.toJSON();
  417. const newCell = factory.createRawCell({ cell });
  418. expect(newCell.value.text).toBe('foo');
  419. });
  420. });
  421. describe('#createMarkdownCell()', () => {
  422. it('should create a new markdown cell', () => {
  423. const cell = factory.createMarkdownCell({});
  424. expect(cell.type).toBe('markdown');
  425. });
  426. it('should clone an existing markdown cell', () => {
  427. const orig = factory.createMarkdownCell({});
  428. orig.value.text = 'foo';
  429. const cell = orig.toJSON();
  430. const newCell = factory.createMarkdownCell({ cell });
  431. expect(newCell.value.text).toBe('foo');
  432. });
  433. it('should clone an existing raw cell', () => {
  434. const orig = factory.createRawCell({});
  435. orig.value.text = 'foo';
  436. const cell = orig.toJSON();
  437. const newCell = factory.createMarkdownCell({ cell });
  438. expect(newCell.value.text).toBe('foo');
  439. });
  440. });
  441. describe('#modelDB', () => {
  442. it('should be undefined by default', () => {
  443. expect(factory.modelDB).toBeUndefined();
  444. });
  445. });
  446. describe('#clone()', () => {
  447. it('should create a new content factory with a new IModelDB', () => {
  448. const modelDB = new ModelDB();
  449. const factory = new NotebookModel.ContentFactory({ modelDB });
  450. expect(factory.modelDB).toBe(modelDB);
  451. const newModelDB = new ModelDB();
  452. const newFactory = factory.clone(newModelDB);
  453. expect(newFactory.modelDB).toBe(newModelDB);
  454. expect(newFactory.codeCellContentFactory).toBe(
  455. factory.codeCellContentFactory
  456. );
  457. });
  458. });
  459. });
  460. describe('.defaultContentFactory', () => {
  461. it('should be a ContentFactory', () => {
  462. expect(NotebookModel.defaultContentFactory).toBeInstanceOf(
  463. NotebookModel.ContentFactory
  464. );
  465. });
  466. });
  467. });
  468. });