Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

css.spec.ts

207 lines6.8 KB

1import { expect } from '@playwright/test'
2import { test } from '@tanstack/router-e2e-utils'
3​
4// Whitelist errors that can occur in CI:
5// - net::ERR_NAME_NOT_RESOLVED: transient network issues
6// - 504 (Outdated Optimize Dep): Vite dependency optimization reload
7const whitelistErrors = [
8 'Failed to load resource: net::ERR_NAME_NOT_RESOLVED',
9 'Failed to load resource: the server responded with a status of 504',
11​
12test.describe('CSS styles in SSR (dev mode)', () => {
13 test.use({ whitelistErrors })
14​
15 // Helper to build full URL from baseURL and path
16 // Playwright's goto with absolute paths (like '/modules') ignores baseURL's path portion
17 // So we need to manually construct the full URL
18 const buildUrl = (baseURL: string, path: string) => {
19 return baseURL.replace(/\/$/, '') + path
20 }
21​
22 test.describe('with JavaScript disabled', () => {
23 test.use({ javaScriptEnabled: false, whitelistErrors })
24​
25 test('global CSS is applied on initial page load', async ({
26 page,
27 baseURL,
28 }) => {
29 await page.goto(buildUrl(baseURL!, '/'))
30​
31 const element = page.getByTestId('global-styled')
32 await expect(element).toBeVisible()
33​
34 // Verify the CSS is applied by checking computed styles
35 // #3b82f6 (blue-500) in RGB is rgb(59, 130, 246)
36 const backgroundColor = await element.evaluate(
37 (el) => getComputedStyle(el).backgroundColor,
38 )
39 expect(backgroundColor).toBe('rgb(59, 130, 246)')
40​
41 const padding = await element.evaluate(
42 (el) => getComputedStyle(el).padding,
43 )
44 expect(padding).toBe('24px')
45​
46 const borderRadius = await element.evaluate(
47 (el) => getComputedStyle(el).borderRadius,
48 )
49 expect(borderRadius).toBe('12px')
50 })
51​
52 test('CSS modules are applied on initial page load', async ({
53 page,
54 baseURL,
55 }) => {
56 await page.goto(buildUrl(baseURL!, '/modules'))
57​
58 const card = page.getByTestId('module-card')
59 await expect(card).toBeVisible()
60​
61 // Verify class is scoped (hashed)
62 const className = await card.getAttribute('class')
63 expect(className).toBeTruthy()
64 expect(className).not.toBe('card')
65 // The class should contain some hash characters (CSS modules add a hash)
66 expect(className!.length).toBeGreaterThan(5)
67​
68 // Verify computed styles from card.module.css
69 // #f0fdf4 (green-50) in RGB is rgb(240, 253, 244)
70 const backgroundColor = await card.evaluate(
71 (el) => getComputedStyle(el).backgroundColor,
72 )
73 expect(backgroundColor).toBe('rgb(240, 253, 244)')
74​
75 const padding = await card.evaluate((el) => getComputedStyle(el).padding)
76 expect(padding).toBe('16px')
77​
78 const borderRadius = await card.evaluate(
79 (el) => getComputedStyle(el).borderRadius,
80 )
81 expect(borderRadius).toBe('8px')
82 })
83​
84 test('global CSS class names are NOT scoped', async ({ page, baseURL }) => {
85 await page.goto(buildUrl(baseURL!, '/'))
86​
87 const element = page.getByTestId('global-styled')
88 await expect(element).toBeVisible()
89​
90 // Get the class attribute - it should be the plain class name (not hashed)
91 const className = await element.getAttribute('class')
92 expect(className).toBe('global-container')
93 })
94​
95 test('Sass mixin styles are applied on initial page load', async ({
96 page,
97 baseURL,
98 }) => {
99 await page.goto(buildUrl(baseURL!, '/sass-mixin'))
100​
101 const element = page.getByTestId('mixin-styled')
102 await expect(element).toBeVisible()
103​
104 // Verify the mixin is applied (display: flex from center-mixin)
105 const display = await element.evaluate(
106 (el) => getComputedStyle(el).display,
107 )
108 expect(display).toBe('flex')
109​
110 const justifyContent = await element.evaluate(
111 (el) => getComputedStyle(el).justifyContent,
112 )
113 expect(justifyContent).toBe('center')
114​
115 const alignItems = await element.evaluate(
116 (el) => getComputedStyle(el).alignItems,
117 )
118 expect(alignItems).toBe('center')
119​
120 // Verify other styles from mixin-consumer.scss
121 // #a855f7 (purple-500) in RGB is rgb(168, 85, 247)
122 const backgroundColor = await element.evaluate(
123 (el) => getComputedStyle(el).backgroundColor,
124 )
125 expect(backgroundColor).toBe('rgb(168, 85, 247)')
126​
127 const padding = await element.evaluate(
128 (el) => getComputedStyle(el).padding,
129 )
130 expect(padding).toBe('24px')
131 })
132 })
133​
134 test('styles persist after hydration', async ({ page, baseURL }) => {
135 await page.goto(buildUrl(baseURL!, '/'))
136​
137 // Wait for hydration and styles to be applied
138 const element = page.getByTestId('global-styled')
139 await expect(element).toBeVisible()
140​
141 // Wait for CSS to be applied (background color should not be transparent)
142 await expect(async () => {
143 const backgroundColor = await element.evaluate(
144 (el) => getComputedStyle(el).backgroundColor,
145 )
146 expect(backgroundColor).toBe('rgb(59, 130, 246)')
147 }).toPass({ timeout: 5000 })
148 })
149​
150 test('CSS modules styles persist after hydration', async ({
151 page,
152 baseURL,
153 }) => {
154 await page.goto(buildUrl(baseURL!, '/modules'))
155​
156 // Wait for hydration and styles to be applied
157 const card = page.getByTestId('module-card')
158 await expect(card).toBeVisible()
159​
160 // Wait for CSS to be applied (background color should not be transparent)
161 await expect(async () => {
162 const backgroundColor = await card.evaluate(
163 (el) => getComputedStyle(el).backgroundColor,
164 )
165 expect(backgroundColor).toBe('rgb(240, 253, 244)')
166 }).toPass({ timeout: 5000 })
167 })
168​
169 test('styles work correctly after client-side navigation', async ({
170 page,
171 baseURL,
172 }) => {
173 // Start from home
174 await page.goto(buildUrl(baseURL!, '/'))
175​
176 // Verify initial styles
177 const globalElement = page.getByTestId('global-styled')
178 await expect(globalElement).toBeVisible()
179 let backgroundColor = await globalElement.evaluate(
180 (el) => getComputedStyle(el).backgroundColor,
181 )
182 expect(backgroundColor).toBe('rgb(59, 130, 246)')
183​
184 // Navigate to modules page
185 await page.getByTestId('nav-modules').click()
186 await page.waitForURL('**/modules')
187​
188 // Verify CSS modules styles
189 const card = page.getByTestId('module-card')
190 await expect(card).toBeVisible()
191 backgroundColor = await card.evaluate(
192 (el) => getComputedStyle(el).backgroundColor,
193 )
194 expect(backgroundColor).toBe('rgb(240, 253, 244)')
195​
196 // Navigate back to home
197 await page.getByTestId('nav-home').click()
198 await page.waitForURL(/\/([^/]*)(\/)?($|\?)/)
199​
200 // Verify global styles still work
201 await expect(globalElement).toBeVisible()
202 backgroundColor = await globalElement.evaluate(
203 (el) => getComputedStyle(el).backgroundColor,
204 )
205 expect(backgroundColor).toBe('rgb(59, 130, 246)')
206 })
207})