-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
220 lines (203 loc) · 5.15 KB
/
test.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
const expect = require('chai').expect;
const sinon = require('sinon');
const plugin = require('./')
const stripIndent = require('strip-indent');
const stylus = require('stylus');
const placeholders = require('./lib/placeholders');
const cleanup = str => stripIndent(str).trim()
describe('styled-jsx-plugin-stylus', () => {
describe('basic props', () => {
const cases = [
[
'color properties',
`
.red
color red
`,
`
.red {
color: #f00;
}
`
],
[
'nested selectors',
`
.outer-class
.inner-class
color green
display none
`,
`
.outer-class .inner-class {
color: #008000;
display: none;
}
`
],
[
'ampersand & operator',
`
.first
&-second
color green
display none
`,
`
.first-second {
color: #008000;
display: none;
}
`
],
[
'dynamic variables in selectors',
`
%%styled-jsx-placeholder-0%%
%%styled-jsx-placeholder-1%%
background red
`,
`
%%styled-jsx-placeholder-0%%,
%%styled-jsx-placeholder-1%% {
background: #f00;
}
`
],
[
'dynamic variables in pseudo selectors',
`
h1
&:nth-child(%%styled-jsx-placeholder-0%%)
background red
`,
`
h1:nth-child(%%styled-jsx-placeholder-0%%) {
background: #f00;
}
`
],
[
'dynamic variables in values interpolation',
`
h1
background red
padding %%styled-jsx-placeholder-0%% %%styled-jsx-placeholder-1%%px
margin-top %%styled-jsx-placeholder-2%%px
`,
`
h1 {
background: #f00;
padding: %%styled-jsx-placeholder-0%% %%styled-jsx-placeholder-1%%px;
margin-top: %%styled-jsx-placeholder-2%%px;
}
`
],
];
cases.forEach(([description, stylus, css]) => {
it(description, () => {
expect(
cleanup(plugin(stylus))
).to.eq(
cleanup(css)
);
});
});
});
describe('with settings', () => {
const inputStylus = cleanup(
`
.my-class
color red
background-color %%styled-jsx-placeholder-0%%
`
);
const outputCss = cleanup(
`
.my-class {
color: #f00;
background-color: %%styled-jsx-placeholder-0%%;
}
`
);
let beforeRender = sinon.spy((stylus, css) => {
stylus.import('fixture.css');
return stylus;
});
let render;
let importer;
let mockStylus = {};
let stylusRenderer;
let opts = {};
let preprocessedCss;
describe('when beforeRender function is provided', () => {
beforeEach(() => {
render = sinon.spy((s, opts) => {
opts.use(mockStylus());
return outputCss;
});
importer = sinon.spy((s) => s);
beforeRender.reset();
opts = {
use: beforeRender
};
mockStylus = (css) => {
return {
render,
'import': importer
}
};
preprocessedCss = plugin(inputStylus, opts, mockStylus());
});
it('returns the correct preprocessed css string', () => {
expect(preprocessedCss.trim()).to.eq(outputCss.trim());
});
it('calls beforeRender function once', () => {
expect(beforeRender.calledOnce).to.eq(true);
});
it('calls render function once', () => {
expect(render.calledOnce).to.eq(true);
});
it('calls import function once', () => {
expect(importer.calledOnce).to.eq(true);
});
it('calls render function with two arguments', () => {
expect(render.args[0].length).to.eq(2);
});
});
describe('when beforeRender function is not provided', () => {
beforeEach(() => {
render = sinon.spy((s) => outputCss);
importer = sinon.spy((s) => s);
beforeRender.reset();
mockStylus = (css) => {
return {
render,
'import': importer
}
};
opts = {
use: null
};
preprocessedCss = plugin(inputStylus, opts, mockStylus());
});
it('returns the correct preprocessed css string', () => {
expect(preprocessedCss.trim()).to.eq(outputCss.trim());
});
it('calls beforeRender function once', () => {
expect(beforeRender.calledOnce).to.eq(false);
});
it('calls beforeRender with a stylus object', () => {
expect(beforeRender.calledWith(stylus)).to.eq(false);
});
it('calls render function once', () => {
expect(render.calledOnce).to.eq(true);
});
it('calls render function with input css', () => {
expect(
render.calledWith(placeholders.add(inputStylus))
).to.eq(true);
});
});
});
})