1
1
import dedent from 'dedent'
2
+ import postcss from 'postcss'
2
3
import { expect , it } from 'vitest'
3
- import { toCss } from '../../ast'
4
- import * as CSS from '../../css-parser'
5
4
import { migrateAtLayerUtilities } from './migrate-at-layer-utilities'
6
5
7
6
const css = dedent
8
7
9
8
function migrate ( input : string ) {
10
- let ast = CSS . parse ( input )
11
- migrateAtLayerUtilities ( ast )
12
- return toCss ( ast ) . trim ( )
9
+ return postcss ( )
10
+ . use ( migrateAtLayerUtilities ( ) )
11
+ . process ( input , { from : expect . getState ( ) . testPath } )
12
+ . then ( ( result ) => result . css )
13
13
}
14
14
15
- it ( 'should migrate simple `@layer utilities` to `@utility`' , ( ) => {
15
+ it ( 'should migrate simple `@layer utilities` to `@utility`' , async ( ) => {
16
16
expect (
17
- migrate ( css `
17
+ await migrate ( css `
18
18
@layer utilities {
19
19
.foo {
20
20
color : red;
@@ -24,13 +24,14 @@ it('should migrate simple `@layer utilities` to `@utility`', () => {
24
24
) . toMatchInlineSnapshot ( `
25
25
"@utility foo {
26
26
color: red;
27
- }"
27
+ }
28
+ "
28
29
` )
29
30
} )
30
31
31
- it ( 'should migrate simple `@layer utilities` with nesting to `@utility`' , ( ) => {
32
+ it ( 'should migrate simple `@layer utilities` with nesting to `@utility`' , async ( ) => {
32
33
expect (
33
- migrate ( css `
34
+ await migrate ( css `
34
35
@layer utilities {
35
36
.foo {
36
37
color : red;
@@ -48,19 +49,22 @@ it('should migrate simple `@layer utilities` with nesting to `@utility`', () =>
48
49
) . toMatchInlineSnapshot ( `
49
50
"@utility foo {
50
51
color: red;
52
+
51
53
&:hover {
52
54
color: blue;
53
55
}
56
+
54
57
&:focus {
55
58
color: green;
56
59
}
57
- }"
60
+ }
61
+ "
58
62
` )
59
63
} )
60
64
61
- it ( 'should migrate multiple simple `@layer utilities` to `@utility`' , ( ) => {
65
+ it ( 'should migrate multiple simple `@layer utilities` to `@utility`' , async ( ) => {
62
66
expect (
63
- migrate ( css `
67
+ await migrate ( css `
64
68
@layer utilities {
65
69
.foo {
66
70
color : red;
@@ -77,13 +81,46 @@ it('should migrate multiple simple `@layer utilities` to `@utility`', () => {
77
81
}
78
82
@utility bar {
79
83
color: blue;
80
- }"
84
+ }
85
+ "
81
86
` )
82
87
} )
83
88
84
- it ( 'should invert at-rules to make them migrate-able' , ( ) => {
89
+ it ( 'should not migrate Rules inside of Rules to a `@utility`' , async ( ) => {
85
90
expect (
86
- migrate ( css `
91
+ await migrate ( css `
92
+ @layer utilities {
93
+ .foo {
94
+ color : red;
95
+ }
96
+
97
+ .bar {
98
+ color : blue;
99
+
100
+ .baz {
101
+ color : green;
102
+ }
103
+ }
104
+ }
105
+ ` ) ,
106
+ ) . toMatchInlineSnapshot ( `
107
+ "@utility foo {
108
+ color: red;
109
+ }
110
+ @utility bar {
111
+ color: blue;
112
+
113
+ .baz {
114
+ color: green;
115
+ }
116
+ }
117
+ "
118
+ ` )
119
+ } )
120
+
121
+ it ( 'should invert at-rules to make them migrate-able' , async ( ) => {
122
+ expect (
123
+ await migrate ( css `
87
124
@layer utilities {
88
125
@media (min-width : 640px ) {
89
126
.foo {
@@ -97,19 +134,24 @@ it('should invert at-rules to make them migrate-able', () => {
97
134
@media (min-width: 640px) {
98
135
color: red;
99
136
}
100
- }"
137
+ }
138
+ "
101
139
` )
102
140
} )
103
141
104
- it ( 'should migrate at-rules with multiple utilities and invert them' , ( ) => {
142
+ it ( 'should migrate at-rules with multiple utilities and invert them' , async ( ) => {
105
143
expect (
106
- migrate ( css `
144
+ await migrate ( css `
107
145
@layer utilities {
108
146
@media (min-width : 640px ) {
109
147
.foo {
110
148
color : red;
111
149
}
150
+ }
151
+ }
112
152
153
+ @layer utilities {
154
+ @media (min-width : 640px ) {
113
155
.bar {
114
156
color : blue;
115
157
}
@@ -122,17 +164,20 @@ it('should migrate at-rules with multiple utilities and invert them', () => {
122
164
color: red;
123
165
}
124
166
}
167
+
168
+
125
169
@utility bar {
126
170
@media (min-width: 640px) {
127
171
color: blue;
128
172
}
129
- }"
173
+ }
174
+ "
130
175
` )
131
176
} )
132
177
133
- it . skip ( 'should migrate deeply nested at-rules with multiple utilities and invert them' , ( ) => {
178
+ it ( 'should migrate deeply nested at-rules with multiple utilities and invert them' , async ( ) => {
134
179
expect (
135
- migrate ( css `
180
+ await migrate ( css `
136
181
@layer utilities {
137
182
@media (min-width : 640px ) {
138
183
.foo {
@@ -169,19 +214,28 @@ it.skip('should migrate deeply nested at-rules with multiple utilities and inver
169
214
}
170
215
}
171
216
@utility baz {
172
- @media (min-width: 1024px ) {
173
- @media (min-width: 640px ) {
217
+ @media (min-width: 640px ) {
218
+ @media (min-width: 1024px ) {
174
219
color: green;
175
220
}
176
221
}
177
222
}
223
+ @utility qux {
224
+ @media (min-width: 640px) {
225
+ @media (min-width: 1024px) {
226
+ @media (min-width: 1280px) {
227
+ color: yellow;
228
+ }
229
+ }
230
+ }
231
+ }
178
232
"
179
233
` )
180
234
} )
181
235
182
- it ( 'should migrate classes with pseudo elements' , ( ) => {
236
+ it ( 'should migrate classes with pseudo elements' , async ( ) => {
183
237
expect (
184
- migrate ( css `
238
+ await migrate ( css `
185
239
@layer utilities {
186
240
.no-scrollbar ::-webkit-scrollbar {
187
241
display : none;
@@ -193,13 +247,14 @@ it('should migrate classes with pseudo elements', () => {
193
247
&::-webkit-scrollbar {
194
248
display: none;
195
249
}
196
- }"
250
+ }
251
+ "
197
252
` )
198
253
} )
199
254
200
- it . skip ( 'should migrate classes with attribute selectors' , ( ) => {
255
+ it ( 'should migrate classes with attribute selectors' , async ( ) => {
201
256
expect (
202
- migrate ( css `
257
+ await migrate ( css `
203
258
@layer utilities {
204
259
.no-scrollbar [data-checked = '' ] {
205
260
display : none;
@@ -208,16 +263,17 @@ it.skip('should migrate classes with attribute selectors', () => {
208
263
` ) ,
209
264
) . toMatchInlineSnapshot ( `
210
265
"@utility no-scrollbar {
211
- &[data-checked='' ] {
266
+ &[data-checked="" ] {
212
267
display: none;
213
268
}
214
- }"
269
+ }
270
+ "
215
271
` )
216
272
} )
217
273
218
- it . skip ( 'should migrate classes with element selectors' , ( ) => {
274
+ it ( 'should migrate classes with element selectors' , async ( ) => {
219
275
expect (
220
- migrate ( css `
276
+ await migrate ( css `
221
277
@layer utilities {
222
278
.no-scrollbar main {
223
279
display : none;
@@ -229,13 +285,14 @@ it.skip('should migrate classes with element selectors', () => {
229
285
& main {
230
286
display: none;
231
287
}
232
- }"
288
+ }
289
+ "
233
290
` )
234
291
} )
235
292
236
- it . skip ( 'should migrate classes with id selectors' , ( ) => {
293
+ it ( 'should migrate classes with id selectors' , async ( ) => {
237
294
expect (
238
- migrate ( css `
295
+ await migrate ( css `
239
296
@layer utilities {
240
297
.no-scrollbar # main {
241
298
display : none;
@@ -247,13 +304,14 @@ it.skip('should migrate classes with id selectors', () => {
247
304
&#main {
248
305
display: none;
249
306
}
250
- }"
307
+ }
308
+ "
251
309
` )
252
310
} )
253
311
254
- it . skip ( 'should migrate classes with another attached class' , ( ) => {
312
+ it ( 'should migrate classes with another attached class' , async ( ) => {
255
313
expect (
256
- migrate ( css `
314
+ await migrate ( css `
257
315
@layer utilities {
258
316
.no-scrollbar .main {
259
317
display : none;
@@ -265,13 +323,19 @@ it.skip('should migrate classes with another attached class', () => {
265
323
&.main {
266
324
display: none;
267
325
}
268
- }"
326
+ }
327
+ @utility main {
328
+ &.no-scrollbar {
329
+ display: none;
330
+ }
331
+ }
332
+ "
269
333
` )
270
334
} )
271
335
272
- it ( 'should migrate a selector with multiple classes to multiple @utility definitions' , ( ) => {
336
+ it ( 'should migrate a selector with multiple classes to multiple @utility definitions' , async ( ) => {
273
337
expect (
274
- migrate ( css `
338
+ await migrate ( css `
275
339
@layer utilities {
276
340
.foo .bar : hover .baz : focus {
277
341
display : none;
@@ -293,13 +357,14 @@ it('should migrate a selector with multiple classes to multiple @utility definit
293
357
.foo .bar:hover &:focus {
294
358
display: none;
295
359
}
296
- }"
360
+ }
361
+ "
297
362
` )
298
363
} )
299
364
300
- it ( 'should merge `@utility` definitions with the same name' , ( ) => {
365
+ it ( 'should merge `@utility` definitions with the same name' , async ( ) => {
301
366
expect (
302
- migrate ( css `
367
+ await migrate ( css `
303
368
@layer utilities {
304
369
.step {
305
370
counter-increment : step;
@@ -320,29 +385,37 @@ it('should merge `@utility` definitions with the same name', () => {
320
385
@apply ml-[-41px];
321
386
content: counter(step);
322
387
}
323
- }"
388
+ }
389
+
390
+ "
324
391
` )
325
392
} )
326
393
327
- it ( 'should not migrate nested classes inside a selector (e.g.: `:has (…)`)' , ( ) => {
394
+ it ( 'should not migrate nested classes inside a `:not (…)`' , async ( ) => {
328
395
expect (
329
- migrate ( css `
396
+ await migrate ( css `
330
397
@layer utilities {
331
- .foo .bar : has (.baz ) {
398
+ .foo .bar : not (. qux ) : has (.baz ) {
332
399
display : none;
333
400
}
334
401
}
335
402
` ) ,
336
403
) . toMatchInlineSnapshot ( `
337
404
"@utility foo {
338
- & .bar:has(.baz) {
405
+ & .bar:not(.qux): has(.baz) {
339
406
display: none;
340
407
}
341
408
}
342
409
@utility bar {
343
- .foo &:has(.baz) {
410
+ .foo &:not(.qux): has(.baz) {
344
411
display: none;
345
412
}
346
- }"
413
+ }
414
+ @utility baz {
415
+ .foo .bar:not(.qux):has(&) {
416
+ display: none;
417
+ }
418
+ }
419
+ "
347
420
` )
348
421
} )
0 commit comments