|
1 |
| -import { constant } from 'lodash'; |
| 1 | +import { noop } from 'lodash'; |
2 | 2 | import { equal, ok } from 'assert';
|
| 3 | +import { dropCache, spy, Through } from '../utils/sugar'; |
3 | 4 | import hook from '../src';
|
4 | 5 |
|
5 | 6 | describe('public api', () => {
|
6 |
| - beforeEach(() => { |
7 |
| - // clearing cache |
8 |
| - delete require.cache[require.resolve('awesome-theme/oceanic.css')]; |
9 |
| - }); |
| 7 | + afterEach(() => dropCache('awesome-theme/oceanic.css')); |
10 | 8 |
|
11 |
| - describe('preprocessCss()', () => { |
12 |
| - describe('content of the file and filename are provided to the preprocessCss function', () => { |
13 |
| - let providedCSS; |
14 |
| - let providedFilename; |
| 9 | + describe('extending list of plugins', () => { |
| 10 | + describe('"prepend" should add plugins in the beginning of the pipeline', () => { |
| 11 | + let processor; |
15 | 12 |
|
16 | 13 | beforeEach(() => {
|
17 |
| - const spy = (content, filename) => { |
18 |
| - providedCSS = content; |
19 |
| - providedFilename = filename; |
20 |
| - return content; |
21 |
| - }; |
| 14 | + processor = spy(noop); |
22 | 15 |
|
23 |
| - hook({preprocessCss: spy}); |
24 |
| - require('awesome-theme/oceanic.css'); |
25 |
| - }); |
| 16 | + hook({ |
| 17 | + prepend: [ |
| 18 | + new Through({processor: processor}), |
| 19 | + ], |
| 20 | + }); |
26 | 21 |
|
27 |
| - it('content of file should be provided', () => { |
28 |
| - equal(typeof providedCSS, 'string'); |
29 |
| - ok(providedCSS.length); |
| 22 | + require('awesome-theme/oceanic.css'); |
30 | 23 | });
|
31 | 24 |
|
32 |
| - it('filename should be provided', () => { |
33 |
| - equal(providedFilename, require.resolve('awesome-theme/oceanic.css')); |
34 |
| - }); |
| 25 | + it('processor should be called', () => |
| 26 | + ok(processor.called)); |
35 | 27 | });
|
36 | 28 |
|
37 |
| - describe('providing empty string constantly', () => { |
38 |
| - before(() => hook({preprocessCss: constant('')})); |
| 29 | + describe('"append" should add plugins to the pipeline', () => { |
| 30 | + let processor; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + processor = spy(noop); |
| 34 | + |
| 35 | + hook({ |
| 36 | + append: [ |
| 37 | + new Through({processor: processor}), |
| 38 | + ], |
| 39 | + }); |
39 | 40 |
|
40 |
| - it('should return an empty result', () => { |
41 |
| - const tokens = require('awesome-theme/oceanic.css'); |
42 |
| - equal(Object.keys(tokens).length, 0); |
| 41 | + require('awesome-theme/oceanic.css'); |
43 | 42 | });
|
| 43 | + |
| 44 | + it('processor should be called', () => |
| 45 | + ok(processor.called)); |
44 | 46 | });
|
45 | 47 |
|
46 |
| - describe('providing nothing should reset preProcess', () => { |
47 |
| - before(() => hook()); |
| 48 | + describe('"use" should set the new list of plugins', () => { |
| 49 | + let appendProcessor; |
| 50 | + let prependProcessor; |
| 51 | + let useProcessor; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + appendProcessor = spy(noop); |
| 55 | + prependProcessor = spy(noop); |
| 56 | + useProcessor = spy(noop); |
| 57 | + |
| 58 | + hook({ |
| 59 | + append: [ |
| 60 | + new Through({processor: appendProcessor}), |
| 61 | + ], |
| 62 | + prepend: [ |
| 63 | + new Through({processor: prependProcessor}), |
| 64 | + ], |
| 65 | + use: [ |
| 66 | + new Through({processor: useProcessor}), |
| 67 | + ], |
| 68 | + }); |
48 | 69 |
|
49 |
| - it('should return the "color" token', () => { |
50 |
| - const tokens = require('awesome-theme/oceanic.css'); |
51 |
| - ok(tokens.color); |
| 70 | + require('awesome-theme/oceanic.css'); |
52 | 71 | });
|
| 72 | + |
| 73 | + it('plugin added by append option should not be called', () => |
| 74 | + ok(!appendProcessor.called)); |
| 75 | + |
| 76 | + it('plugin added by prepend option should not be called', () => |
| 77 | + ok(!prependProcessor.called)); |
| 78 | + |
| 79 | + it('plugin added by use option should be called', () => |
| 80 | + ok(useProcessor.called)); |
53 | 81 | });
|
54 | 82 | });
|
55 | 83 |
|
56 |
| - describe('processCss()', () => { |
57 |
| - let providedCSS; |
58 |
| - let providedFilename; |
| 84 | + describe('setting custom processors for the CSS', () => { |
| 85 | + describe('"preprocessCss()" should transform CSS before the PostCSS', () => { |
| 86 | + let processor; |
| 87 | + let providedFilename; |
| 88 | + let providedContent; |
59 | 89 |
|
60 |
| - beforeEach(() => { |
61 |
| - const spy = (content, filename) => { |
62 |
| - providedCSS = content; |
63 |
| - providedFilename = filename; |
64 |
| - return content; |
65 |
| - }; |
| 90 | + beforeEach(() => { |
| 91 | + processor = spy((content, filename) => { |
| 92 | + providedFilename = filename; |
| 93 | + providedContent = content; |
| 94 | + return ''; |
| 95 | + }); |
66 | 96 |
|
67 |
| - hook({processCss: spy}); |
68 |
| - require('awesome-theme/oceanic.css'); |
69 |
| - }); |
| 97 | + hook({preprocessCss: processor}); |
| 98 | + require('awesome-theme/oceanic.css'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('processor should be called', () => |
| 102 | + ok(processor.called)); |
70 | 103 |
|
71 |
| - it('content of file should be provided', () => { |
72 |
| - equal(typeof providedCSS, 'string'); |
73 |
| - ok(providedCSS.length); |
| 104 | + it('filename should be provided', () => |
| 105 | + equal(providedFilename, require.resolve('awesome-theme/oceanic.css'))); |
| 106 | + |
| 107 | + it('content of the file should be provided', () => |
| 108 | + ok(providedContent.length)); |
74 | 109 | });
|
75 | 110 |
|
76 |
| - it('filename should be provided', () => { |
77 |
| - equal(providedFilename, require.resolve('awesome-theme/oceanic.css')); |
| 111 | + describe('"processCss()" should transform CSS after the PostCSS', () => { |
| 112 | + let processor; |
| 113 | + let providedFilename; |
| 114 | + let providedContent; |
| 115 | + |
| 116 | + beforeEach(() => { |
| 117 | + processor = spy((content, filename) => { |
| 118 | + providedFilename = filename; |
| 119 | + providedContent = content; |
| 120 | + return ''; |
| 121 | + }); |
| 122 | + |
| 123 | + hook({processCss: processor}); |
| 124 | + require('awesome-theme/oceanic.css'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('processor should be called', () => |
| 128 | + ok(processor.called)); |
| 129 | + |
| 130 | + it('filename should be provided', () => |
| 131 | + equal(providedFilename, require.resolve('awesome-theme/oceanic.css'))); |
| 132 | + |
| 133 | + it('content of the file should be provided', () => |
| 134 | + ok(providedContent.length)); |
78 | 135 | });
|
79 | 136 | });
|
80 | 137 | });
|
0 commit comments