forked from redhat-developer/yaml-language-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschemaSelectionHandlers.test.ts
More file actions
114 lines (97 loc) · 4.69 KB
/
schemaSelectionHandlers.test.ts
File metadata and controls
114 lines (97 loc) · 4.69 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
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
/*---------------------------------------------------------------------------------------------
* 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 chai from 'chai';
import * as sinonChai from 'sinon-chai';
import { JSONSchemaSelection } from '../src/languageserver/handlers/schemaSelectionHandlers';
import { YAMLSchemaService } from '../src/languageservice/services/yamlSchemaService';
import { Connection, RemoteClient } from 'vscode-languageserver/node';
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
import { SchemaSelectionRequests } from '../src/requestTypes';
import { SCHEMA_ID, setupSchemaIDTextDocument } from './utils/testHelper';
const expect = chai.expect;
chai.use(sinonChai);
describe('Schema Selection Handlers', () => {
const sandbox = sinon.createSandbox();
const connection: Connection = {} as Connection;
let service: YAMLSchemaService;
let requestServiceMock: sinon.SinonSpy;
beforeEach(() => {
requestServiceMock = sandbox.fake.resolves(undefined);
service = new YAMLSchemaService(requestServiceMock);
connection.client = {} as RemoteClient;
const onRequest = sandbox.fake();
connection.onRequest = onRequest;
});
afterEach(() => {
sandbox.restore();
});
it('add handler for "getSchema" and "getAllSchemas" requests', () => {
new JSONSchemaSelection(service, new SettingsState(), connection);
expect(connection.onRequest).calledWith(SchemaSelectionRequests.getSchema);
expect(connection.onRequest).calledWith(SchemaSelectionRequests.getAllSchemas);
});
it('getAllSchemas should return all schemas', async () => {
service.registerExternalSchema('https://some.com/some.json', ['foo.yaml'], undefined, 'Schema name', 'Schema description');
const settings = new SettingsState();
const testTextDocument = setupSchemaIDTextDocument('');
settings.documents = new TextDocumentTestManager();
(settings.documents as TextDocumentTestManager).set(testTextDocument);
const selection = new JSONSchemaSelection(service, settings, connection);
const result = await selection.getAllSchemas(testTextDocument.uri);
expect(result).length(1);
expect(result[0]).to.be.eqls({
uri: 'https://some.com/some.json',
fromStore: true,
usedForCurrentFile: false,
name: 'Schema name',
description: 'Schema description',
versions: undefined,
});
});
it('getAllSchemas should return all schemas and mark used for current file', async () => {
service.registerExternalSchema('https://some.com/some.json', [SCHEMA_ID], undefined, 'Schema name', 'Schema description');
const settings = new SettingsState();
const testTextDocument = setupSchemaIDTextDocument('');
settings.documents = new TextDocumentTestManager();
(settings.documents as TextDocumentTestManager).set(testTextDocument);
const selection = new JSONSchemaSelection(service, settings, connection);
const result = await selection.getAllSchemas(testTextDocument.uri);
expect(result).length(1);
expect(result[0]).to.be.eqls({
uri: 'https://some.com/some.json',
name: 'Schema name',
description: 'Schema description',
fromStore: false,
usedForCurrentFile: true,
versions: undefined,
});
});
it('getSchemas should return all schemas', async () => {
service.registerExternalSchema('https://some.com/some.json', [SCHEMA_ID], undefined, 'Schema name', 'Schema description');
const settings = new SettingsState();
const testTextDocument = setupSchemaIDTextDocument('');
settings.documents = new TextDocumentTestManager();
(settings.documents as TextDocumentTestManager).set(testTextDocument);
const selection = new JSONSchemaSelection(service, settings, connection);
const result = await selection.getSchemas(testTextDocument.uri);
expect(result).length(1);
expect(result[0]).to.be.eqls({
uri: 'https://some.com/some.json',
name: 'Schema name',
description: 'Schema description',
versions: undefined,
});
});
it('getSchemas should handle empty schemas', async () => {
const settings = new SettingsState();
const testTextDocument = setupSchemaIDTextDocument('');
settings.documents = new TextDocumentTestManager();
(settings.documents as TextDocumentTestManager).set(testTextDocument);
const selection = new JSONSchemaSelection(service, settings, connection);
const result = await selection.getSchemas(testTextDocument.uri);
expect(result).length(0);
});
});