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

css.dev.spec.ts

236 lines7.9 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',
11test.skip(process.env.MODE === 'prod', 'Dev-only repro')
12​
13test.describe('CSS styles in SSR (dev mode)', () => {
14 test.use({ whitelistErrors })
15​
16 // Helper to build full URL from baseURL and path
17 // Playwright's goto with absolute paths (like '/modules') ignores baseURL's path portion
18 // So we need to manually construct the full URL
19 const buildUrl = (baseURL: string, path: string) => {
20 return baseURL.replace(/\/$/, '') + path
21 }
22​
23 test.describe('with JavaScript disabled', () => {
24 test.use({ javaScriptEnabled: false, whitelistErrors })
25​
26 test('global CSS is applied on initial page load', async ({
27 page,
28 baseURL,
29 }) => {
30 await page.goto(buildUrl(baseURL!, '/'))
31​
32 const element = page.getByTestId('global-styled')
33 await expect(element).toBeVisible()
34​
35 // Verify the CSS is applied by checking computed styles
36 // #3b82f6 (blue-500) in RGB is rgb(59, 130, 246)
37 const backgroundColor = await element.evaluate(
38 (el) => getComputedStyle(el).backgroundColor,
39 )
40 expect(backgroundColor).toBe('rgb(59, 130, 246)')
41​
42 const padding = await element.evaluate(
43 (el) => getComputedStyle(el).padding,
44 )
45 expect(padding).toBe('24px')
46​
47 const borderRadius = await element.evaluate(
48 (el) => getComputedStyle(el).borderRadius,
49 )
50 expect(borderRadius).toBe('12px')
51 })
52​
53 test('CSS modules are applied on initial page load', async ({
54 page,
55 baseURL,
56 }) => {
57 await page.goto(buildUrl(baseURL!, '/modules'))
58​
59 const card = page.getByTestId('module-card')
60 await expect(card).toBeVisible()
61​
62 // Verify class is scoped (hashed)
63 const className = await card.getAttribute('class')
64 expect(className).toBeTruthy()
65 expect(className).not.toBe('card')
66 // The class should contain some hash characters (CSS modules add a hash)
67 expect(className!.length).toBeGreaterThan(5)
68​
69 // Verify computed styles from card.module.css
70 // #f0fdf4 (green-50) in RGB is rgb(240, 253, 244)
71 const backgroundColor = await card.evaluate(
72 (el) => getComputedStyle(el).backgroundColor,
73 )
74 expect(backgroundColor).toBe('rgb(240, 253, 244)')
75​
76 const padding = await card.evaluate((el) => getComputedStyle(el).padding)
77 expect(padding).toBe('16px')
78​
79 const borderRadius = await card.evaluate(
80 (el) => getComputedStyle(el).borderRadius,
81 )
82 expect(borderRadius).toBe('8px')
83 })
84​
85 test('global CSS class names are NOT scoped', async ({ page, baseURL }) => {
86 await page.goto(buildUrl(baseURL!, '/'))
87​
88 const element = page.getByTestId('global-styled')
89 await expect(element).toBeVisible()
90​
91 // Get the class attribute - it should be the plain class name (not hashed)
92 const className = await element.getAttribute('class')
93 expect(className).toBe('global-container')
94 })
95​
96 test('Sass mixin styles are applied on initial page load', async ({
97 page,
98 baseURL,
99 }) => {
100 await page.goto(buildUrl(baseURL!, '/sass-mixin'))
101​
102 const element = page.getByTestId('mixin-styled')
103 await expect(element).toBeVisible()
104​
105 // Verify the mixin is applied (display: flex from center-mixin)
106 const display = await element.evaluate(
107 (el) => getComputedStyle(el).display,
108 )
109 expect(display).toBe('flex')
110​
111 const justifyContent = await element.evaluate(
112 (el) => getComputedStyle(el).justifyContent,
113 )
114 expect(justifyContent).toBe('center')
115​
116 const alignItems = await element.evaluate(
117 (el) => getComputedStyle(el).alignItems,
118 )
119 expect(alignItems).toBe('center')
120​
121 // Verify other styles from mixin-consumer.scss
122 // #a855f7 (purple-500) in RGB is rgb(168, 85, 247)
123 const backgroundColor = await element.evaluate(
124 (el) => getComputedStyle(el).backgroundColor,
125 )
126 expect(backgroundColor).toBe('rgb(168, 85, 247)')
127​
128 const padding = await element.evaluate(
129 (el) => getComputedStyle(el).padding,
130 )
131 expect(padding).toBe('24px')
132 })
133​
134 test('CSS with quoted content is fully extracted', async ({
135 page,
136 baseURL,
137 }) => {
138 await page.goto(buildUrl(baseURL!, '/quotes'))
139​
140 // Verify the element using CSS with content:"..." is styled
141 const quoteElement = page.getByTestId('quote-styled')
142 await expect(quoteElement).toBeVisible()
143​
144 // #ef4444 (red-500) in RGB is rgb(239, 68, 68)
145 const quoteBackgroundColor = await quoteElement.evaluate(
146 (el) => getComputedStyle(el).backgroundColor,
147 )
148 expect(quoteBackgroundColor).toBe('rgb(239, 68, 68)')
149​
150 // Verify styles AFTER the quoted content are also extracted
151 // This is the key test - the regex bug would cut off CSS at the first quote
152 const afterQuoteElement = page.getByTestId('after-quote-styled')
153 await expect(afterQuoteElement).toBeVisible()
154​
155 // #f59e0b (amber-500) in RGB is rgb(245, 158, 11)
156 const afterQuoteBackgroundColor = await afterQuoteElement.evaluate(
157 (el) => getComputedStyle(el).backgroundColor,
158 )
159 expect(afterQuoteBackgroundColor).toBe('rgb(245, 158, 11)')
160 })
161 })
162​
163 test('styles persist after hydration', async ({ page, baseURL }) => {
164 await page.goto(buildUrl(baseURL!, '/'))
165​
166 // Wait for hydration and styles to be applied
167 const element = page.getByTestId('global-styled')
168 await expect(element).toBeVisible()
169​
170 // Wait for CSS to be applied (background color should not be transparent)
171 await expect(async () => {
172 const backgroundColor = await element.evaluate(
173 (el) => getComputedStyle(el).backgroundColor,
174 )
175 expect(backgroundColor).toBe('rgb(59, 130, 246)')
176 }).toPass({ timeout: 5000 })
177 })
178​
179 test('CSS modules styles persist after hydration', async ({
180 page,
181 baseURL,
182 }) => {
183 await page.goto(buildUrl(baseURL!, '/modules'))
184​
185 // Wait for hydration and styles to be applied
186 const card = page.getByTestId('module-card')
187 await expect(card).toBeVisible()
188​
189 // Wait for CSS to be applied (background color should not be transparent)
190 await expect(async () => {
191 const backgroundColor = await card.evaluate(
192 (el) => getComputedStyle(el).backgroundColor,
193 )
194 expect(backgroundColor).toBe('rgb(240, 253, 244)')
195 }).toPass({ timeout: 5000 })
196 })
197​
198 test('styles work correctly after client-side navigation', async ({
199 page,
200 baseURL,
201 }) => {
202 // Start from home
203 await page.goto(buildUrl(baseURL!, '/'))
204​
205 // Verify initial styles
206 const globalElement = page.getByTestId('global-styled')
207 await expect(globalElement).toBeVisible()
208 let backgroundColor = await globalElement.evaluate(
209 (el) => getComputedStyle(el).backgroundColor,
210 )
211 expect(backgroundColor).toBe('rgb(59, 130, 246)')
212​
213 // Navigate to modules page
214 await page.getByTestId('nav-modules').click()
215 await page.waitForURL('**/modules')
216​
217 // Verify CSS modules styles
218 const card = page.getByTestId('module-card')
219 await expect(card).toBeVisible()
220 backgroundColor = await card.evaluate(
221 (el) => getComputedStyle(el).backgroundColor,
222 )
223 expect(backgroundColor).toBe('rgb(240, 253, 244)')
224​
225 // Navigate back to home
226 await page.getByTestId('nav-home').click()
227 await page.waitForURL(/\/([^/]*)(\/)?($|\?)/)
228​
229 // Verify global styles still work
230 await expect(globalElement).toBeVisible()
231 backgroundColor = await globalElement.evaluate(
232 (el) => getComputedStyle(el).backgroundColor,
233 )
234 expect(backgroundColor).toBe('rgb(59, 130, 246)')
235 })
236})