forked from redhat-developer/vscode-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-schema-cache.test.ts
More file actions
68 lines (56 loc) · 2.43 KB
/
json-schema-cache.test.ts
File metadata and controls
68 lines (56 loc) · 2.43 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';
import * as chai from 'chai';
import * as vscode from 'vscode';
import * as fs from 'fs-extra';
import { JSONSchemaCache } from '../src/json-schema-cache';
import { TestMemento } from './helper';
const expect = chai.expect;
chai.use(sinonChai);
describe('JSON Schema Cache Tests', () => {
const sandbox = sinon.createSandbox();
let memento: sinon.SinonStubbedInstance<vscode.Memento>;
let ensureDirStub: sinon.SinonStub;
let readdirStub: sinon.SinonStub;
let pathExistsStub: sinon.SinonStub;
let readFileStub: sinon.SinonStub;
afterEach(() => {
sandbox.restore();
});
beforeEach(() => {
memento = sandbox.stub(new TestMemento());
ensureDirStub = sandbox.stub(fs, 'ensureDir');
readdirStub = sandbox.stub(fs, 'readdir');
pathExistsStub = sandbox.stub(fs, 'pathExists');
readFileStub = sandbox.stub(fs, 'readFile');
});
it('should clean up cache if there are no schema file', async () => {
memento.get.returns({ somePath: { schemaPath: '/foo/path/' } });
memento.update.resolves();
ensureDirStub.resolves();
readdirStub.resolves([]);
pathExistsStub.resolves(false);
readFileStub.resolves();
const cache = new JSONSchemaCache('/some/path/', (memento as unknown) as vscode.Memento, {} as vscode.OutputChannel);
const result = await cache.getSchema('/some/uri');
expect(result).is.undefined;
expect(memento.update).calledOnceWith('json-schema-key', {});
});
it('should check cache', async () => {
const mementoData = { somePath: { schemaPath: '/some/path/foo.json' } };
memento.get.returns(mementoData);
memento.update.resolves();
ensureDirStub.resolves();
readdirStub.resolves(['foo.json']);
pathExistsStub.resolves(false);
readFileStub.resolves();
const cache = new JSONSchemaCache('/some/path/', (memento as unknown) as vscode.Memento, {} as vscode.OutputChannel);
const result = await cache.getSchema('/some/uri');
expect(result).is.undefined;
expect(memento.update).calledOnceWith('json-schema-key', mementoData);
});
});