/
savehandler.spec.ts
215 lines (184 loc) · 6.12 KB
/
savehandler.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { ServiceManager } from '@jupyterlab/services';
import {
Context,
DocumentRegistry,
TextModelFactory
} from '@jupyterlab/docregistry';
import { SaveHandler } from '../src';
import { PromiseDelegate, UUID } from '@lumino/coreutils';
import {
acceptDialog,
signalToPromise,
testEmission,
waitForDialog
} from '@jupyterlab/testutils';
import * as Mock from '@jupyterlab/testutils/lib/mock';
describe('docregistry/savehandler', () => {
let manager: ServiceManager.IManager;
const factory = new TextModelFactory();
let context: Context<DocumentRegistry.IModel>;
let handler: SaveHandler;
beforeAll(() => {
manager = new Mock.ServiceManagerMock();
});
beforeEach(() => {
context = new Context({
manager,
factory,
path: UUID.uuid4() + '.txt'
});
handler = new SaveHandler({ context });
return context.initialize(true);
});
afterEach(() => {
context.dispose();
handler.dispose();
});
describe('SaveHandler', () => {
describe('#constructor()', () => {
it('should create a new save handler', () => {
expect(handler).toBeInstanceOf(SaveHandler);
});
});
describe('#saveInterval()', () => {
it('should be the save interval of the handler', () => {
expect(handler.saveInterval).toBe(120);
});
it('should be set-able', () => {
handler.saveInterval = 200;
expect(handler.saveInterval).toBe(200);
});
});
describe('#isActive', () => {
it('should test whether the handler is active', () => {
expect(handler.isActive).toBe(false);
handler.start();
expect(handler.isActive).toBe(true);
});
});
describe('#isDisposed', () => {
it('should test whether the handler is disposed', () => {
expect(handler.isDisposed).toBe(false);
handler.dispose();
expect(handler.isDisposed).toBe(true);
});
it('should be true after the context is disposed', () => {
context.dispose();
expect(handler.isDisposed).toBe(true);
});
});
describe('#dispose()', () => {
it('should dispose of the resources used by the handler', () => {
expect(handler.isDisposed).toBe(false);
handler.dispose();
expect(handler.isDisposed).toBe(true);
handler.dispose();
expect(handler.isDisposed).toBe(true);
});
});
describe('#start()', () => {
it('should start the save handler', () => {
handler.start();
expect(handler.isActive).toBe(true);
});
it('should trigger a save', () => {
const promise = signalToPromise(context.fileChanged);
context.model.fromString('bar');
expect(handler.isActive).toBe(false);
handler.saveInterval = 0.1;
handler.start();
return promise;
});
it('should continue to save', async () => {
let called = 0;
// Lower the duration multiplier.
(handler as any)._multiplier = 1;
const promise = testEmission(context.fileChanged, {
test: () => {
if (called === 0) {
context.model.fromString('bar');
called++;
}
return called === 1;
}
});
context.model.fromString('foo');
expect(handler.isActive).toBe(false);
handler.saveInterval = 0.1;
handler.start();
return promise;
});
it('should overwrite the file on disk', async () => {
const delegate = new PromiseDelegate();
// Lower the duration multiplier.
(handler as any)._multiplier = 1;
context.model.fromString('foo');
await context.initialize(true);
// The context allows up to 0.5 difference in timestamps before complaining.
setTimeout(async () => {
await manager.contents.save(context.path, {
type: factory.contentType,
format: factory.fileFormat,
content: 'bar'
});
handler.saveInterval = 1;
handler.start();
context.model.fromString('baz');
context.fileChanged.connect(() => {
expect(context.model.toString()).toBe('baz');
delegate.resolve(undefined);
});
}, 1500);
// Extend the timeout to wait for the dialog because of the setTimeout.
await acceptDialog(document.body, 3000);
await delegate.promise;
});
it('should revert to the file on disk', async () => {
const delegate = new PromiseDelegate();
const revert = () => {
const dialog = document.body.getElementsByClassName('jp-Dialog')[0];
const buttons = dialog.getElementsByTagName('button');
for (let i = 0; i < buttons.length; i++) {
if (buttons[i].textContent === 'Revert') {
buttons[i].click();
return;
}
}
};
// Lower the duration multiplier.
(handler as any)._multiplier = 1;
await context.initialize(true);
context.model.fromString('foo');
context.fileChanged.connect(() => {
expect(context.model.toString()).toBe('bar');
delegate.resolve(undefined);
});
// The context allows up to 0.5 difference in timestamps before complaining.
setTimeout(async () => {
await manager.contents.save(context.path, {
type: factory.contentType,
format: factory.fileFormat,
content: 'bar'
});
handler.saveInterval = 1;
handler.start();
context.model.fromString('baz');
}, 1500);
// Extend the timeout to wait for the dialog because of the setTimeout.
await waitForDialog(document.body, 3000);
revert();
await delegate.promise;
});
});
describe('#stop()', () => {
it('should stop the save timer', () => {
handler.start();
expect(handler.isActive).toBe(true);
handler.stop();
expect(handler.isActive).toBe(false);
});
});
});
});