forked from vscode-icons/vscode-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettingsManager.test.ts
368 lines (306 loc) · 12.3 KB
/
settingsManager.test.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
/* eslint-disable prefer-arrow-callback */
/* eslint-disable no-unused-expressions */
import { expect } from 'chai';
import * as semver from 'semver';
import * as sinon from 'sinon';
import { ErrorHandler } from '../../src/common/errorHandler';
import * as fsAsync from '../../src/common/fsAsync';
import { constants } from '../../src/constants';
import {
ExtensionStatus,
ISettingsManager,
IState,
IVSCodeManager,
} from '../../src/models';
import { SettingsManager } from '../../src/settings/settingsManager';
import { Utils } from '../../src/utils';
import { VSCodeManager } from '../../src/vscode/vscodeManager';
describe('SettingsManager: tests', function () {
context('ensures that', function () {
let sandbox: sinon.SinonSandbox;
let vscodeManagerStub: sinon.SinonStubbedInstance<IVSCodeManager>;
let settingsManager: ISettingsManager;
let stateMock: IState;
let globalStateGetStub: sinon.SinonStub;
let globalStateUpdateStub: sinon.SinonStub;
let logErrorStub: sinon.SinonStub;
let parseJSONStub: sinon.SinonStub;
beforeEach(function () {
sandbox = sinon.createSandbox();
vscodeManagerStub = sandbox.createStubInstance<IVSCodeManager>(
VSCodeManager,
);
globalStateGetStub = sandbox.stub();
globalStateUpdateStub = sandbox.stub();
sandbox.stub(vscodeManagerStub, 'context').get(() => ({
globalState: {
get: globalStateGetStub,
update: globalStateUpdateStub,
},
}));
settingsManager = new SettingsManager(vscodeManagerStub);
logErrorStub = sandbox.stub(ErrorHandler, 'logError');
parseJSONStub = sandbox.stub(Utils, 'parseJSONSafe');
sandbox.stub(Utils, 'pathUnixJoin');
stateMock = {
version: '0.0.0',
status: ExtensionStatus.deactivated,
welcomeShown: false,
};
});
afterEach(function () {
settingsManager = null;
sandbox.restore();
});
it(`an Error gets thrown, when 'vscodeManager' is NOT instantiated`, function () {
expect(() => new SettingsManager(null))
.to.throw(ReferenceError)
.that.matches(/'vscodeManager' not set to an instance/);
});
context('moving the state from its legacy place', function () {
let existsAsyncStub: sinon.SinonStub;
let readFileAsyncStub: sinon.SinonStub;
let unlinkFileAsyncStub: sinon.SinonStub;
let semverSpy: sinon.SinonSpy;
beforeEach(function () {
existsAsyncStub = sandbox.stub(fsAsync, 'existsAsync');
readFileAsyncStub = sandbox.stub(fsAsync, 'readFileAsync');
unlinkFileAsyncStub = sandbox.stub(fsAsync, 'unlinkAsync');
vscodeManagerStub.getAppUserDirPath.returns('');
});
it('when the file is NOT found, no moving happens', async function () {
existsAsyncStub.resolves(false);
globalStateUpdateStub.resolves();
await settingsManager.moveStateFromLegacyPlace();
expect(existsAsyncStub.calledOnce).to.be.true;
expect(readFileAsyncStub.called).to.be.false;
expect(globalStateUpdateStub.called).to.be.false;
expect(unlinkFileAsyncStub.called).to.be.false;
});
context(`when the file is found`, function () {
beforeEach(function () {
semverSpy = sandbox.spy(semver, 'eq');
});
context(`it gets not moved`, function () {
it(`if state version equals the default state version`, async function () {
existsAsyncStub.resolves(true);
readFileAsyncStub.resolves(JSON.stringify(stateMock));
globalStateUpdateStub.resolves();
await settingsManager.moveStateFromLegacyPlace();
expect(existsAsyncStub.calledOnce).to.be.true;
expect(readFileAsyncStub.calledOnce).to.be.true;
expect(
semverSpy.calledOnceWithExactly('0.0.0', '0.0.0'),
).to.be.true;
expect(globalStateUpdateStub.called).to.be.false;
expect(unlinkFileAsyncStub.called).to.be.false;
});
it(`if parsing the state fails`, async function () {
existsAsyncStub.resolves(true);
readFileAsyncStub.resolves('sometext');
globalStateUpdateStub.resolves();
await settingsManager.moveStateFromLegacyPlace();
expect(existsAsyncStub.calledOnce).to.be.true;
expect(readFileAsyncStub.calledOnce).to.be.true;
expect(
semverSpy.calledOnceWithExactly('0.0.0', '0.0.0'),
).to.be.true;
expect(globalStateUpdateStub.called).to.be.false;
expect(unlinkFileAsyncStub.called).to.be.false;
});
});
context(`it gets moved`, function () {
it(`if state version does NOT equal the default state version`, async function () {
stateMock.version = '1.0.0';
existsAsyncStub.resolves(true);
parseJSONStub.returns(stateMock);
globalStateUpdateStub.resolves();
await settingsManager.moveStateFromLegacyPlace();
expect(existsAsyncStub.calledOnce).to.be.true;
expect(readFileAsyncStub.calledOnce).to.be.true;
expect(
semverSpy.calledOnceWithExactly(stateMock.version, '0.0.0'),
).to.be.true;
expect(
globalStateUpdateStub.calledOnceWithExactly(
constants.vsicons.name,
stateMock,
),
).to.be.true;
expect(unlinkFileAsyncStub.calledOnce).to.be.true;
});
});
context(`an Error gets logged when`, function () {
it(`reading the file fails`, async function () {
existsAsyncStub.resolves(true);
const error = new Error();
readFileAsyncStub.rejects(error);
globalStateUpdateStub.resolves();
await settingsManager.moveStateFromLegacyPlace();
expect(logErrorStub.calledOnceWithExactly(error, true)).to.be.true;
expect(existsAsyncStub.calledOnce).to.be.true;
expect(readFileAsyncStub.calledOnce).to.be.true;
expect(
semverSpy.calledOnceWithExactly('0.0.0', '0.0.0'),
).to.be.true;
expect(globalStateUpdateStub.called).to.be.false;
expect(unlinkFileAsyncStub.called).to.be.false;
});
it(`deleting the file fails`, async function () {
stateMock.version = '1.0.0';
existsAsyncStub.resolves(true);
parseJSONStub.returns(stateMock);
globalStateUpdateStub.resolves();
const error = new Error();
unlinkFileAsyncStub.rejects(error);
await settingsManager.moveStateFromLegacyPlace();
expect(existsAsyncStub.calledOnce).to.be.true;
expect(readFileAsyncStub.calledOnce).to.be.true;
expect(
semverSpy.calledOnceWithExactly(stateMock.version, '0.0.0'),
).to.be.true;
expect(
globalStateUpdateStub.calledOnceWithExactly(
constants.vsicons.name,
stateMock,
),
).to.be.true;
expect(
unlinkFileAsyncStub.calledOnceWithExactly(undefined),
).to.be.true;
expect(logErrorStub.calledOnceWithExactly(error)).to.be.true;
});
});
});
});
it('the state gets set to the global state storage', async function () {
globalStateUpdateStub.resolves();
await settingsManager.setState(stateMock);
expect(
globalStateUpdateStub.calledOnceWithExactly(
constants.vsicons.name,
stateMock,
),
).to.be.true;
expect(logErrorStub.called).to.be.false;
});
it('an Error gets logged when setting the state fails', async function () {
const error = new Error();
globalStateUpdateStub.rejects(error);
await settingsManager.setState(stateMock);
expect(
globalStateUpdateStub.calledOnceWithExactly(
constants.vsicons.name,
stateMock,
),
).to.be.true;
expect(logErrorStub.calledOnceWithExactly(error)).to.be.true;
});
it('the state status gets updated', async function () {
globalStateGetStub.returns(stateMock);
globalStateUpdateStub.resolves();
const status = ExtensionStatus.activated;
const state = await settingsManager.updateStatus(status);
expect(
globalStateGetStub.calledOnceWithExactly(constants.vsicons.name),
).to.be.true;
expect(
globalStateUpdateStub.calledOnceWithExactly(
constants.vsicons.name,
stateMock,
),
).to.be.true;
expect(state.status).to.be.equal(status);
});
it('the settings status does NOT get updated, if no status is provided', async function () {
globalStateGetStub.returns(stateMock);
globalStateUpdateStub.resolves();
const state = await settingsManager.updateStatus();
expect(
globalStateGetStub.calledOnceWithExactly(constants.vsicons.name),
).to.be.true;
expect(
globalStateUpdateStub.calledOnceWithExactly(
constants.vsicons.name,
stateMock,
),
).to.be.true;
expect(state.version).to.be.equal(constants.extension.version);
expect(state.status).to.be.equal(stateMock.status);
expect(state.welcomeShown).to.be.true;
});
it('the state gets deleted', async function () {
globalStateUpdateStub.resolves();
await settingsManager.deleteState();
expect(
globalStateUpdateStub.calledOnceWithExactly(
constants.vsicons.name,
undefined,
),
).to.be.true;
});
it('an Error gets logged when deleting the state fails', async function () {
const error = new Error();
globalStateUpdateStub.rejects(error);
await settingsManager.deleteState();
expect(
globalStateUpdateStub.calledOnceWithExactly(
constants.vsicons.name,
undefined,
),
).to.be.true;
expect(logErrorStub.calledOnceWithExactly(error)).to.be.true;
});
context('getting the state from the global state storage', function () {
it('returns the default state when no state exists', function () {
globalStateGetStub.returns(undefined);
const state = settingsManager.getState();
expect(state).to.be.an('object');
expect(Reflect.ownKeys(state)).to.have.lengthOf(3);
expect(state).to.have.all.keys('version', 'status', 'welcomeShown');
expect(state.version).to.be.equal('0.0.0');
expect(
globalStateGetStub.calledOnceWithExactly(constants.vsicons.name),
).to.be.true;
});
it('returns the state', function () {
stateMock.version = '1.0.0';
globalStateGetStub.returns(stateMock);
const state = settingsManager.getState();
expect(state).to.be.instanceOf(Object);
expect(Object.keys(state)).to.have.lengthOf(3);
expect(state).to.have.all.keys('version', 'status', 'welcomeShown');
expect(state.version).to.be.equal('1.0.0');
expect(
globalStateGetStub.calledOnceWithExactly(constants.vsicons.name),
).to.be.true;
});
});
context(`the 'isNewVersion' function is`, function () {
it('truthy for a new extension version', function () {
stateMock.version = '1.0.0';
globalStateGetStub.returns(stateMock);
expect(settingsManager.isNewVersion).to.be.true;
expect(
globalStateGetStub.calledOnceWithExactly(constants.vsicons.name),
).to.be.true;
});
it('falsy for the same extension version', function () {
stateMock.version = constants.extension.version;
globalStateGetStub.returns(stateMock);
expect(settingsManager.isNewVersion).to.be.false;
expect(
globalStateGetStub.calledOnceWithExactly(constants.vsicons.name),
).to.be.true;
});
it('falsy for an older extension version', function () {
stateMock.version = '100.0.0';
globalStateGetStub.returns(stateMock);
expect(settingsManager.isNewVersion).to.be.false;
expect(
globalStateGetStub.calledOnceWithExactly(constants.vsicons.name),
).to.be.true;
});
});
});
});