-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
263 lines (234 loc) Β· 6.44 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
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
const postcss = require('postcss');
const test = require('ava');
const plugin = require('./');
function run(t, input, output, opts = {}, warnings = 0) {
return postcss([plugin(opts)])
.process(input)
.then((result) => {
t.deepEqual(result.css, output);
t.deepEqual(result.warnings().length, warnings);
});
}
test('Chain ancestor in a simple case', (t) => {
return run(t, '.a{ &:hover{ ^&-b{} } }', '.a{ &:hover{ .a-b{} } }', {});
});
test('Prepend ancestor in a simple case', (t) => {
return run(t, '.a{ &:hover{ ^& .b{} } }', '.a{ &:hover{ .a .b{} } }', {});
});
test('Replace ancestor selectors in sibling rules', (t) => {
return run(
t,
'.a{ &:hover{ ^&-b1{} ^&-b2{} ^& .b3{} } }',
'.a{ &:hover{ .a-b1{} .a-b2{} .a .b3{} } }',
{}
);
});
test('Chain 2 ancestors in double selector', (t) => {
return run(
t,
'.a{ &:hover{ ^&-b, ^&-c{} } }',
'.a{ &:hover{ .a-b, .a-c{} } }',
{}
);
});
test('Prepend 2 ancestors in double selector', (t) => {
return run(
t,
'.a{ &:hover{ ^& .b, ^& .c{} } }',
'.a{ &:hover{ .a .b, .a .c{} } }',
{}
);
});
test('Return empty string when pointing to a non-existent ancestor', (t) => {
return run(
t,
'.a{ &-b{ &-c{ ^^^^&-d{} } } }',
'.a{ &-b{ &-c{ -d{} } } }',
{},
1
);
});
test('Process 2 nested ancestor selectors', (t) => {
return run(
t,
'.a{ &-b{ &-c{ ^^&-d{ &-e{ ^^^^&-f{ } } } } } }',
'.a{ &-b{ &-c{ .a-d{ &-e{ .a-f{ } } } } } }',
{}
);
});
test('Replace with root comment', (t) => {
return run(
t,
'/* This is a comment */ .a{ &:hover{ ^& .b, ^& .c{} } }',
'/* This is a comment */ .a{ &:hover{ .a .b, .a .c{} } }',
{}
);
});
test('Replace with nested comment', (t) => {
return run(
t,
'.a{ &:hover{ /* This is a comment */ ^& .b, ^& .c{} } }',
'.a{ &:hover{ /* This is a comment */ .a .b, .a .c{} } }',
{}
);
});
// replaceDeclarations option
test('Replace declaration values (I)', (t) => {
return run(
t,
'.a{ &:hover { &:before { content: "^&"; } } }',
'.a{ &:hover { &:before { content: ".a:hover"; } } }',
{ replaceDeclarations: true }
);
});
test('Replace declaration values (II)', (t) => {
return run(
t,
'.a{ &:hover { &:before { content: "^^&.foo"; } } }',
'.a{ &:hover { &:before { content: ".a.foo"; } } }',
{ replaceDeclarations: true }
);
});
test('Replace declaration values with multiple parent selector', (t) => {
return run(
t,
'.a1,.a2{ &:hover { &:before { content: "^^&"; } } }',
'.a1,.a2{ &:hover { &:before { content: ".a1,.a2"; } } }',
{ replaceDeclarations: true }
);
});
test('Replace ancestors at different nesting levels', (t) => {
return run(
t,
'.a{ &:hover{ ^&-b{} } .c{ .d{ ^&-e{} } } .z{} }',
'.a{ &:hover{ .a-b{} } .c{ .d{ .a .c-e{} } } .z{} }',
{}
);
});
test('Replace ancestors with 4 different hierarchy levels (1 exceeding)', (t) => {
return run(
t,
'.a{ &-b{ &-c{ &-d{} ^&-d,^&-d{} ^^&-d{} ^^^&-d{} } } }',
'.a{ &-b{ &-c{ &-d{} .a-b-d,.a-b-d{} .a-d{} -d{} } } }',
{},
1
);
});
test('Process nested ancestor close to > , + and ~ selectors', (t) => {
return run(
t,
'.a{ &-b{ > ^&-c{} + ^&-d{} ~ ^&-e{} } }',
'.a{ &-b{ > .a-c{} + .a-d{} ~ .a-e{} } }',
{}
);
});
test('Replace default ancestor selector with "Β£%"', (t) => {
return run(
t,
'.a{ &-b{ &-c{ ££%-d{ £££%-f{ } } } } }',
'.a{ &-b{ &-c{ .a-d{ .a-f{ } } } } }',
{ placeholder: 'Β£%' }
);
});
test('Replace default ancestor with custom levelSymbol and parentSymbol', (t) => {
return run(
t,
'.a{ &-b{ &-c{ foofoobar-d{ foofoofoobar-f{ } } } } }',
'.a{ &-b{ &-c{ .a-d{ .a-f{ } } } } }',
{ levelSymbol: 'foo', parentSymbol: 'bar' }
);
});
// Complex nesting
test('Generate comma separated selectors when root element has multiple selectors', (t) => {
return run(
t,
'.a1,.a2{ .b{ &:hover{ ^& .c{} } } }',
'.a1,.a2{ .b{ &:hover{ .a1 .b .c,.a2 .b .c{} } } }',
{}
);
});
test('Generate comma separated selectors when root element has multiple selectors + "&" selector', (t) => {
return run(
t,
'.a1,.a2{ &-b{ &:hover{ ^&-c{} } } }',
'.a1,.a2{ &-b{ &:hover{ .a1-b-c,.a2-b-c{} } } }',
{}
);
});
test('Generate comma separated selectors when two ancestors have multiple selectors', (t) => {
return run(
t,
'.a1,.a2{ .b1,.b2{ &:hover{ ^& .c{} } } }',
'.a1,.a2{ .b1,.b2{ &:hover{ .a1 .b1 .c,.a2 .b1 .c,.a1 .b2 .c,.a2 .b2 .c{} } } }',
{}
);
});
test('Generate comma separated selectors when two ancestors have multiple selectors + "&" selectors', (t) => {
return run(
t,
'.a1,.a2{ &-b1,&-b2{ &:hover{ ^&-c{} } } }',
'.a1,.a2{ &-b1,&-b2{ &:hover{ .a1-b1-c,.a2-b1-c,.a1-b2-c,.a2-b2-c{} } } }',
{}
);
});
test('Generate comma separated selectors when two ancestors have multiple selectors + one "&" selector', (t) => {
return run(
t,
'.a1,.a2{ .b1,&-b2{ &:hover{ ^&-c{} } } }',
'.a1,.a2{ .b1,&-b2{ &:hover{ .a1 .b1-c,.a2 .b1-c,.a1-b2-c,.a2-b2-c{} } } }',
{}
);
});
test('Do not consider media queries when building ancestor selectors', (t) => {
return run(
t,
'.a{ &-b{ &-c{ @media (max-width: 320px){ ^&-d{} &-d{} } } } }',
'.a{ &-b{ &-c{ @media (max-width: 320px){ .a-b-d{} &-d{} } } } }',
{}
);
});
// Ancestor selector repeated in same rule
test('Use same ancestor selector twice in same rule', (t) => {
return run(
t,
'.a{ &:hover{ ^&^& .b{} ^&^&-b{} } }',
'.a{ &:hover{ .a.a .b{} .a.a-b{} } }',
{},
2
);
});
test('Use same ancestor selector twice in same rule inside multiple selector rule (I)', (t) => {
return run(
t,
'.a1,.a2{ &:hover{ ^&^&-b{} } }',
'.a1,.a2{ &:hover{ .a1.a1-b,.a2.a2-b{} } }',
{},
1
);
});
test('Use same ancestor selector twice in same rule inside multiple selector rule (II)', (t) => {
return run(
t,
'.a1,.a2{ &-b1,&-b2{ &:hover{ ^&^&-c{} } } }',
'.a1,.a2{ &-b1,&-b2{ &:hover{ .a1-b1.a1-b1-c,.a2-b1.a2-b1-c,.a1-b2.a1-b2-c,.a2-b2.a2-b2-c{} } } }',
{},
1
);
});
test.failing('Use two different ancestor placeholders in same rule', (t) => {
return run(
t,
'.a{ &-b{ &:hover{ ^&^^&-c{} } } }',
'.a{ &-b{ &:hover{ .a-b.a-c{} } } }',
{},
1
);
});
test('Alternative to having two different ancestor placeholders in same rule', (t) => {
return run(
t,
'.a{ &-b{ &:hover{ ^& { &^^^&-c{} } } } }',
'.a{ &-b{ &:hover{ .a-b { &.a-c{} } } } }',
{}
);
});