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