-
Notifications
You must be signed in to change notification settings - Fork 3
/
completion.ts
97 lines (78 loc) · 3.06 KB
/
completion.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
import * as core from '@donaldpipowitch/vscode-extension-core';
import { configureCompletion } from '../src/completion';
import { prettier } from './__fixtures__/search';
const setup = ({ text, offset }: { text: string; offset: number }) => {
const search = jest.spyOn(core, 'search');
let resolve: Function;
const request = new Promise((_resolve) => (resolve = _resolve));
const cancel = jest.fn(() => resolve());
search.mockReturnValue({ cancel, request });
const mockedConnection = { onCompletion: jest.fn() };
const mockedDocuments = {
get() {
return {
getText() {
return text;
},
offsetAt() {
return offset;
}
};
}
};
configureCompletion(mockedConnection as any, mockedDocuments as any);
expect(mockedConnection.onCompletion).toHaveBeenCalledTimes(1);
const completionHandler = mockedConnection.onCompletion.mock.calls[0][0];
return {
callCompletionHandler: () =>
completionHandler(
{
textDocument: { uri: '.vscode/extensions.json' }
},
{ onCancellationRequested: () => {} }
),
resolveSearch: () => resolve(prettier),
cancelSearch: cancel
};
};
test('should provide completion items (search "prettier")', async () => {
const text = '{"recommendations": ["prettier"]}';
const offset = 21; // the `"`, before `prettier"]`
const mocks = setup({ text, offset });
const itemsPromise = mocks.callCompletionHandler();
mocks.resolveSearch();
expect(await itemsPromise).toMatchSnapshot();
});
test('should provide no completion items (search term is too short)', async () => {
const text = '{"recommendations": ["p"]}';
const offset = 21; // the `"`, before `p"]`
const mocks = setup({ text, offset });
const itemsPromise = mocks.callCompletionHandler();
mocks.resolveSearch();
expect(await itemsPromise).toMatchSnapshot();
});
test('should provide no completion items (triggered on "recommendations" property)', async () => {
const text = '{"recommendations": ["prettier"]}';
const offset = 1; // the `"`, before `recommendations"`
const mocks = setup({ text, offset });
const itemsPromise = mocks.callCompletionHandler();
mocks.resolveSearch();
expect(await itemsPromise).toMatchSnapshot();
});
test('should provide no completion items (triggered on root)', async () => {
const text = '{"recommendations": ["prettier"]}';
const offset = 0; // the initial `{`
const mocks = setup({ text, offset });
const itemsPromise = mocks.callCompletionHandler();
mocks.resolveSearch();
expect(await itemsPromise).toMatchSnapshot();
});
test('should cancel, if completion handler is called again, before search was resolved', async () => {
const text = '{"recommendations": ["prettier"]}';
const offset = 21; // the `"`, before `prettier"]`
const mocks = setup({ text, offset });
const itemsPromise = mocks.callCompletionHandler();
mocks.callCompletionHandler(); // call handler again, while search was not resolved
expect(await itemsPromise).toBe(undefined);
expect(mocks.cancelSearch).toHaveBeenCalledTimes(1);
});