model.spec.ts 18 KB

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