Skip to content

Commit 1c6772f

Browse files
committed
added tests for the public API
1 parent adec8b0 commit 1c6772f

File tree

3 files changed

+145
-54
lines changed

3 files changed

+145
-54
lines changed

test/plugins.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { equal } from 'assert';
22
import { identity } from 'lodash';
3+
import { dropCache } from '../utils/sugar';
34
import hook from '../src';
45

56
import ExtractImports from 'postcss-modules-extract-imports';
67
import LocalByDefault from 'postcss-modules-local-by-default';
78
import Scope from 'postcss-modules-scope';
89

910
describe('plugins', () => {
10-
beforeEach(() => {
11-
// clearing cache
12-
delete require.cache[require.resolve('awesome-theme/oceanic.css')];
13-
});
11+
afterEach(() => dropCache('awesome-theme/oceanic.css'));
1412

1513
describe('custom generateScopedName() function', () => {
1614
before(() => hook({generateScopedName: identity}));

test/public-api.js

Lines changed: 107 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,137 @@
1-
import { constant } from 'lodash';
1+
import { noop } from 'lodash';
22
import { equal, ok } from 'assert';
3+
import { dropCache, spy, Through } from '../utils/sugar';
34
import hook from '../src';
45

56
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'));
108

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;
1512

1613
beforeEach(() => {
17-
const spy = (content, filename) => {
18-
providedCSS = content;
19-
providedFilename = filename;
20-
return content;
21-
};
14+
processor = spy(noop);
2215

23-
hook({preprocessCss: spy});
24-
require('awesome-theme/oceanic.css');
25-
});
16+
hook({
17+
prepend: [
18+
new Through({processor: processor}),
19+
],
20+
});
2621

27-
it('content of file should be provided', () => {
28-
equal(typeof providedCSS, 'string');
29-
ok(providedCSS.length);
22+
require('awesome-theme/oceanic.css');
3023
});
3124

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));
3527
});
3628

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+
});
3940

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');
4342
});
43+
44+
it('processor should be called', () =>
45+
ok(processor.called));
4446
});
4547

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+
});
4869

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');
5271
});
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));
5381
});
5482
});
5583

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;
5989

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+
});
6696

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));
70103

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));
74109
});
75110

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));
78135
});
79136
});
80137
});

utils/sugar.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { identity } from 'lodash';
2+
import { plugin } from 'postcss';
3+
4+
/**
5+
* @param {string} moduleName
6+
*/
7+
export function dropCache(moduleName) {
8+
delete require.cache[require.resolve(moduleName)];
9+
}
10+
11+
/**
12+
* @param {function} fn
13+
* @return {function}
14+
*/
15+
export function spy(fn) {
16+
const wrapper = function wrapper() {
17+
wrapper.called = true;
18+
wrapper.times++;
19+
return fn.apply(this, arguments);
20+
};
21+
22+
wrapper.called = false;
23+
wrapper.times = 0;
24+
25+
return wrapper;
26+
}
27+
28+
/**
29+
* Simple PostCSS plugin for the test purpose
30+
* @param {object} opts
31+
* @return {Function}
32+
*/
33+
export const Through = plugin('through', function through(opts = {}) {
34+
const processor = opts.processor || identity;
35+
return css => processor(css);
36+
});

0 commit comments

Comments
 (0)