/
get-completion-at-position.test.ts
66 lines (54 loc) · 3.06 KB
/
get-completion-at-position.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
import ts from 'typescript';
import { GraphQLSchema } from 'graphql';
import { AdapterFixture } from './testing/adapter-fixture';
import { createSimpleSchema } from './testing/simple-schema';
const notFoundCompletionInfo: ts.CompletionInfo = {
entries: [],
isGlobalCompletion: false,
isMemberCompletion: false,
isNewIdentifierLocation: false,
};
const delegateFn = () => notFoundCompletionInfo;
function createFixture(name: string, schema?: GraphQLSchema) {
return new AdapterFixture(name, schema);
}
describe('getCompletionAtPosition', () => {
it('should delegate original method when schema is not set', () => {
const fixture = createFixture('input.ts');
const actual = fixture.adapter.getCompletionAtPosition(delegateFn, 'input.ts', 0, undefined);
expect(actual).toBe(notFoundCompletionInfo);
});
it('should delegate original method when the cursor is not on Template String Literal', () => {
const fixture = createFixture('input.ts', createSimpleSchema());
fixture.source = 'const a = 1;';
const actual = fixture.adapter.getCompletionAtPosition(delegateFn, 'input.ts', 0, undefined);
expect(actual).toBe(notFoundCompletionInfo);
});
it('should return completion entries', () => {
const fixture = createFixture('input.ts', createSimpleSchema());
const completionFn = fixture.adapter.getCompletionAtPosition.bind(fixture.adapter, delegateFn, 'input.ts');
fixture.source = 'const a = `';
expect(completionFn(10, undefined)!.entries.length).toBeTruthy(); // return entries when cursor is at the start of the template
fixture.source = 'const a = `q';
expect(completionFn(11, undefined)!.entries.length);
expect(completionFn(11, undefined)!.entries).toEqual([
{ kind: '4' as ts.ScriptElementKind, kindModifiers: 'declare', name: '{', sortText: '0' },
{ kind: '3' as ts.ScriptElementKind, kindModifiers: 'declare', name: 'query', sortText: '0' },
] as ts.CompletionEntry[]);
fixture.source = 'const a = `query {';
expect(completionFn(17, undefined)!.entries).toBeTruthy();
expect(completionFn(17, undefined)!.entries.filter(e => e.name === 'hello').length).toBeTruthy(); // contains schema keyword;
fixture.source = 'const a = `query { }`';
expect(completionFn(17, undefined)!.entries).toBeTruthy();
expect(completionFn(17, undefined)!.entries.filter(e => e.name === 'hello').length).toBeTruthy(); // contains schema keyword;
});
it('should return completion entries with external fragment definitions', () => {
const fixture = createFixture('input.ts', createSimpleSchema());
const completionFn = fixture.adapter.getCompletionAtPosition.bind(fixture.adapter, delegateFn, 'input.ts');
fixture.registerFragment('fragments.ts', 'fragment FRAGMENT on Query { hello }');
fixture.source = 'const a = `query { ...FR';
expect(completionFn(23, undefined)!.entries).toBeTruthy();
expect(completionFn(23, undefined)!.entries.length).not.toBe(0);
expect(completionFn(23, undefined)!.entries.filter(e => e.name === 'FRAGMENT').length).toBeTruthy(); // contains schema keyword;
});
});