forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.spec.ts
431 lines (386 loc) · 13.3 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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { expect } from 'chai';
import { toArray } from '@phosphor/algorithm';
import { IChangedArgs, nbformat } from '@jupyterlab/coreutils';
import {
CellModel,
RawCellModel,
MarkdownCellModel,
CodeCellModel
} from '@jupyterlab/cells';
import { OutputAreaModel } from '@jupyterlab/outputarea';
import { NBTestUtils } from '@jupyterlab/testutils';
class TestModel extends CellModel {
get type(): 'raw' {
return 'raw';
}
}
describe('cells/model', () => {
describe('CellModel', () => {
describe('#constructor()', () => {
it('should create a cell model', () => {
const model = new CellModel({});
expect(model).to.be.an.instanceof(CellModel);
});
it('should accept a base cell argument', () => {
const cell: nbformat.IRawCell = {
cell_type: 'raw',
source: 'foo',
metadata: { trusted: false }
};
const model = new CellModel({ cell });
expect(model).to.be.an.instanceof(CellModel);
expect(model.value.text).to.equal(cell.source);
});
it('should accept a base cell argument with a multiline source', () => {
const cell: nbformat.IRawCell = {
cell_type: 'raw',
source: ['foo\n', 'bar\n', 'baz'],
metadata: { trusted: false }
};
const model = new CellModel({ cell });
expect(model).to.be.an.instanceof(CellModel);
expect(model.value.text).to.equal((cell.source as string[]).join(''));
});
});
describe('#contentChanged', () => {
it('should signal when model content has changed', () => {
const model = new CellModel({});
let called = false;
model.contentChanged.connect(() => {
called = true;
});
expect(called).to.equal(false);
model.value.text = 'foo';
expect(called).to.equal(true);
});
});
describe('#stateChanged', () => {
it('should signal when model state has changed', () => {
const model = new CodeCellModel({});
let called = false;
const listener = (sender: any, args: IChangedArgs<any>) => {
expect(args.newValue).to.equal(1);
called = true;
};
model.stateChanged.connect(listener);
model.executionCount = 1;
expect(called).to.equal(true);
});
it('should not signal when model state has not changed', () => {
const model = new CodeCellModel({});
let called = 0;
model.stateChanged.connect(() => {
called++;
});
expect(called).to.equal(0);
model.executionCount = 1;
expect(called).to.equal(1);
model.executionCount = 1;
expect(called).to.equal(1);
});
});
describe('#trusted', () => {
it('should be the trusted state of the cell', () => {
const model = new CodeCellModel({});
expect(model.trusted).to.equal(false);
model.trusted = true;
expect(model.trusted).to.equal(true);
const other = new CodeCellModel({ cell: model.toJSON() });
expect(other.trusted).to.equal(true);
});
it('should update the trusted state of the output models', () => {
const model = new CodeCellModel({});
model.outputs.add(NBTestUtils.DEFAULT_OUTPUTS[0]);
expect(model.outputs.get(0).trusted).to.equal(false);
model.trusted = true;
expect(model.outputs.get(0).trusted).to.equal(true);
});
});
describe('#metadataChanged', () => {
it('should signal when model metadata has changed', () => {
const model = new TestModel({});
const listener = (sender: any, args: any) => {
value = args.newValue;
};
let value = '';
model.metadata.changed.connect(listener);
expect(value).to.be.empty;
model.metadata.set('foo', 'bar');
expect(value).to.equal('bar');
});
it('should not signal when model metadata has not changed', () => {
const model = new TestModel({});
let called = 0;
model.metadata.changed.connect(() => {
called++;
});
expect(called).to.equal(0);
model.metadata.set('foo', 'bar');
expect(called).to.equal(1);
model.metadata.set('foo', 'bar');
expect(called).to.equal(1);
});
});
describe('#source', () => {
it('should default to an empty string', () => {
const model = new CellModel({});
expect(model.value.text).to.be.empty;
});
it('should be settable', () => {
const model = new CellModel({});
expect(model.value.text).to.be.empty;
model.value.text = 'foo';
expect(model.value.text).to.equal('foo');
});
});
describe('#isDisposed', () => {
it('should be false by default', () => {
const model = new CellModel({});
expect(model.isDisposed).to.equal(false);
});
it('should be true after model is disposed', () => {
const model = new CellModel({});
model.dispose();
expect(model.isDisposed).to.equal(true);
});
});
describe('#dispose()', () => {
it('should dispose of the resources held by the model', () => {
const model = new TestModel({});
model.dispose();
expect(model.isDisposed).to.equal(true);
});
it('should be safe to call multiple times', () => {
const model = new CellModel({});
model.dispose();
model.dispose();
expect(model.isDisposed).to.equal(true);
});
});
describe('#toJSON()', () => {
it('should return a base cell encapsulation of the model value', () => {
const cell: nbformat.IRawCell = {
cell_type: 'raw',
source: 'foo',
metadata: { trusted: false }
};
const model = new TestModel({ cell });
expect(model.toJSON()).to.not.equal(cell);
expect(model.toJSON()).to.deep.equal(cell);
});
it('should always return a string source', () => {
const cell: nbformat.IRawCell = {
cell_type: 'raw',
source: ['foo\n', 'bar\n', 'baz'],
metadata: { trusted: false }
};
const model = new TestModel({ cell });
cell.source = (cell.source as string[]).join('');
expect(model.toJSON()).to.not.equal(cell);
expect(model.toJSON()).to.deep.equal(cell);
});
});
describe('#metadata', () => {
it('should handle a metadata for the cell', () => {
const model = new CellModel({});
expect(model.metadata.get('foo')).to.be.undefined;
model.metadata.set('foo', 1);
expect(model.metadata.get('foo')).to.equal(1);
});
it('should get a list of user metadata keys', () => {
const model = new CellModel({});
expect(toArray(model.metadata.keys())).to.be.empty;
model.metadata.set('foo', 1);
expect(model.metadata.keys()).to.deep.equal(['foo']);
});
it('should trigger changed signal', () => {
const model = new CellModel({});
let called = false;
model.metadata.changed.connect(() => {
called = true;
});
model.metadata.set('foo', 1);
expect(called).to.equal(true);
});
});
});
describe('RawCellModel', () => {
describe('#type', () => {
it('should be set with type "raw"', () => {
const model = new RawCellModel({});
expect(model.type).to.equal('raw');
});
});
});
describe('MarkdownCellModel', () => {
describe('#type', () => {
it('should be set with type "markdown"', () => {
const model = new MarkdownCellModel({});
expect(model.type).to.equal('markdown');
});
});
});
describe('CodeCellModel', () => {
describe('#constructor()', () => {
it('should create a code cell model', () => {
const model = new CodeCellModel({});
expect(model).to.be.an.instanceof(CodeCellModel);
});
it('should accept a code cell argument', () => {
const cell: nbformat.ICodeCell = {
cell_type: 'code',
execution_count: 1,
outputs: [
{
output_type: 'display_data',
data: { 'text/plain': 'foo' },
metadata: {}
} as nbformat.IDisplayData
],
source: 'foo',
metadata: { trusted: false }
};
const model = new CodeCellModel({ cell });
expect(model).to.be.an.instanceof(CodeCellModel);
expect(model.value.text).to.equal(cell.source);
});
it('should connect the outputs changes to content change signal', () => {
const data = {
output_type: 'display_data',
data: { 'text/plain': 'foo' },
metadata: {}
} as nbformat.IDisplayData;
const model = new CodeCellModel({});
let called = false;
model.contentChanged.connect(() => {
called = true;
});
expect(called).to.equal(false);
model.outputs.add(data);
expect(called).to.equal(true);
});
});
describe('#type', () => {
it('should be set with type "code"', () => {
const model = new CodeCellModel({});
expect(model.type).to.equal('code');
});
});
describe('#executionCount', () => {
it('should show the execution count of the cell', () => {
const cell: nbformat.ICodeCell = {
cell_type: 'code',
execution_count: 1,
outputs: [],
source: 'foo',
metadata: { trusted: false }
};
const model = new CodeCellModel({ cell });
expect(model.executionCount).to.equal(1);
});
it('should be settable', () => {
const model = new CodeCellModel({});
expect(model.executionCount).to.be.null;
model.executionCount = 1;
expect(model.executionCount).to.equal(1);
});
it('should emit a state change signal when set', () => {
const model = new CodeCellModel({});
let called = false;
model.stateChanged.connect(() => {
called = true;
});
expect(model.executionCount).to.be.null;
expect(called).to.equal(false);
model.executionCount = 1;
expect(model.executionCount).to.equal(1);
expect(called).to.equal(true);
});
it('should not signal when state has not changed', () => {
const model = new CodeCellModel({});
let called = 0;
model.stateChanged.connect(() => {
called++;
});
expect(model.executionCount).to.be.null;
expect(called).to.equal(0);
model.executionCount = 1;
expect(model.executionCount).to.equal(1);
model.executionCount = 1;
expect(called).to.equal(1);
});
});
describe('#outputs', () => {
it('should be an output area model', () => {
const model = new CodeCellModel({});
expect(model.outputs).to.be.an.instanceof(OutputAreaModel);
});
});
describe('#dispose()', () => {
it('should dispose of the resources held by the model', () => {
const model = new CodeCellModel({});
expect(model.outputs).to.be.an.instanceof(OutputAreaModel);
model.dispose();
expect(model.isDisposed).to.equal(true);
expect(model.outputs).to.be.null;
});
it('should be safe to call multiple times', () => {
const model = new CodeCellModel({});
model.dispose();
model.dispose();
expect(model.isDisposed).to.equal(true);
});
});
describe('#toJSON()', () => {
it('should return a code cell encapsulation of the model value', () => {
const cell: nbformat.ICodeCell = {
cell_type: 'code',
execution_count: 1,
outputs: [
{
output_type: 'display_data',
data: {
'text/plain': 'foo',
'application/json': { bar: 1 }
},
metadata: {}
} as nbformat.IDisplayData
],
source: 'foo',
metadata: { trusted: false }
};
const model = new CodeCellModel({ cell });
const serialized = model.toJSON();
expect(serialized).to.not.equal(cell);
expect(serialized).to.deep.equal(cell);
const output = serialized.outputs[0] as any;
expect(output.data['application/json']['bar']).to.equal(1);
});
});
describe('.ContentFactory', () => {
describe('#constructor()', () => {
it('should create a new output area factory', () => {
const factory = new CodeCellModel.ContentFactory();
expect(factory).to.be.an.instanceof(CodeCellModel.ContentFactory);
});
});
describe('#createOutputArea()', () => {
it('should create an output area model', () => {
const factory = new CodeCellModel.ContentFactory();
expect(
factory.createOutputArea({ trusted: true })
).to.be.an.instanceof(OutputAreaModel);
});
});
});
describe('.defaultContentFactory', () => {
it('should be an ContentFactory', () => {
expect(CodeCellModel.defaultContentFactory).to.be.an.instanceof(
CodeCellModel.ContentFactory
);
});
});
});
});