2021-03-30 16:11:21 +00:00
import { suite } from 'uvu' ;
import * as assert from 'uvu/assert' ;
2021-04-03 02:44:23 +00:00
import { scopeRule } from '../lib/compiler/optimize/postcss-scoped-styles/index.js' ;
2021-03-30 16:11:21 +00:00
const ScopedStyles = suite ( 'Astro PostCSS Scoped Styles Plugin' ) ;
2021-04-03 02:44:23 +00:00
const className = 'astro-abcd1234' ;
2021-03-30 16:11:21 +00:00
2021-04-03 02:44:23 +00:00
ScopedStyles ( 'Scopes rules correctly' , ( ) => {
// Note: assume all selectors have no unnecessary spaces (i.e. must be minified)
const tests = {
'.class' : ` .class. ${ className } ` ,
h1 : ` h1. ${ className } ` ,
'.nav h1' : ` .nav. ${ className } h1. ${ className } ` ,
'.class+.class' : ` .class. ${ className } +.class. ${ className } ` ,
'.class~:global(a)' : ` .class. ${ className } ~a ` ,
'.class *' : ` .class. ${ className } . ${ className } ` ,
'.class>*' : ` .class. ${ className } >. ${ className } ` ,
'.class :global(*)' : ` .class. ${ className } * ` ,
'.class :global(.nav:not(.is-active))' : ` .class. ${ className } .nav:not(.is-active) ` , // preserve nested parens
'.class :global(ul li)' : ` .class. ${ className } ul li ` , // allow doubly-scoped selectors
'.class:not(.is-active)' : ` .class. ${ className } :not(.is-active) ` , // Note: the :not() selector can NOT contain multiple classes, so this is correct; if this causes issues for some people then it‘ s worth a discussion
'body h1' : ` body h1. ${ className } ` , // body shouldn‘ t be scoped; it‘ s not a component
from : 'from' , // ignore keyframe keywords (below)
to : 'to' ,
'55%' : '55%' ,
} ;
2021-03-30 16:11:21 +00:00
for ( const [ given , expected ] of Object . entries ( tests ) ) {
2021-04-03 02:44:23 +00:00
assert . equal ( scopeRule ( given , className ) , expected ) ;
2021-03-30 16:11:21 +00:00
}
} ) ;
ScopedStyles . run ( ) ;