forked from sveltejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetCompletions.test.ts
More file actions
167 lines (139 loc) · 5.89 KB
/
getCompletions.test.ts
File metadata and controls
167 lines (139 loc) · 5.89 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
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
import * as assert from 'assert';
import { EOL } from 'os';
import { Position } from 'vscode-languageserver';
import { getCompletions } from '../../../../src/plugins/svelte/features/getCompletions';
import { SvelteDocument } from '../../../../src/plugins/svelte/SvelteDocument';
import { Document } from '../../../../src/lib/documents';
import { getModifierData } from '../../../../src/plugins/svelte/features/getModifierData';
describe('SveltePlugin#getCompletions', () => {
function expectCompletionsFor(
content: string,
position: Position = Position.create(0, content.length)
) {
const document = new Document('url', content);
const svelteDoc = new SvelteDocument(document);
const completions = getCompletions(document, svelteDoc, position);
return {
toEqual: (expectedLabels: string[] | null) =>
assert.deepStrictEqual(
completions?.items.map((item) => item.label) ?? null,
expectedLabels
)
};
}
describe('should return null', () => {
it('if position inside style', () => {
expectCompletionsFor(
'<style>h1{color:blue;}</style><p>test</p>',
Position.create(0, 10)
).toEqual(null);
});
it('if position inside script', () => {
expectCompletionsFor(
'<script>const a = true</script><p>test</p>',
Position.create(0, 10)
).toEqual(null);
});
it('if not preceeded by valid content #1', () => {
expectCompletionsFor('{nope').toEqual(null);
});
it('if not preceeded by valid content #2', () => {
expectCompletionsFor('not really').toEqual(null);
});
it('if not preceeded by valid content #3', () => {
expectCompletionsFor('{#awa.').toEqual(null);
});
});
it('should return completions for #', () => {
expectCompletionsFor('{#').toEqual(['if', 'each', 'await :then', 'await then', 'key']);
});
it('should return completions for @', () => {
expectCompletionsFor('{@').toEqual(['html', 'debug', 'const']);
});
describe('should return no completions for :', () => {
it(' when no open tag before that', () => {
expectCompletionsFor('{:').toEqual(null);
});
it(' when only completed tag before that', () => {
expectCompletionsFor('{#if}{/if}{:').toEqual(null);
});
});
describe('should return no completions for /', () => {
it('when no open tag before that', () => {
expectCompletionsFor('{/').toEqual(null);
});
it('when only completed tag before that', () => {
expectCompletionsFor('{#if}{/if}{/').toEqual(null);
});
it('when the only completed tag before it has white space before close symbol', () => {
expectCompletionsFor('{#if}{ /if}{/').toEqual(null);
});
});
describe('should return completion for :', () => {
it('for if', () => {
expectCompletionsFor('{#if}{:').toEqual(['else', 'else if']);
});
it('for each', () => {
expectCompletionsFor('{#each}{:').toEqual(['else']);
});
it('for await', () => {
expectCompletionsFor('{#await}{:').toEqual(['then', 'catch']);
});
it('for last open tag', () => {
expectCompletionsFor('{#if}{/if}{#if}{#await}{:').toEqual(['then', 'catch']);
});
});
describe('should return completion for /', () => {
it('for if', () => {
expectCompletionsFor('{#if}{/').toEqual(['if']);
});
it('for each', () => {
expectCompletionsFor('{#each}{/').toEqual(['each']);
});
it('for await', () => {
expectCompletionsFor('{#await}{/').toEqual(['await']);
});
it('for key', () => {
expectCompletionsFor('{#key}{/').toEqual(['key']);
});
it('for last open tag', () => {
expectCompletionsFor('{#if}{/if}{#if}{#await}{/').toEqual(['await']);
});
});
it('should return completion for component documentation comment', () => {
const content = '<!--@';
const document = new Document('url', content);
const svelteDoc = new SvelteDocument(document);
const completions = getCompletions(document, svelteDoc, Position.create(0, content.length));
assert.deepStrictEqual(completions?.items?.[0].insertText, `component${EOL}$1${EOL}`);
});
function expectCompletionsForModifier(
content: string,
position = Position.create(0, content.lastIndexOf('|') + 1)
) {
return expectCompletionsFor(content, position);
}
describe('should return completion for event modifier', () => {
const modifierData = getModifierData();
const allModifiers = modifierData.map((modifier) => modifier.modifier);
it('can provides modifiers', () => {
expectCompletionsForModifier('<div on:click| />').toEqual(allModifiers);
});
it('can chain modifier and does not provide duplicated modifier', () => {
expectCompletionsForModifier('<div on:click|stopPropagation| />').toEqual(
allModifiers.filter((modifier) => modifier !== 'stopPropagation')
);
});
it("can chain modifier and does not provide modifier that can't used together", () => {
expectCompletionsForModifier('<div on:click|preventDefault| />').toEqual(
modifierData
.filter(
(modifier) =>
modifier.modifier != 'preventDefault' &&
!modifier.modifiersInvalidWith?.includes('preventDefault')
)
.map((modifier) => modifier.modifier)
);
});
});
});