@@ -3,40 +3,78 @@ import { preprocess } from '../utils';
3
3
4
4
describe ( 'transformer - globalRule' , ( ) => {
5
5
it ( 'wraps selector in :global(...) modifier' , async ( ) => {
6
- const template = `<style>@ global{ div{color:red}.test{} }</style>` ;
6
+ const template = `<style>: global div{color:red}:global .test{}</style>` ;
7
7
const opts = autoProcess ( ) ;
8
8
const preprocessed = await preprocess ( template , opts ) ;
9
9
expect ( preprocessed . toString ( ) ) . toContain (
10
10
`:global(div){color:red}:global(.test){}` ,
11
11
) ;
12
12
} ) ;
13
13
14
- it ( 'wraps selector in :global(...) modifier only inside the rule ' , async ( ) => {
15
- const template = `<style>@ global{div{color:red}}.test {}</style>` ;
14
+ it ( 'wraps selector in :global(...) only if needed ' , async ( ) => {
15
+ const template = `<style>: global .test{}:global :global(.foo) {}</style>` ;
16
16
const opts = autoProcess ( ) ;
17
17
const preprocessed = await preprocess ( template , opts ) ;
18
18
expect ( preprocessed . toString ( ) ) . toContain (
19
- `:global(div){color:red}.test {}` ,
19
+ `:global(.test){}:global(.foo) {}` ,
20
20
) ;
21
21
} ) ;
22
22
23
- it ( 'wraps selector in :global(...) only if needed ' , async ( ) => {
24
- const template = ` <style>@global{.test{} :global(.foo){}} </style>` ;
23
+ it ( 'wraps selector in :global(...) on multiple levels ' , async ( ) => {
24
+ const template = ' <style>:global div .cls{} </style>' ;
25
25
const opts = autoProcess ( ) ;
26
26
const preprocessed = await preprocess ( template , opts ) ;
27
- expect ( preprocessed . toString ( ) ) . toContain (
28
- `:global(.test){}:global(.foo){}` ,
27
+ expect ( preprocessed . toString ( ) ) . toMatch (
28
+ // either be :global(div .cls){}
29
+ // or :global(div) :global(.cls){}
30
+ / ( : g l o b a l \( d i v .c l s \) \{ \} | : g l o b a l \( d i v \) : g l o b a l \( \. c l s \) \{ \} ) / ,
29
31
) ;
30
32
} ) ;
31
33
32
- it ( "prefixes @keyframes names with '-global-' only if needed" , async ( ) => {
33
- const template = `<style>
34
- @global{@keyframes a {from{} to{}}@keyframes -global-b {from{} to{}}}
35
- </style>` ;
34
+ it ( 'wraps selector in :global(...) on multiple levels when in the middle' , async ( ) => {
35
+ const template = '<style>div :global span .cls{}</style>' ;
36
36
const opts = autoProcess ( ) ;
37
37
const preprocessed = await preprocess ( template , opts ) ;
38
- expect ( preprocessed . toString ( ) ) . toContain (
39
- `@keyframes -global-a {from{} to{}}@keyframes -global-b {from{} to{}}` ,
38
+ expect ( preprocessed . toString ( ) ) . toMatch (
39
+ // either be div :global(span .cls) {}
40
+ // or div :global(span) :global(.cls) {}
41
+ / d i v ( : g l o b a l \( s p a n .c l s \) \{ \} | : g l o b a l \( s p a n \) : g l o b a l \( \. c l s \) \{ \} ) / ,
42
+ ) ;
43
+ } ) ;
44
+
45
+ it ( 'does not break when at the end' , async ( ) => {
46
+ const template = '<style>span :global{}</style>' ;
47
+ const opts = autoProcess ( ) ;
48
+ const preprocessed = await preprocess ( template , opts ) ;
49
+ expect ( preprocessed . toString ( ) ) . toContain ( 'span{}' ) ;
50
+ } ) ;
51
+
52
+ it ( 'works with collapsed nesting several times' , async ( ) => {
53
+ const template = '<style>div :global span :global .cls{}</style>' ;
54
+ const opts = autoProcess ( ) ;
55
+ const preprocessed = await preprocess ( template , opts ) ;
56
+ expect ( preprocessed . toString ( ) ) . toMatch (
57
+ // either be div :global(span .cls) {}
58
+ // or div :global(span) :global(.cls) {}
59
+ / d i v ( : g l o b a l \( s p a n .c l s \) \{ \} | : g l o b a l \( s p a n \) : g l o b a l \( \. c l s \) \{ \} ) / ,
60
+ ) ;
61
+ } ) ;
62
+
63
+ it ( 'does not interfere with the :global(...) syntax' , async ( ) => {
64
+ const template = '<style>div :global(span){}</style>' ;
65
+ const opts = autoProcess ( ) ;
66
+ const preprocessed = await preprocess ( template , opts ) ;
67
+ expect ( preprocessed . toString ( ) ) . toContain ( 'div :global(span){}' ) ;
68
+ } ) ;
69
+
70
+ it ( 'allows mixing with the :global(...) syntax' , async ( ) => {
71
+ const template = '<style>div :global(span) :global .cls{}</style>' ;
72
+ const opts = autoProcess ( ) ;
73
+ const preprocessed = await preprocess ( template , opts ) ;
74
+ expect ( preprocessed . toString ( ) ) . toMatch (
75
+ // either be div :global(span .cls) {}
76
+ // or div :global(span) :global(.cls) {}
77
+ / d i v ( : g l o b a l \( s p a n .c l s \) \{ \} | : g l o b a l \( s p a n \) : g l o b a l \( \. c l s \) \{ \} ) / ,
40
78
) ;
41
79
} ) ;
42
80
} ) ;
0 commit comments