1-
21import { test , expect } from '@playwright/test' ;
32
4- test . describe ( 'Zero-UI Vite Integration' , ( ) => {
5-
6-
7- test ( 'Browser loads initial theme on body ⚛️' , async ( { page } ) => {
8-
9- await page . goto ( '/' , { waitUntil : 'domcontentloaded' } ) ;
10-
11- // body attribute check + theme = initial theme
12- const theme = await page . getAttribute ( 'body' , 'data-theme' ) ;
13- expect ( theme ) . toBe ( 'light' ) ;
14- } ) ;
15-
16- test ( 'theme toggles on button click' , async ( { page } ) => {
17- await page . goto ( '/' ) ;
18-
19- // Wait for hydration and button
20- const button = page . getByTestId ( 'theme-toggle' ) ;
21- await expect ( button ) . toBeVisible ( ) ;
22-
23- // Wait for container to ensure styles are applied
24- const container = page . getByTestId ( 'theme-container' ) ;
25- await expect ( container ) . toHaveClass ( / t h e m e - l i g h t : / ) ; // initially light
26-
27- // Read initial theme attribute
28- const before = await page . getAttribute ( 'body' , 'data-theme' ) ;
29- console . log ( '🌞 before:' , before ) ;
30-
31- // Click to toggle
32- await button . click ( ) ;
3+ // Table-driven so every toggle runs in its own fresh browser context.
4+ const scenarios = [
5+ { toggle : 'theme-toggle' , attr : 'data-theme' } ,
6+ { toggle : 'theme-toggle-secondary' , attr : 'data-theme-2' } ,
7+ { toggle : 'theme-toggle-3' , attr : 'data-theme-three' } ,
8+ ] ;
339
34- // Wait for DOM to reflect change
35- await page . waitForTimeout ( 100 ) ; // Optional: add debounce if needed
36- const after = await page . getAttribute ( 'body' , 'data-theme' ) ;
37- console . log ( '🌙 after:' , after ) ;
38-
39- // Assertion
40- expect ( before ) . toBe ( 'light' ) ;
41- expect ( after ) . toBe ( 'dark' ) ;
42- } ) ;
43-
44- test ( 'theme2 toggles on button click' , async ( { page } ) => {
45- await page . goto ( '/' ) ;
46-
47- // Wait for hydration and button
48- const button = page . getByTestId ( 'theme-toggle-secondary' ) ;
49- await expect ( button ) . toBeVisible ( ) ;
50-
51- // Wait for container to ensure styles are applied
52- const container = page . getByTestId ( 'theme-container-secondary' ) ;
53- await expect ( container ) . toHaveClass ( / t h e m e - 2 - l i g h t : / ) ; // initially light
54-
55- // Read initial theme attribute
56- const before = await page . getAttribute ( 'body' , 'data-theme-2' ) ;
57- console . log ( '🌞 before:' , before ) ;
58-
59- // Click to toggle
60- await button . click ( ) ;
61-
62- // Wait for DOM to reflect change
63- await page . waitForTimeout ( 100 ) ; // Optional: add debounce if needed
64- const after = await page . getAttribute ( 'body' , 'data-theme-2' ) ;
65- console . log ( '🌙 after:' , after ) ;
66-
67- // Assertion
68- expect ( before ) . toBe ( 'light' ) ;
69- expect ( after ) . toBe ( 'dark' ) ;
70- } ) ;
71-
72- test ( 'theme3 toggles on button click' , async ( { page } ) => {
73- await page . goto ( '/' ) ;
10+ test . describe ( 'Zero-UI Vite Integration' , ( ) => {
11+ for ( const { toggle, attr } of scenarios ) {
12+ test ( `toggles <${ attr } > from light → dark` , async ( { page } ) => {
13+ // 1️⃣ Load fully hydrated page
14+ await page . goto ( '/' , { waitUntil : 'networkidle' } ) ;
15+ console . log ( `✅ page loaded testing ${ attr } from light → dark` ) ;
7416
75- // Wait for hydration and button
76- const button = page . getByTestId ( 'theme-toggle-3' ) ;
77- await expect ( button ) . toBeVisible ( ) ;
17+ const body = page . locator ( 'body' ) ;
18+ const button = page . getByTestId ( toggle ) ;
7819
79- // Wait for container to ensure styles are applied
80- const container = page . getByTestId ( 'theme-container-3' ) ;
81- await expect ( container ) . toHaveClass ( / t h e m e - t h r e e - l i g h t : / ) ; // initially light
8220
83- // Read initial theme attribute
84- const before = await page . getAttribute ( ' body' , 'data-theme-three ' ) ;
85- console . log ( '🌞 before:' , before ) ;
21+ // 2️⃣ Assert initial state
22+ await expect ( body ) . toHaveAttribute ( attr , 'light ' ) ;
23+ await expect ( button ) . toBeVisible ( ( console . log ( `✅ ${ button } is visible` ) ) ) ; // auto-retries until true
8624
87- // Click to toggle
88- await button . click ( ) ;
8925
90- // Wait for DOM to reflect change
91- await page . waitForTimeout ( 100 ) ; // Optional: add debounce if needed
92- const after = await page . getAttribute ( 'body' , 'data-theme-three' ) ;
93- console . log ( '🌙 after:' , after ) ;
26+ // 3️⃣ Action
27+ await button . click ( ( console . log ( `✅ ${ button } clicked` ) ) ) ;
9428
95- // Assertion
96- expect ( before ) . toBe ( 'light' ) ;
97- expect ( after ) . toBe ( 'dark' ) ;
98- } ) ;
99- } ) ;
29+ // 4️⃣ Final state
30+ await expect ( body ) . toHaveAttribute ( attr , 'dark' , ( console . log ( `✅ body attr changes after click` ) ) ) ;
31+ } ) ;
32+ }
33+ } ) ;
0 commit comments