forked from atom/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippet-loading-spec.js
320 lines (264 loc) · 11.3 KB
/
snippet-loading-spec.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const path = require('path');
const fs = require('fs-plus');
const temp = require('temp').track();
describe("Snippet Loading", () => {
let configDirPath, snippetsService, defaultContext;
beforeEach(() => {
configDirPath = temp.mkdirSync('atom-config-dir-');
spyOn(atom, 'getConfigDirPath').andReturn(configDirPath);
spyOn(console, 'warn');
if (atom.notifications != null) { spyOn(atom.notifications, 'addError'); }
spyOn(atom.packages, 'getLoadedPackages').andReturn([
atom.packages.loadPackage(path.join(__dirname, 'fixtures', 'package-with-snippets')),
atom.packages.loadPackage(path.join(__dirname, 'fixtures', 'package-with-broken-snippets')),
]);
});
afterEach(() => {
waitsForPromise(() => Promise.resolve(atom.packages.deactivatePackages('snippets')));
runs(() => {
jasmine.unspy(atom.packages, 'getLoadedPackages');
});
});
const activateSnippetsPackage = () => {
waitsForPromise(() => atom.packages.activatePackage("snippets").then(({mainModule}) => {
snippetsService = mainModule.provideSnippets();
mainModule.loaded = false;
}));
waitsFor("all snippets to load", 3000, () => snippetsService.bundledSnippetsLoaded());
};
it("loads the bundled snippet template snippets", () => {
activateSnippetsPackage();
runs(() => {
const jsonSnippet = snippetsService.snippetsForScopes(['.source.json'])['snip'];
let instance = jsonSnippet.generateInstance();
expect(jsonSnippet.name).toBe('Atom Snippet');
expect(jsonSnippet.prefix).toBe('snip');
expect(instance.bodyText).toContain('"prefix":');
expect(instance.bodyText).toContain('"body":');
expect(instance.tabStopList.length).toBeGreaterThan(0);
const csonSnippet = snippetsService.snippetsForScopes(['.source.coffee'])['snip'];
instance = csonSnippet.generateInstance();
expect(csonSnippet.name).toBe('Atom Snippet');
expect(csonSnippet.prefix).toBe('snip');
expect(instance.bodyText).toContain("'prefix':");
expect(instance.bodyText).toContain("'body':");
expect(instance.tabStopList.length).toBeGreaterThan(0);
});
});
it("loads non-hidden snippet files from atom packages with snippets directories", () => {
activateSnippetsPackage();
runs(() => {
let snippet = snippetsService.snippetsForScopes(['.test'])['test'];
let instance = snippet.generateInstance();
expect(snippet.prefix).toBe('test');
expect(instance.bodyText).toBe('testing 123');
snippet = snippetsService.snippetsForScopes(['.test'])['testd'];
instance = snippet.generateInstance();
expect(snippet.prefix).toBe('testd');
expect(snippet.description).toBe('a description');
expect(snippet.descriptionMoreURL).toBe('http://google.com');
expect(instance.bodyText).toBe('testing 456');
snippet = snippetsService.snippetsForScopes(['.test'])['testlabelleft'];
instance = snippet.generateInstance();
expect(snippet.prefix).toBe('testlabelleft');
expect(snippet.leftLabel).toBe('a label');
expect(instance.bodyText).toBe('testing 456');
snippet = snippetsService.snippetsForScopes(['.test'])['testhtmllabels'];
instance = snippet.generateInstance();
expect(snippet.prefix).toBe('testhtmllabels');
expect(snippet.leftLabelHTML).toBe('<span style=\"color:red\">Label</span>');
expect(snippet.rightLabelHTML).toBe('<span style=\"color:white\">Label</span>');
expect(instance.bodyText).toBe('testing 456');
});
});
it("logs a warning if package snippets files cannot be parsed", () => {
activateSnippetsPackage();
runs(() => {
// Warn about invalid-file, but don't even try to parse a hidden file
expect(console.warn.calls.length).toBeGreaterThan(0);
expect(console.warn.mostRecentCall.args[0]).toMatch(/Error reading.*package-with-broken-snippets/);
});
});
describe("::loadPackageSnippets(callback)", () => {
beforeEach(() => { // simulate a list of packages where the javascript core package is returned at the end
atom.packages.getLoadedPackages.andReturn([
atom.packages.loadPackage(path.join(__dirname, 'fixtures', 'package-with-snippets')),
atom.packages.loadPackage('language-javascript')
])
});
it("allows other packages to override core packages' snippets", () => {
waitsForPromise(() => atom.packages.activatePackage("language-javascript"));
activateSnippetsPackage();
runs(() => {
expect(atom.packages.getLoadedPackages().length).toBe(2);
expect(atom.packages.isPackageLoaded("package-with-snippets")).toBe(true);
expect(atom.packages.isPackageLoaded("language-javascript")).toBe(true);
const snippet = snippetsService.snippetsForScopes([".source.js"])["log"];
expect(snippet.generateInstance().bodyText).toBe("from-a-community-package");
});
});
});
describe("::onDidLoadSnippets(callback)", () => {
it("invokes listeners when all snippets are loaded", () => {
let loadedCallback = null;
waitsFor("package to activate", done => atom.packages.activatePackage("snippets").then(({mainModule}) => {
mainModule.onDidLoadSnippets(loadedCallback = jasmine.createSpy('onDidLoadSnippets callback'));
done();
}));
waitsFor("onDidLoad callback to be called", () => loadedCallback.callCount > 0);
});
});
describe("when ~/.atom/snippets.json exists", () => {
beforeEach(() => {
fs.writeFileSync(path.join(configDirPath, 'snippets.json'), `\
{
".foo": {
"foo snippet": {
"prefix": "foo",
"body": "bar1"
}
}
}\
`
);
activateSnippetsPackage();
});
it("loads the snippets from that file", () => {
let snippet = null;
waitsFor(() => snippet = snippetsService.snippetsForScopes(['.foo'])['foo']);
runs(() => {
expect(snippet.name).toBe('foo snippet');
expect(snippet.prefix).toBe("foo");
expect(snippet.generateInstance().bodyText).toBe("bar1");
});
});
describe("when that file changes", () => {
it("reloads the snippets", () => {
fs.writeFileSync(path.join(configDirPath, 'snippets.json'), `\
{
".foo": {
"foo snippet": {
"prefix": "foo",
"body": "bar2"
}
}
}\
`
);
waitsFor("snippets to be changed", () => {
const snippet = snippetsService.snippetsForScopes(['.foo'])['foo'];
return snippet && snippet.generateInstance().bodyText === 'bar2';
});
runs(() => {
fs.writeFileSync(path.join(configDirPath, 'snippets.json'), "");
});
waitsFor("snippets to be removed", () => !snippetsService.snippetsForScopes(['.foo'])['foo']);
});
});
});
describe("when ~/.atom/snippets.cson exists", () => {
beforeEach(() => {
fs.writeFileSync(path.join(configDirPath, 'snippets.cson'), `\
".foo":
"foo snippet":
"prefix": "foo"
"body": "bar1"\
`
);
activateSnippetsPackage();
});
it("loads the snippets from that file", () => {
let snippet = null;
waitsFor(() => snippet = snippetsService.snippetsForScopes(['.foo'])['foo']);
runs(() => {
expect(snippet.name).toBe('foo snippet');
expect(snippet.prefix).toBe("foo");
expect(snippet.generateInstance().bodyText).toBe("bar1");
});
});
describe("when that file changes", () => {
it("reloads the snippets", () => {
fs.writeFileSync(path.join(configDirPath, 'snippets.cson'), `\
".foo":
"foo snippet":
"prefix": "foo"
"body": "bar2"\
`
);
waitsFor("snippets to be changed", () => {
const snippet = snippetsService.snippetsForScopes(['.foo'])['foo'];
return snippet && snippet.generateInstance().bodyText === 'bar2';
});
runs(() => {
fs.writeFileSync(path.join(configDirPath, 'snippets.cson'), "");
});
waitsFor("snippets to be removed", () => {
const snippet = snippetsService.snippetsForScopes(['.foo'])['foo'];
return snippet == null;
});
});
});
});
it("notifies the user when the user snippets file cannot be loaded", () => {
fs.writeFileSync(path.join(configDirPath, 'snippets.cson'), '".junk":::');
activateSnippetsPackage();
runs(() => {
expect(console.warn).toHaveBeenCalled();
if (atom.notifications != null) {
expect(atom.notifications.addError).toHaveBeenCalled();
}
});
});
describe("packages-with-snippets-disabled feature", () => {
it("disables no snippets if the config option is empty", () => {
const originalConfig = atom.config.get('core.packagesWithSnippetsDisabled');
atom.config.set('core.packagesWithSnippetsDisabled', []);
activateSnippetsPackage();
runs(() => {
const snippets = snippetsService.snippetsForScopes(['.package-with-snippets-unique-scope']);
expect(Object.keys(snippets).length).toBe(1);
atom.config.set('core.packagesWithSnippetsDisabled', originalConfig);
});
});
it("still includes a disabled package's snippets in the list of unparsed snippets", () => {
let originalConfig = atom.config.get('core.packagesWithSnippetsDisabled');
atom.config.set('core.packagesWithSnippetsDisabled', []);
activateSnippetsPackage();
runs(() => {
atom.config.set('core.packagesWithSnippetsDisabled', ['package-with-snippets']);
const allSnippets = snippetsService.getUnparsedSnippets();
const scopedSnippet = allSnippets.find(s => s.selectorString === '.package-with-snippets-unique-scope');
expect(scopedSnippet).not.toBe(undefined);
atom.config.set('core.packagesWithSnippetsDisabled', originalConfig);
});
});
it("never loads a package's snippets when that package is disabled in config", () => {
const originalConfig = atom.config.get('core.packagesWithSnippetsDisabled');
atom.config.set('core.packagesWithSnippetsDisabled', ['package-with-snippets']);
activateSnippetsPackage();
runs(() => {
const snippets = snippetsService.snippetsForScopes(['.package-with-snippets-unique-scope']);
expect(Object.keys(snippets).length).toBe(0);
atom.config.set('core.packagesWithSnippetsDisabled', originalConfig);
});
});
it("unloads and/or reloads snippets from a package if the config option is changed after activation", () => {
const originalConfig = atom.config.get('core.packagesWithSnippetsDisabled');
atom.config.set('core.packagesWithSnippetsDisabled', []);
activateSnippetsPackage();
runs(() => {
let snippets = snippetsService.snippetsForScopes(['.package-with-snippets-unique-scope']);
expect(Object.keys(snippets).length).toBe(1);
// Disable it.
atom.config.set('core.packagesWithSnippetsDisabled', ['package-with-snippets']);
snippets = snippetsService.snippetsForScopes(['.package-with-snippets-unique-scope']);
expect(Object.keys(snippets).length).toBe(0);
// Re-enable it.
atom.config.set('core.packagesWithSnippetsDisabled', []);
snippets = snippetsService.snippetsForScopes(['.package-with-snippets-unique-scope']);
expect(Object.keys(snippets).length).toBe(1);
atom.config.set('core.packagesWithSnippetsDisabled', originalConfig);
});
});
});
});