forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.spec.ts
442 lines (342 loc) · 11.2 KB
/
context.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import expect = require('expect.js');
import {
Contents, ServiceManager
} from '@jupyterlab/services';
import {
Widget
} from '@phosphor/widgets';
import {
Context, DocumentRegistry, TextModelFactory
} from '../../../lib/docregistry';
import {
waitForDialog, acceptDialog
} from '../utils';
describe('docregistry/context', () => {
let manager: ServiceManager.IManager;
let factory = new TextModelFactory();
before((done) => {
manager = new ServiceManager();
manager.ready.then(done, done);
});
describe('Context', () => {
let context: Context<DocumentRegistry.IModel>;
beforeEach(() => {
context = new Context({ manager, factory, path: 'foo' });
});
afterEach((done) => {
if (context.kernel) {
context.kernel.ready.then(() => {
return context.changeKernel(null);
}).then(() => {
context.dispose();
}).then(done, done);
} else {
context.dispose();
done();
}
});
describe('#constructor()', () => {
it('should create a new context', () => {
context = new Context({ manager, factory, path: 'bar' });
expect(context).to.be.a(Context);
});
});
describe('#kernelChanged', () => {
it('should be emitted when the kernel changes', (done) => {
let name = manager.specs.default;
context.kernelChanged.connect((sender, args) => {
expect(sender).to.be(context);
expect(args.name).to.be(name);
done();
});
context.changeKernel({ name });
});
});
describe('#pathChanged', () => {
it('should be emitted when the path changes', (done) => {
context.pathChanged.connect((sender, args) => {
expect(sender).to.be(context);
expect(args).to.be('foo');
done();
});
context.save().then(() => {
return manager.contents.rename(context.path, 'foo');
}).catch(done);
});
});
describe('#fileChanged', () => {
it('should be emitted when the file is saved', (done) => {
context.fileChanged.connect((sender, args) => {
expect(sender).to.be(context);
expect(args.name).to.be('foo');
done();
});
context.save();
});
});
describe('#isReady', () => {
it('should indicate whether the context is ready', (done) => {
expect(context.isReady).to.be(false);
context.ready.then(() => {
expect(context.isReady).to.be(true);
done();
}).catch(done);
context.save().catch(done);
});
});
describe('#ready()', () => {
it('should resolve when the file is saved for the first time', (done) => {
context.ready.then(done, done);
context.save().catch(done);
});
it('should resolve when the file is reverted for the first time', (done) => {
manager.contents.save(context.path, {
type: factory.contentType,
format: factory.fileFormat,
content: 'foo'
});
context.ready.then(done, done);
context.revert().catch(done);
});
});
describe('#disposed', () => {
it('should be emitted when the context is disposed', (done) => {
context.disposed.connect((sender, args) => {
expect(sender).to.be(context);
expect(args).to.be(void 0);
done();
});
context.dispose();
});
});
describe('#model', () => {
it('should be the model associated with the document', () => {
expect(context.model.toString()).to.be('');
});
});
describe('#kernel', () => {
it('should default to `null`', () => {
expect(context.kernel).to.be(null);
});
it('should be set after switching kernels', (done) => {
let name = manager.specs.default;
context.changeKernel({ name }).then(() => {
expect(context.kernel.name).to.be(name);
done();
}).catch(done);
});
});
describe('#path', () => {
it('should be the current path for the context', () => {
expect(context.path).to.be('foo');
});
});
describe('#contentsModel', () => {
it('should be `null` before poulation', () => {
expect(context.contentsModel).to.be(null);
});
it('should be set after poulation', (done) => {
context.ready.then(() => {
expect(context.contentsModel.name).to.be('foo');
done();
});
context.save().catch(done);
});
});
describe('#factoryName', () => {
it('should be the name of the factory used by the context', () => {
expect(context.factoryName).to.be(factory.name);
});
});
describe('#isDisposed', () => {
it('should test whether the context is disposed', () => {
expect(context.isDisposed).to.be(false);
context.dispose();
expect(context.isDisposed).to.be(true);
});
});
describe('#dispose()', () => {
it('should dispose of the resources used by the context', () => {
context.dispose();
expect(context.isDisposed).to.be(true);
context.dispose();
expect(context.isDisposed).to.be(true);
});
});
describe('#startDefaultKernel()', () => {
it('should start the default kernel for the context', (done) => {
context.save().then(() => {
return context.startDefaultKernel();
}).then(kernel => {
expect(kernel.name).to.be.ok();
done();
}).catch(done);
});
});
describe('#changeKernel()', () => {
it('should change the kernel instance', (done) => {
let name = manager.specs.default;
context.changeKernel({ name }).then(() => {
expect(context.kernel.name).to.be(name);
}).then(done, done);
});
it('should shut down the session if given `null`', (done) => {
let name = manager.specs.default;
context.changeKernel({ name }).then(() => {
expect(context.kernel.name).to.be(name);
return context.kernel.ready;
}).then(() => {
return context.changeKernel(null);
}).then(() => {
expect(context.kernel).to.be(null);
done();
}).catch(done);
});
});
describe('#save()', () => {
it('should save the contents of the file to disk', (done) => {
context.model.fromString('foo');
context.save().then(() => {
let opts: Contents.IFetchOptions = {
format: factory.fileFormat,
type: factory.contentType,
content: true
};
return manager.contents.get(context.path, opts);
}).then(model => {
expect(model.content).to.be('foo');
done();
}).catch(done);
});
});
describe('#saveAs()', () => {
it('should save the document to a different path chosen by the user', (done) => {
waitForDialog().then(() => {
let dialog = document.body.getElementsByClassName('jp-Dialog')[0];
let input = dialog.getElementsByTagName('input')[0];
input.value = 'bar';
acceptDialog();
});
context.saveAs().then(() => {
expect(context.path).to.be('bar');
done();
}).catch(done);
});
});
describe('#revert()', () => {
it('should revert the contents of the file to the disk', (done) => {
manager.contents.save(context.path, {
type: factory.contentType,
format: factory.fileFormat,
content: 'foo'
}).then(() => {
context.model.fromString('bar');
return context.revert();
}).then(() => {
expect(context.model.toString()).to.be('foo');
done();
}).catch(done);
});
});
describe('#createCheckpoint()', () => {
it('should create a checkpoint for the file', (done) => {
context.createCheckpoint().then(model => {
expect(model.id).to.be.ok();
expect(model.last_modified).to.be.ok();
done();
}).catch(done);
});
});
describe('#deleteCheckpoint()', () => {
it('should delete the given checkpoint', (done) => {
context.createCheckpoint().then(model => {
return context.deleteCheckpoint(model.id);
}).then(() => {
return context.listCheckpoints();
}).then(models => {
expect(models.length).to.be(0);
done();
}).catch(done);
});
});
describe('#restoreCheckpoint()', () => {
it('should restore the value to the last checkpoint value', (done) => {
context.model.fromString('bar');
let id = '';
context.save().then(() => {
return context.createCheckpoint();
}).then(model => {
context.model.fromString('foo');
id = model.id;
return context.save();
}).then(() => {
return context.restoreCheckpoint(id);
}).then(() => {
return context.revert();
}).then(() => {
expect(context.model.toString()).to.be('bar');
done();
}).catch(done);
});
});
describe('#listCheckpoints()', () => {
it('should list the checkpoints for the file', (done) => {
let id = '';
context.createCheckpoint().then(model => {
id = model.id;
return context.listCheckpoints();
}).then(models => {
for (let model of models) {
if (model.id === id) {
done();
return;
}
}
}).catch(done);
});
});
describe('#resolveUrl()', () => {
it('should resolve a relative url to a correct server path', (done) => {
context.resolveUrl('./foo').then(path => {
expect(path).to.be('foo');
}).then(done, done);
});
it('should ignore urls that have a protocol', (done) => {
context.resolveUrl('http://foo').then(path => {
expect(path).to.be('http://foo');
done();
}).catch(done);
});
});
describe('#getDownloadUrl()', () => {
it('should resolve an absolute server url to a download url', (done) => {
let contextPromise = context.getDownloadUrl('foo');
let contentsPromise = manager.contents.getDownloadUrl('foo');
Promise.all([contextPromise, contentsPromise])
.then(values => {
expect(values[0]).to.be(values[1]);
done();
}).catch(done);
});
it('should ignore urls that have a protocol', (done) => {
context.getDownloadUrl('http://foo').then(path => {
expect(path).to.be('http://foo');
done();
}).catch(done);
});
});
describe('#addSibling()', () => {
it('should add a sibling widget', () => {
let called = false;
let opener = (widget: Widget) => {
called = true;
};
context = new Context({ manager, factory, path: 'foo', opener });
context.addSibling(new Widget());
expect(called).to.be(true);
});
});
});
});