-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.test.js
520 lines (470 loc) · 12.2 KB
/
index.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
let { deepStrictEqual, equal } = require('node:assert')
let { test } = require('node:test')
let { join } = require('node:path')
let postcss = require('postcss')
let mixins = require('../')
async function run(input, output, opts) {
let result = await postcss([mixins(opts)]).process(input, { from: undefined })
equal(result.css, output)
equal(result.warnings().length, 0)
return result
}
async function catchError(fn) {
let error
try {
await fn()
} catch (e) {
error = e
}
return error
}
test('throws error on unknown mixin', async () => {
let error = await catchError(() => run('@mixin A'))
equal(error.message, 'postcss-mixins: <css input>:1:1: Undefined mixin A')
})
test('throws error on brackets in mixin', async () => {
let error = await catchError(() => run('@define-mixin a $p {}; @mixin a($p)'))
equal(
error.message,
'postcss-mixins: <css input>:1:24: Remove brackets from mixin. ' +
'Like: @mixin name(1px) → @mixin name 1px'
)
})
test('does not throw error on brackets in at-rules inside function mixins', async () => {
await run(
'@mixin a { @supports (max(0px)) { color: black; } }',
'.a { @supports (max(0px)) { color: black; } }',
{
mixins: {
a() {
return { '.a': { '@mixin-content': {} } }
}
}
}
)
})
test('cans remove unknown mixin on request', async () => {
await run('@mixin A; a{}', 'a{}', { silent: true })
})
test('supports functions mixins', async () => {
await run('a { @mixin color black; }', 'a { color: black; }', {
mixins: {
color(rule, color) {
rule.replaceWith({ prop: 'color', value: color })
}
}
})
})
test('removes mixin at-rule', async () => {
await run('a { @mixin none; }', 'a { }', {
mixins: {
none() {}
}
})
})
test('converts object from function to nodes', async () => {
await run('a { @mixin color black; }', 'a { color: black; }', {
mixins: {
color(rule, color) {
return { color }
}
}
})
})
test('passes undefined on missed parameters', async () => {
await run('a { @mixin test; @mixin test ; }', 'a { }', {
mixins: {
test(rule, param1) {
equal(typeof param1, 'undefined')
return {}
}
}
})
})
test('supports object mixins', async () => {
await run(
'@mixin obj;',
'@media screen {\n b {\n one: 1\n }\n}',
{
mixins: {
obj: {
'@media screen': {
b: {
one: '1'
}
}
}
}
}
)
})
test('supports nested function mixins', async () => {
await run(
'a { color: black; @mixin parent { @mixin child; } }',
'a { color: black; .parent { color: white } }',
{
mixins: {
child() {
return { color: 'white' }
},
parent(mixin) {
let rule = postcss.rule({ selector: '.parent' })
if (mixin.nodes) {
rule.append(mixin.nodes)
}
mixin.replaceWith(rule)
}
}
}
)
})
test('throws on unknown mixin type', async () => {
let error = await catchError(() =>
run('@mixin a', '', {
mixins: {
a: 1
}
})
)
equal(error.message, 'Wrong a mixin type number')
})
test('supports CSS mixins', async () => {
await run(
'@define-mixin black { color: black; } a { @mixin black; }',
'a { color: black; }'
)
})
test('uses variable', async () => {
await run(
'@define-mixin color $color { color: $color $other; } ' +
'a { @mixin color black; }',
'a { color: black $other; }'
)
})
test('supports default value', async () => {
await run(
'@define-mixin c $color: black { color: $color; } a { @mixin c; }',
'a { color: black; }'
)
})
test('supports mixins with content', async () => {
await run(
'@define-mixin m { @media { @mixin-content; } } @mixin m { a {} }',
'@media { a {} }'
)
})
test('supports mixins with declarations content', async () => {
await run(
'@define-mixin m { a: 1; @mixin-content; } .m { @mixin m { b: 2 } }',
'.m { a: 1; b: 2 }'
)
})
test('supports mixins with empty content', async () => {
await run(
'@define-mixin m { a: 1; @mixin-content; } .m { @mixin m; }',
'.m { a: 1; }'
)
})
test('supports mixins with multiple content', async () => {
await run(
'@define-mixin m { @mixin-content; @mixin-content; } ' +
'.m { @mixin m { a: 1 } }',
'.m { a: 1; a: 1 }'
)
})
test('supports object mixins with content', async () => {
await run('@mixin obj { b {} }', 'a { b {}\n}', {
mixins: {
obj: {
a: {
'@mixin-content': {}
}
}
}
})
})
test('uses variables', async () => {
await run(
'@define-mixin m $a, $b: b, $c: c { v: $a $b $c; } @mixin m 1, 2;',
'v: 1 2 c;'
)
})
test('loads mixins from dir', async () => {
let result = await run(
'a { @mixin a 1; @mixin b; @mixin c; @mixin d; @mixin e; }',
'a { a: 1; b: 2; c: 3; d: 4; e: 5; }',
{
mixinsDir: join(__dirname, 'mixins')
}
)
deepStrictEqual(
result.messages.sort((a, b) => a.file && a.file.localeCompare(b.file)),
[
{
file: join(__dirname, 'mixins/a.js'),
parent: '',
type: 'dependency'
},
{
file: join(__dirname, 'mixins/b.json'),
parent: '',
type: 'dependency'
},
{
file: join(__dirname, 'mixins/c.CSS'),
parent: '',
type: 'dependency'
},
{
file: join(__dirname, 'mixins/d.sss'),
parent: '',
type: 'dependency'
},
{
file: join(__dirname, 'mixins/e.pcss'),
parent: '',
type: 'dependency'
},
{
dir: join(__dirname, 'mixins'),
glob: '*.{js,cjs,mjs,json,css,sss,pcss}',
parent: '',
type: 'dir-dependency'
}
]
)
})
test('loads mixins from dir with parent options', async () => {
let parent = join(__dirname, 'a.js')
let result = await run(
'a { @mixin a 1; @mixin b; @mixin c; @mixin d; @mixin e; }',
'a { a: 1; b: 2; c: 3; d: 4; e: 5; }',
{
mixinsDir: join(__dirname, 'mixins'),
parent: join(__dirname, 'a.js')
}
)
deepStrictEqual(
result.messages.sort((a, b) => a.file && a.file.localeCompare(b.file)),
[
{
file: join(__dirname, 'mixins/a.js'),
parent,
type: 'dependency'
},
{
file: join(__dirname, 'mixins/b.json'),
parent,
type: 'dependency'
},
{
file: join(__dirname, 'mixins/c.CSS'),
parent,
type: 'dependency'
},
{
file: join(__dirname, 'mixins/d.sss'),
parent,
type: 'dependency'
},
{
file: join(__dirname, 'mixins/e.pcss'),
parent,
type: 'dependency'
},
{
dir: join(__dirname, 'mixins'),
glob: '*.{js,cjs,mjs,json,css,sss,pcss}',
parent: '',
type: 'dir-dependency'
}
]
)
})
test('loads mixins from dirs', async () => {
await run('a { @mixin a 1; @mixin c; }', 'a { a: 1; c: 3; }', {
mixinsDir: [join(__dirname, 'mixins'), join(__dirname, 'other')]
})
})
test('loads mixins from relative dir', async () => {
await run(
'a { @mixin a 1; @mixin b; @mixin c; @mixin d; @mixin e; }',
'a { a: 1; b: 2; c: 3; d: 4; e: 5; }',
{
mixinsDir: 'test/mixins/'
}
)
})
test('loads mixins from relative dirs', async () => {
await run('a { @mixin a 1; @mixin c; }', 'a { a: 1; c: 3; }', {
mixinsDir: ['test/mixins', 'test/other']
})
})
test('loads mixins from file glob', async () => {
await run('a { @mixin a 1; @mixin b; }', 'a { a: 1; b: 2; }', {
mixinsFiles: join(__dirname, 'mixins', '*.{js,json}')
})
})
test('loads mixins from file globs', async () => {
await run('a { @mixin a 1; @mixin c; }', 'a { a: 1; c: 3; }', {
mixinsFiles: [
join(__dirname, 'mixins', '*.!(json|css)'),
join(__dirname, 'other', '*')
]
})
})
test('loads mixins with dependencies', async () => {
let result = await run('a { @mixin f; }', 'a { g: 5; }', {
mixinsFiles: join(__dirname, 'deps', 'f.js')
})
deepStrictEqual(
result.messages.sort((a, b) => a.file && a.file.localeCompare(b.file)),
[
{
file: join(__dirname, 'deps/f.js'),
parent: '',
type: 'dependency'
},
{
file: join(__dirname, 'deps/g.js'),
parent: join(__dirname, 'deps/f.js'),
type: 'dependency'
}
]
)
})
test('coverts mixins values', async () => {
let processor = postcss(
mixins({
mixins: {
empty() {
return { width: 0 }
}
}
})
)
let result = await processor.process('a{ @mixin empty; }', { from: 'a.css' })
equal(typeof result.root.first.first.value, 'string')
})
test('supports nested mixins', async () => {
await run(
'@define-mixin a $a { a: $a; } ' +
'@define-mixin b $b { @mixin a $b; } ' +
'@mixin b 1;',
'a: 1;'
)
})
test('supports nested mixins in mixin-content', async () => {
await run(
'@define-mixin a { a: 1 } ' +
'@define-mixin b { b { @mixin-content } } ' +
'@mixin b { @mixin a }',
'b { a: 1}'
)
})
test('supports nested mixins on object mixins', async () => {
await run('@define-mixin a { a: a; } @mixin b;', 'a: a;', {
mixins: {
b: {
'@mixin a': {}
}
}
})
})
test('supports default arguments in nested mixins', async () => {
await run(
'@define-mixin a $a: 1 { a: $a } ' +
'@define-mixin b $b { @mixin a $b } ' +
'@mixin b;',
'a: 1;'
)
})
test('works in sync mode on no option', () => {
let input = '@define-mixin a { a: 1 }; @mixin a'
let out = 'a: 1'
equal(postcss(mixins()).process(input, { from: 'a.css' }).css, out)
})
test('has @add-mixin alias', async () => {
await run('@define-mixin a { a: 1 } @add-mixin a', 'a: 1')
})
test('treats single-arg content as a single argument', async () => {
await run(
'@define-mixin a $x, $y { a: $x; b: $y; } ' +
'@mixin a single-arg(1, 2), 3;',
'a: 1, 2;\nb: 3;'
)
})
test('throws error when single-arg does not have start parenthesis', async () => {
let error = await catchError(() =>
run('@define-mixin a $p {}; @mixin a single-arg 1, 2);')
)
equal(
error.message,
'postcss-mixins: <css input>:1:24: ' +
'Content of single-arg must be wrapped in brackets: single-arg 1'
)
})
test('throws error when single-arg does not have end parenthesis', async () => {
let error = await catchError(() =>
run('@define-mixin a $p {}; @mixin a single-arg(1, 2;')
)
equal(
error.message,
'postcss-mixins: <css input>:1:24: ' +
'Content of single-arg must be wrapped in brackets: single-arg(1, 2;'
)
})
test('ignores whitespaces outside of single-arg parentheses', async () => {
await run(
'@define-mixin a $x, $y { a: $x; b: $y; } ' +
'@mixin a single-arg (1, 2) , 3;',
'a: 1, 2;\nb: 3;'
)
})
test('can replace multiple single-arg contents', async () => {
await run(
'@define-mixin a $x, $y { a: $x; b: $y; } ' +
'@mixin a single-arg(1, 2), single-arg(3, 4);',
'a: 1, 2;\nb: 3, 4;'
)
})
test('can replace multiple single-arg contents inside single declaration', async () => {
await run(
'@define-mixin a $x, $y { a: $x, $y; } ' +
'@mixin a single-arg(1, 2), single-arg(3, 4);',
'a: 1, 2, 3, 4;'
)
})
test('can replace single-arg contents with nested parentheses', async () => {
await run(
'@define-mixin a $x { a: $x } ' + '@mixin a single-arg(1, (2), 3);',
'a: 1, (2), 3;'
)
})
test('handles single-arg inside rules', async () => {
await run(
'@define-mixin a $x, $y { .s { a: $x; b: $y; } } ' +
'@mixin a single-arg(1, 2), 3;',
'.s { a: 1, 2; b: 3; }'
)
})
test('passes single-arg to the nested mixin', async () => {
await run(
'@define-mixin a $p { a: $p; } ' +
'@define-mixin b $x, $y { @mixin a $x; b: $y; } ' +
'@mixin b single-arg(1, 2), 3;',
'a: 1, 2;\nb: 3;'
)
})
test('passes single-arg to the nested function mixin', async () => {
await run('@mixin b single-arg(1, 2), 3;', 'a: 1, 2;\nb: 3;', {
mixins: {
a(rule, p) {
return { a: p }
},
b(rule, x, y) {
return { ['@mixin a ' + x]: {}, b: y }
}
}
})
})