model.spec.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import expect = require('expect.js');
  4. import {
  5. ArrayExt, toArray
  6. } from '@phosphor/algorithm';
  7. import {
  8. CodeCellModel
  9. } from '@jupyterlab/cells';
  10. import {
  11. nbformat
  12. } from '@jupyterlab/coreutils';
  13. import {
  14. NotebookModel
  15. } from '@jupyterlab/notebook';
  16. import {
  17. ModelDB
  18. } from '@jupyterlab/observables';
  19. import {
  20. DEFAULT_CONTENT
  21. } from '../../notebook-utils';
  22. import {
  23. moment
  24. } from '../../utils';
  25. describe('@jupyterlab/notebook', () => {
  26. describe('NotebookModel', () => {
  27. describe('#constructor()', () => {
  28. it('should create a notebook model', () => {
  29. let model = new NotebookModel();
  30. expect(model).to.be.a(NotebookModel);
  31. });
  32. it('should accept an optional language preference', () => {
  33. let model = new NotebookModel({ languagePreference: 'python' });
  34. let lang = model.metadata.get('language_info') as nbformat.ILanguageInfoMetadata;
  35. expect(lang.name).to.be('python');
  36. });
  37. it('should add a single code cell by default', () => {
  38. let model = new NotebookModel();
  39. expect(model.cells.length).to.be(1);
  40. expect(model.cells.get(0)).to.be.a(CodeCellModel);
  41. });
  42. it('should accept an optional factory', () => {
  43. let contentFactory = new NotebookModel.ContentFactory({});
  44. let model = new NotebookModel({ contentFactory });
  45. expect(model.contentFactory.codeCellContentFactory)
  46. .to.be(contentFactory.codeCellContentFactory);
  47. });
  48. });
  49. describe('#metadataChanged', () => {
  50. it('should be emitted when a metadata field changes', () => {
  51. let model = new NotebookModel();
  52. let called = false;
  53. model.metadata.changed.connect((sender, args) => {
  54. expect(sender).to.be(model.metadata);
  55. expect(args.key).to.be('foo');
  56. expect(args.oldValue).to.be(void 0);
  57. expect(args.newValue).to.be(1);
  58. called = true;
  59. });
  60. model.metadata.set('foo', 1);
  61. expect(called).to.be(true);
  62. });
  63. it('should not be emitted when the value does not change', () => {
  64. let model = new NotebookModel();
  65. let called = false;
  66. model.metadata.set('foo', 1);
  67. model.metadata.changed.connect(() => { called = true; });
  68. model.metadata.set('foo', 1);
  69. expect(called).to.be(false);
  70. });
  71. });
  72. describe('#cells', () => {
  73. it('should add an empty code cell by default', () => {
  74. let model = new NotebookModel();
  75. expect(model.cells.length).to.be(1);
  76. expect(model.cells.get(0)).to.be.a(CodeCellModel);
  77. });
  78. it('should be reset when loading from disk', () => {
  79. let model = new NotebookModel();
  80. let cell = model.contentFactory.createCodeCell({});
  81. model.cells.push(cell);
  82. model.fromJSON(DEFAULT_CONTENT);
  83. expect(ArrayExt.firstIndexOf(toArray(model.cells), cell)).to.be(-1);
  84. expect(model.cells.length).to.be(6);
  85. });
  86. it('should allow undoing a change', () => {
  87. let model = new NotebookModel();
  88. let cell = model.contentFactory.createCodeCell({});
  89. cell.value.text = 'foo';
  90. model.cells.push(cell);
  91. model.fromJSON(DEFAULT_CONTENT);
  92. model.cells.undo();
  93. expect(model.cells.length).to.be(2);
  94. expect(model.cells.get(1).value.text).to.be('foo');
  95. expect(model.cells.get(1)).to.be(cell); // should be ===.
  96. });
  97. context('cells `changed` signal', () => {
  98. it('should emit a `contentChanged` signal', () => {
  99. let model = new NotebookModel();
  100. let cell = model.contentFactory.createCodeCell({});
  101. let called = false;
  102. model.contentChanged.connect(() => { called = true; });
  103. model.cells.push(cell);
  104. expect(called).to.be(true);
  105. });
  106. it('should set the dirty flag', () => {
  107. let model = new NotebookModel();
  108. let cell = model.contentFactory.createCodeCell({});
  109. model.cells.push(cell);
  110. expect(model.dirty).to.be(true);
  111. });
  112. it('should add a new code cell when cells are cleared', async () => {
  113. let model = new NotebookModel();
  114. model.cells.clear();
  115. await moment();
  116. expect(model.cells.length).to.be(1);
  117. expect(model.cells.get(0)).to.be.a(CodeCellModel);
  118. });
  119. });
  120. describe('cell `changed` signal', () => {
  121. it('should be called when a cell content changes', () => {
  122. let model = new NotebookModel();
  123. let cell = model.contentFactory.createCodeCell({});
  124. model.cells.push(cell);
  125. cell.value.text = 'foo';
  126. });
  127. it('should emit the `contentChanged` signal', () => {
  128. let model = new NotebookModel();
  129. let cell = model.contentFactory.createCodeCell({});
  130. model.cells.push(cell);
  131. let called = false;
  132. model.contentChanged.connect(() => { called = true; });
  133. model.metadata.set('foo', 'bar');
  134. expect(called).to.be(true);
  135. });
  136. it('should set the dirty flag', () => {
  137. let model = new NotebookModel();
  138. let cell = model.contentFactory.createCodeCell({});
  139. model.cells.push(cell);
  140. model.dirty = false;
  141. cell.value.text = 'foo';
  142. expect(model.dirty).to.be(true);
  143. });
  144. });
  145. });
  146. describe('#contentFactory', () => {
  147. it('should be the cell model factory used by the model', () => {
  148. let model = new NotebookModel();
  149. expect(model.contentFactory.codeCellContentFactory)
  150. .to.be(NotebookModel.defaultContentFactory.codeCellContentFactory);
  151. });
  152. });
  153. describe('#nbformat', () => {
  154. it('should get the major version number of the nbformat', () => {
  155. let model = new NotebookModel();
  156. model.fromJSON(DEFAULT_CONTENT);
  157. expect(model.nbformat).to.be(DEFAULT_CONTENT.nbformat);
  158. });
  159. });
  160. describe('#nbformatMinor', () => {
  161. it('should get the minor version number of the nbformat', () => {
  162. let model = new NotebookModel();
  163. model.fromJSON(DEFAULT_CONTENT);
  164. expect(model.nbformatMinor).to.be(nbformat.MINOR_VERSION);
  165. });
  166. });
  167. describe('#defaultKernelName()', () => {
  168. it('should get the default kernel name of the document', () => {
  169. let model = new NotebookModel();
  170. model.metadata.set('kernelspec', { name: 'python3' });
  171. expect(model.defaultKernelName).to.be('python3');
  172. });
  173. it('should default to an empty string', () => {
  174. let model = new NotebookModel();
  175. expect(model.defaultKernelName).to.be('');
  176. });
  177. });
  178. describe('#defaultKernelLanguage', () => {
  179. it('should get the default kernel language of the document', () => {
  180. let model = new NotebookModel();
  181. model.metadata.set('language_info', { name: 'python' });
  182. expect(model.defaultKernelLanguage).to.be('python');
  183. });
  184. it('should default to an empty string', () => {
  185. let model = new NotebookModel();
  186. expect(model.defaultKernelLanguage).to.be('');
  187. });
  188. it('should be set from the constructor arg', () => {
  189. let model = new NotebookModel({ languagePreference: 'foo' });
  190. expect(model.defaultKernelLanguage).to.be('foo');
  191. });
  192. });
  193. describe('#dispose()', () => {
  194. it('should dispose of the resources held by the model', () => {
  195. let model = new NotebookModel();
  196. model.fromJSON(DEFAULT_CONTENT);
  197. model.dispose();
  198. expect(model.cells).to.be(null);
  199. expect(model.isDisposed).to.be(true);
  200. });
  201. it('should be safe to call multiple times', () => {
  202. let model = new NotebookModel();
  203. model.dispose();
  204. model.dispose();
  205. expect(model.isDisposed).to.be(true);
  206. });
  207. });
  208. describe('#toString()', () => {
  209. it('should serialize the model to a string', () => {
  210. let model = new NotebookModel();
  211. model.fromJSON(DEFAULT_CONTENT);
  212. let text = model.toString();
  213. let data = JSON.parse(text);
  214. expect(data.cells.length).to.be(6);
  215. });
  216. });
  217. describe('#fromString()', () => {
  218. it('should deserialize the model from a string', () => {
  219. let model = new NotebookModel();
  220. model.fromString(JSON.stringify(DEFAULT_CONTENT));
  221. expect(model.cells.length).to.be(6);
  222. });
  223. it('should set the dirty flag', () => {
  224. let model = new NotebookModel();
  225. model.dirty = false;
  226. model.fromString(JSON.stringify(DEFAULT_CONTENT));
  227. expect(model.dirty).to.be(true);
  228. });
  229. });
  230. describe('#toJSON()', () => {
  231. it('should serialize the model to JSON', () => {
  232. let model = new NotebookModel();
  233. model.fromJSON(DEFAULT_CONTENT);
  234. let data = model.toJSON();
  235. expect(data.cells.length).to.be(6);
  236. });
  237. });
  238. describe('#fromJSON()', () => {
  239. it('should serialize the model from JSON', () => {
  240. let model = new NotebookModel();
  241. model.fromJSON(DEFAULT_CONTENT);
  242. expect(model.cells.length).to.be(6);
  243. expect(model.nbformat).to.be(DEFAULT_CONTENT.nbformat);
  244. expect(model.nbformatMinor).to.be(nbformat.MINOR_VERSION);
  245. });
  246. it('should set the dirty flag', () => {
  247. let model = new NotebookModel();
  248. model.dirty = false;
  249. model.fromJSON(DEFAULT_CONTENT);
  250. expect(model.dirty).to.be(true);
  251. });
  252. });
  253. describe('#metadata', () => {
  254. it('should have default values', () => {
  255. let model = new NotebookModel();
  256. let metadata = model.metadata;
  257. expect(metadata.has('kernelspec'));
  258. expect(metadata.has('language_info'));
  259. expect(metadata.size).to.be(2);
  260. });
  261. it('should set the dirty flag when changed', () => {
  262. let model = new NotebookModel();
  263. expect(model.dirty).to.be(false);
  264. model.metadata.set('foo', 'bar');
  265. expect(model.dirty).to.be(true);
  266. });
  267. it('should emit the `contentChanged` signal', () => {
  268. let model = new NotebookModel();
  269. let called = false;
  270. model.contentChanged.connect(() => { called = true; });
  271. model.metadata.set('foo', 'bar');
  272. expect(called).to.be(true);
  273. });
  274. it('should emit the `metadataChanged` signal', () => {
  275. let model = new NotebookModel();
  276. let called = false;
  277. model.metadata.changed.connect((sender, args) => {
  278. expect(sender).to.be(model.metadata);
  279. expect(args.key).to.be('foo');
  280. expect(args.oldValue).to.be(void 0);
  281. expect(args.newValue).to.be('bar');
  282. called = true;
  283. });
  284. model.metadata.set('foo', 'bar');
  285. expect(called).to.be(true);
  286. });
  287. });
  288. describe('.ContentFactory', () => {
  289. let factory = new NotebookModel.ContentFactory({});
  290. context('#codeCellContentFactory', () => {
  291. it('should be a code cell content factory', () => {
  292. expect(factory.codeCellContentFactory).to.be(CodeCellModel.defaultContentFactory);
  293. });
  294. it('should be settable in the constructor', () => {
  295. let codeCellContentFactory = new CodeCellModel.ContentFactory();
  296. factory = new NotebookModel.ContentFactory({ codeCellContentFactory });
  297. expect(factory.codeCellContentFactory).to.be(codeCellContentFactory);
  298. });
  299. });
  300. context('#createCodeCell()', () => {
  301. it('should create a new code cell', () => {
  302. let cell = factory.createCodeCell({});
  303. expect(cell.type).to.be('code');
  304. });
  305. it('should clone an existing code cell', () => {
  306. let orig = factory.createCodeCell({});
  307. orig.value.text = 'foo';
  308. let cell = orig.toJSON();
  309. let newCell = factory.createCodeCell({ cell });
  310. expect(newCell.value.text).to.be('foo');
  311. });
  312. it('should clone an existing raw cell', () => {
  313. let orig = factory.createRawCell({});
  314. orig.value.text = 'foo';
  315. let cell = orig.toJSON();
  316. let newCell = factory.createCodeCell({ cell });
  317. expect(newCell.value.text).to.be('foo');
  318. });
  319. });
  320. context('#createRawCell()', () => {
  321. it('should create a new raw cell', () => {
  322. let cell = factory.createRawCell({});
  323. expect(cell.type).to.be('raw');
  324. });
  325. it('should clone an existing raw cell', () => {
  326. let orig = factory.createRawCell({});
  327. orig.value.text = 'foo';
  328. let cell = orig.toJSON();
  329. let newCell = factory.createRawCell({ cell });
  330. expect(newCell.value.text).to.be('foo');
  331. });
  332. it('should clone an existing code cell', () => {
  333. let orig = factory.createCodeCell({});
  334. orig.value.text = 'foo';
  335. let cell = orig.toJSON();
  336. let newCell = factory.createRawCell({ cell });
  337. expect(newCell.value.text).to.be('foo');
  338. });
  339. });
  340. describe('#createMarkdownCell()', () => {
  341. it('should create a new markdown cell', () => {
  342. let cell = factory.createMarkdownCell({});
  343. expect(cell.type).to.be('markdown');
  344. });
  345. it('should clone an existing markdown cell', () => {
  346. let orig = factory.createMarkdownCell({});
  347. orig.value.text = 'foo';
  348. let cell = orig.toJSON();
  349. let newCell = factory.createMarkdownCell({ cell });
  350. expect(newCell.value.text).to.be('foo');
  351. });
  352. it('should clone an existing raw cell', () => {
  353. let orig = factory.createRawCell({});
  354. orig.value.text = 'foo';
  355. let cell = orig.toJSON();
  356. let newCell = factory.createMarkdownCell({ cell });
  357. expect(newCell.value.text).to.be('foo');
  358. });
  359. });
  360. describe('#modelDB', () => {
  361. it('should be undefined by default', () => {
  362. expect(factory.modelDB).to.be(undefined);
  363. });
  364. });
  365. describe('#clone()', () => {
  366. it('should create a new content factory with a new IModelDB', () => {
  367. let modelDB = new ModelDB();
  368. let factory = new NotebookModel.ContentFactory({ modelDB });
  369. expect(factory.modelDB).to.be(modelDB);
  370. let newModelDB = new ModelDB();
  371. let newFactory = factory.clone(newModelDB);
  372. expect(newFactory.modelDB).to.be(newModelDB);
  373. expect(newFactory.codeCellContentFactory)
  374. .to.be(factory.codeCellContentFactory);
  375. });
  376. });
  377. });
  378. describe('.defaultContentFactory', () => {
  379. it('should be a ContentFactory', () => {
  380. expect(NotebookModel.defaultContentFactory).to.be.a(NotebookModel.ContentFactory);
  381. });
  382. });
  383. });
  384. });