model.spec.ts 18 KB

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