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

css.spec.ts

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