UNPKG

17.5 kBJavaScriptView Raw
1var assert = require('assert'),
2 fs = require('fs-extra'),
3 path = require('path'),
4 temp = require('unique-temp-dir'),
5 watcher = require('../lib/watcher');
6
7describe('watcher', function() {
8 var main, sibling;
9 var origin = path.join(__dirname, 'fixtures', 'watcher');
10
11 beforeEach(function() {
12 var fixture = temp();
13 fs.ensureDirSync(fixture);
14 fs.copySync(origin, fixture);
15 main = fs.realpathSync(path.join(fixture, 'main'));
16 sibling = fs.realpathSync(path.join(fixture, 'sibling'));
17 });
18
19 describe('with directory', function() {
20 beforeEach(function() {
21 watcher.reset({
22 directory: main,
23 includePath: [main]
24 });
25 });
26
27 describe('when a file is changed', function() {
28 describe('and it is in the graph', function() {
29 describe('if it is a partial', function() {
30 it('should record its ancestors as changed', function() {
31 var file = path.join(main, 'partials', '_one.scss');
32 var files = watcher.changed(file);
33 assert.deepEqual(files.changed, [
34 path.join(main, 'one.scss'),
35 ]);
36 });
37
38 it('should record its descendants as added', function() {
39 var file = path.join(main, 'partials', '_one.scss');
40 var files = watcher.changed(file);
41 assert.deepEqual(files.added, [
42 path.join(main, 'partials', '_three.scss'),
43 ]);
44 });
45
46 it('should record nothing as removed', function() {
47 var file = path.join(main, 'partials', '_one.scss');
48 var files = watcher.changed(file);
49 assert.deepEqual(files.removed, []);
50 });
51 });
52
53 describe('if it is not a partial', function() {
54 it('should record itself as changed', function() {
55 var file = path.join(main, 'one.scss');
56 var files = watcher.changed(file);
57 assert.deepEqual(files.changed, [
58 file,
59 ]);
60 });
61
62 it('should record its descendants as added', function() {
63 var file = path.join(main, 'one.scss');
64 var files = watcher.changed(file);
65 assert.deepEqual(files.added, [
66 path.join(main, 'partials', '_one.scss'),
67 path.join(main, 'partials', '_three.scss'),
68 ]);
69 });
70
71 it('should record nothing as removed', function() {
72 var file = path.join(main, 'one.scss');
73 var files = watcher.changed(file);
74 assert.deepEqual(files.removed, []);
75 });
76 });
77 });
78
79 describe('and is not in the graph', function() {
80 describe('if it is a partial', function() {
81 it('should not record anything', function() {
82 var file = path.join(sibling, 'partials', '_three.scss');
83 var files = watcher.changed(file);
84 assert.deepEqual(files, {
85 added: [],
86 changed: [],
87 removed: [],
88 });
89 });
90 });
91
92 describe('if it is not a partial', function() {
93 it('should record itself as changed', function() {
94 var file = path.join(sibling, 'three.scss');
95 var files = watcher.changed(file);
96 assert.deepEqual(files, {
97 added: [],
98 changed: [file],
99 removed: [],
100 });
101 });
102 });
103 });
104 });
105
106 describe('when a file is added', function() {
107 describe('and it is in the graph', function() {
108 describe('if it is a partial', function() {
109 it('should record nothing as added', function() {
110 var file = path.join(main, 'partials', '_three.scss');
111 var files = watcher.added(file);
112 assert.deepEqual(files.added, []);
113 });
114
115 it('should record its descendants as added', function() {
116 var file = path.join(main, 'partials', '_one.scss');
117 var files = watcher.added(file);
118 assert.deepEqual(files.added, [
119 path.join(main, 'partials', '_three.scss')
120 ]);
121 });
122
123 it('should record nothing as changed', function() {
124 var file = path.join(main, 'partials', '_three.scss');
125 var files = watcher.added(file);
126 assert.deepEqual(files.changed, []);
127 });
128
129 it('should record nothing as removed', function() {
130 var file = path.join(main, 'partials', '_three.scss');
131 var files = watcher.added(file);
132 assert.deepEqual(files.removed, []);
133 });
134 });
135
136 describe('if it is not a partial', function() {
137 it('should record nothing as added', function() {
138 var file = path.join(main, 'three.scss');
139 var files = watcher.added(file);
140 assert.deepEqual(files.added, []);
141 });
142
143 it('should record its descendants as added', function() {
144 var file = path.join(main, 'one.scss');
145 var files = watcher.added(file);
146 assert.deepEqual(files.added, [
147 path.join(main, 'partials', '_one.scss'),
148 path.join(main, 'partials', '_three.scss'),
149 ]);
150 });
151
152 it('should record nothing as changed', function() {
153 var file = path.join(main, 'one.scss');
154 var files = watcher.added(file);
155 assert.deepEqual(files.changed, []);
156 });
157
158 it('should record nothing as removed', function() {
159 var file = path.join(main, 'one.scss');
160 var files = watcher.added(file);
161 assert.deepEqual(files.removed, []);
162 });
163 });
164 });
165 });
166
167 describe('when a file is removed', function() {
168 describe('and it is in the graph', function() {
169 describe('if it is a partial', function() {
170 it('should record nothing as added', function() {
171 var file = path.join(main, 'partials', '_one.scss');
172 var files = watcher.removed(file);
173 assert.deepEqual(files.added, []);
174 });
175
176 it('should record its ancestors as changed', function() {
177 var file = path.join(main, 'partials', '_one.scss');
178 var files = watcher.removed(file);
179 assert.deepEqual(files.changed, [
180 path.join(main, 'one.scss'),
181 ]);
182 });
183
184 it('should record itself as removed', function() {
185 var file = path.join(main, 'partials', '_one.scss');
186 var files = watcher.removed(file);
187 assert.deepEqual(files.removed, [file]);
188 });
189 });
190
191 describe('if it is not a partial', function() {
192 it('should record nothing as added', function() {
193 var file = path.join(main, 'one.scss');
194 var files = watcher.removed(file);
195 assert.deepEqual(files.added, []);
196 });
197
198 it('should record nothing as changed', function() {
199 var file = path.join(main, 'one.scss');
200 var files = watcher.removed(file);
201 assert.deepEqual(files.changed, []);
202 });
203
204 it('should record itself as removed', function() {
205 var file = path.join(main, 'one.scss');
206 var files = watcher.removed(file);
207 assert.deepEqual(files.removed, [file]);
208 });
209 });
210 });
211
212 describe('and is not in the graph', function() {
213 describe('if it is a partial', function() {
214 it('should record nothing', function() {
215 var file = path.join(sibling, 'partials', '_three.scss');
216 var files = watcher.removed(file);
217 assert.deepEqual(files, {
218 added: [],
219 changed: [],
220 removed: [],
221 });
222 });
223 });
224
225 describe('if it is not a partial', function() {
226 it('should record nothing', function() {
227 var file = path.join(sibling, 'three.scss');
228 var files = watcher.removed(file);
229 assert.deepEqual(files, {
230 added: [],
231 changed: [],
232 removed: [],
233 });
234 });
235 });
236 });
237 });
238 });
239
240 describe('with file', function() {
241 beforeEach(function() {
242 watcher.reset({
243 src: path.join(main, 'one.scss'),
244 includePath: [main]
245 });
246 });
247
248 describe('when a file is changed', function() {
249 describe('and it is in the graph', function() {
250 describe('if it is a partial', function() {
251 it('should record its descendants as added', function() {
252 var file = path.join(main, 'partials', '_one.scss');
253 var files = watcher.changed(file);
254 assert.deepEqual(files.added, [
255 path.join(main, 'partials', '_three.scss'),
256 ]);
257 });
258
259 it('should record its ancenstors as changed', function() {
260 var file = path.join(main, 'partials', '_one.scss');
261 var files = watcher.changed(file);
262 assert.deepEqual(files.changed, [
263 path.join(main, 'one.scss'),
264 ]);
265 });
266
267 it('should record nothing as removed', function() {
268 var file = path.join(main, 'partials', '_one.scss');
269 var files = watcher.changed(file);
270 assert.deepEqual(files.removed, []);
271 });
272 });
273
274 describe('if it is not a partial', function() {
275 it('should record its descendants as added', function() {
276 var file = path.join(main, 'one.scss');
277 var files = watcher.changed(file);
278 assert.deepEqual(files.added, [
279 path.join(main, 'partials', '_one.scss'),
280 path.join(main, 'partials', '_three.scss'),
281 ]);
282 });
283
284 it('should record itself as changed', function() {
285 var file = path.join(main, 'one.scss');
286 var files = watcher.changed(file);
287 assert.deepEqual(files.changed, [file]);
288 });
289
290 it('should record nothing as removed', function() {
291 var file = path.join(main, 'one.scss');
292 var files = watcher.changed(file);
293 assert.deepEqual(files.removed, []);
294 });
295 });
296 });
297
298 describe('and it is not in the graph', function() {
299 describe('if it is a partial', function() {
300 it('should record nothing', function() {
301 var file = path.join(sibling, 'partials', '_three.scss');
302 var files = watcher.changed(file);
303 assert.deepEqual(files, {
304 added: [],
305 changed: [],
306 removed: [],
307 });
308 });
309 });
310
311 describe('if it is not a partial', function() {
312 it('should record nothing as added', function() {
313 var file = path.join(sibling, 'three.scss');
314 var files = watcher.changed(file);
315 assert.deepEqual(files.added, []);
316 });
317
318 it('should record itself as changed', function() {
319 var file = path.join(sibling, 'three.scss');
320 var files = watcher.changed(file);
321 assert.deepEqual(files.changed, [file]);
322 });
323
324 it('should record nothing as removed', function() {
325 var file = path.join(sibling, 'three.scss');
326 var files = watcher.changed(file);
327 assert.deepEqual(files.removed, []);
328 });
329 });
330 });
331 });
332
333 describe('when a file is added', function() {
334 describe('and it is in the graph', function() {
335 it('should record nothing as added', function() {
336 var file = path.join(main, 'partials', '_three.scss');
337 var files = watcher.added(file);
338 assert.deepEqual(files.added, []);
339 });
340
341 it('should record its descendants as added', function() {
342 var file = path.join(main, 'partials', '_one.scss');
343 var files = watcher.added(file);
344 assert.deepEqual(files.added, [
345 path.join(main, 'partials', '_three.scss'),
346 ]);
347 });
348
349 it('should record nothing as changed', function() {
350 var file = path.join(main, 'partials', '_three.scss');
351 var files = watcher.added(file);
352 assert.deepEqual(files.changed, []);
353 });
354
355 it('should record nothing as removed', function() {
356 var file = path.join(main, 'partials', '_three.scss');
357 var files = watcher.added(file);
358 assert.deepEqual(files.removed, []);
359 });
360 });
361
362 describe('and it is not in the graph', function() {
363 beforeEach(function() {
364 watcher.reset({
365 src: path.join(main, 'two.scss'),
366 includePath: [main]
367 });
368 });
369
370 describe('if it is a partial', function() {
371 it('should record nothing as added', function() {
372 var file = path.join(main, 'partials', '_three.scss');
373 var files = watcher.added(file);
374 assert.deepEqual(files.added, [
375 file,
376 ]);
377 });
378
379 it('should not record its descendants as added', function() {
380 var file = path.join(main, 'partials', '_one.scss');
381 var files = watcher.added(file);
382 assert.deepEqual(files.added, [
383 file,
384 ]);
385 });
386
387 it('should record nothing as changed', function() {
388 var file = path.join(main, 'partials', '_three.scss');
389 var files = watcher.added(file);
390 assert.deepEqual(files.changed, []);
391 });
392
393 it('should record nothing as removed', function() {
394 var file = path.join(main, 'partials', '_three.scss');
395 var files = watcher.added(file);
396 assert.deepEqual(files.removed, []);
397 });
398 });
399
400 describe('if it is not a partial', function() {
401 it('should record itself as added', function() {
402 var file = path.join(main, 'three.scss');
403 var files = watcher.added(file);
404 assert.deepEqual(files.added, [
405 file,
406 ]);
407 });
408
409 it('should record nothing as changed', function() {
410 var file = path.join(main, 'one.scss');
411 var files = watcher.added(file);
412 assert.deepEqual(files.changed, []);
413 });
414
415 it('should record nothing as removed', function() {
416 var file = path.join(main, 'one.scss');
417 var files = watcher.added(file);
418 assert.deepEqual(files.removed, []);
419 });
420 });
421 });
422 });
423
424 describe('when a file is removed', function() {
425 describe('and it is in the graph', function() {
426 describe('if it is a partial', function() {
427 it('should record nothing as added', function() {
428 var file = path.join(main, 'partials', '_one.scss');
429 var files = watcher.removed(file);
430 assert.deepEqual(files.added, []);
431 });
432
433 it('should record its ancestors as changed', function() {
434 var file = path.join(main, 'partials', '_one.scss');
435 var files = watcher.removed(file);
436 assert.deepEqual(files.changed, [
437 path.join(main, 'one.scss'),
438 ]);
439 });
440
441 it('should record itself as removed', function() {
442 var file = path.join(main, 'partials', '_one.scss');
443 var files = watcher.removed(file);
444 assert.deepEqual(files.removed, [file]);
445 });
446 });
447
448 describe('if it is not a partial', function() {
449 it('should record nothing as added', function() {
450 var file = path.join(main, 'one.scss');
451 var files = watcher.removed(file);
452 assert.deepEqual(files.added, []);
453 });
454
455 it('should record nothing as changed', function() {
456 var file = path.join(main, 'one.scss');
457 var files = watcher.removed(file);
458 assert.deepEqual(files.changed, []);
459 });
460
461 it('should record itself as removed', function() {
462 var file = path.join(main, 'one.scss');
463 var files = watcher.removed(file);
464 assert.deepEqual(files.removed, [file]);
465 });
466 });
467 });
468
469 describe('and is not in the graph', function() {
470 beforeEach(function() {
471 watcher.reset({
472 src: path.join(main, 'two.scss'),
473 includePath: [main]
474 });
475 });
476
477 describe('if it is a partial', function() {
478 it('should record nothing as added', function() {
479 var file = path.join(main, 'partials', '_one.scss');
480 var files = watcher.removed(file);
481 assert.deepEqual(files, {
482 added: [],
483 changed: [],
484 removed: [],
485 });
486 });
487 });
488
489 describe('if it is not a partial', function() {
490 it('should record nothing', function() {
491 var file = path.join(main, 'one.scss');
492 var files = watcher.removed(file);
493 assert.deepEqual(files, {
494 added: [],
495 changed: [],
496 removed: [],
497 });
498 });
499 });
500 });
501 });
502 });
503});