1- const path = require ( 'path' ) ;
1+ const utils = require ( './utils' ) ;
2+ const { COLORS , onBeforeAll, assert} = utils ;
23
3- async function getComputedStyle ( element , pseudoElement = null ) {
4- return page . evaluate ( ( element , pseudoElement ) => {
5- const style = window . getComputedStyle ( element , pseudoElement ) ;
6- return JSON . parse ( JSON . stringify ( style ) ) ;
7- } , element , pseudoElement ) ;
8- }
4+ describe ( 'Classes' , ( ) => {
5+ beforeAll ( onBeforeAll ) ;
96
10- async function getClasses ( element ) {
11- return page . evaluate ( ( element ) => element . className , element ) ;
12- }
13-
14- async function sleep ( ms ) {
15- return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
16- }
17-
18- const COLORS = {
19- red : 'rgb(255, 0, 0)' ,
20- green : 'rgb(0, 128, 0)' ,
21- blue : 'rgb(0, 0, 255)' ,
22- } ;
23-
24- const EXAMPLES = {
25- first : '.example' ,
26- second : '.example:nth-of-type(2)'
27- }
28-
29- describe ( 'CSS-in-JS-in-HTML process' , ( ) => {
30-
31- beforeAll ( async ( ) => {
32- const url = 'file://' + path . join ( __dirname , 'index.html' ) ;
33-
34- await page . goto ( url ) ;
35- await page . addScriptTag ( { path : path . join ( __dirname , '..' , 'build' , 'index.min.js' ) } ) ;
36-
37- await page . addScriptTag ( {
38- content : `CSS_IN_JS_IN_HTML.init(document, null);`
39- } ) ;
40- } ) ;
41-
42- describe ( 'Classes only' , ( ) => {
43- it ( 'class "background-[red]" should be "background" as "red"' , async ( ) => {
44- const element = await page . evaluate ( async ( ) => {
45- const element = document . querySelector ( '#example' ) ;
46- element . className = 'background-[red]' ;
47- await Promise . resolve ( setTimeout ( ( ) => { } , 1000 ) ) ;
48- return JSON . parse ( JSON . stringify ( getComputedStyle ( element ) ) ) ;
49- } ) ;
50- expect ( element . backgroundColor ) . toBe ( COLORS . red ) ;
51- } ) ;
52-
53- it ( 'class "background-[green] color-[blue]" should be "background" as "green" and "color" as "blue"' , async ( ) => {
54- const element = await page . evaluate ( async ( ) => {
55- const element = document . querySelector ( '#example' ) ;
56- element . className = 'background-[green] color-[blue]' ;
57- await Promise . resolve ( setTimeout ( ( ) => { } , 1000 ) ) ;
58- return JSON . parse ( JSON . stringify ( getComputedStyle ( element ) ) ) ;
59- } ) ;
60- expect ( element . backgroundColor ) . toBe ( COLORS . green ) ;
61- expect ( element . color ) . toBe ( COLORS . blue ) ;
62- } ) ;
7+ it ( 'Simple class' , async ( ) => {
8+ await assert ( '#example' , 'background-[red]' , { backgroundColor : COLORS . red } ) ;
639 } ) ;
6410
65- describe ( 'Selectors + Classes' , ( ) => {
66- it ( 'class "[ul]:display-[flex]" should be "display" as "flex" for every "ul" element in child tree' , async ( ) => {
67- const is_every_ul_flex = await page . evaluate ( async ( ) => {
68- const element = document . querySelector ( '#example-second' ) ;
69- element . className = '[ul]:display-[flex]' ;
70- await Promise . resolve ( setTimeout ( ( ) => { } , 1000 ) ) ;
71- const every_ul = element . querySelectorAll ( 'ul' ) ;
72- const check_every_ul = Array . from ( every_ul ) . every ( ( ul ) => {
73- return ul . style . display === 'flex' ;
74- } ) ;
75- return check_every_ul ;
76- } ) ;
77- expect ( is_every_ul_flex ) . toBe ( true ) ;
11+ it ( 'Multiple classes' , async ( ) => {
12+ await assert ( '#example' , 'background-[red] color-[white]' , {
13+ backgroundColor : COLORS . red ,
14+ color : COLORS . white
7815 } ) ;
7916 } ) ;
8017} ) ;
0 commit comments