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

shared-css.prod.spec.ts

94 lines2.8 KB

1import { expect } from '@playwright/test'
2import { test } from '@tanstack/router-e2e-utils'
3import type { Page } from '@playwright/test'
4​
5test.skip(
6 process.env.VITE_CSS_CODE_SPLIT === 'false',
7 'Route stylesheet isolation requires CSS code splitting',
8)
9test.use({ javaScriptEnabled: false })
10​
11async function getStylesheetContent(page: Page) {
12 const hrefs = await page
13 .locator('link[rel="stylesheet"]')
14 .evaluateAll((links: Array<HTMLLinkElement>) =>
15 links.map((link) => link.href),
16 )
17 expect(hrefs.length).toBeGreaterThan(0)
18 const stylesheets = await Promise.all(
19 hrefs.map(async (href) => {
20 const response = await page.request.get(href)
21 expect(response.ok()).toBe(true)
22 return response.text()
23 }),
24 )
25 return stylesheets.join('\n')
27​
28for (const route of ['alpha', 'beta']) {
29 test(`${route} has complete and isolated CSS without JavaScript`, async ({
30 page,
31 }) => {
32 await page.goto(`/${route}`)
33​
34 const header = page.getByTestId('shared-header')
35 await expect(header).toHaveCSS('display', 'grid')
36 await expect(header).toHaveCSS('height', '64px')
37 await expect(page.getByTestId('route-page')).toHaveCSS(
38 `--repro-${route}`,
39 '1',
40 )
41​
42 const css = await getStylesheetContent(page)
43 expect(css).toContain('--repro-shared-header')
44 expect(css).toContain(`--repro-${route}`)
45 expect(css).not.toContain(`--repro-${route === 'alpha' ? 'beta' : 'alpha'}`)
46 })
48​
49function getScriptPreloadHrefs(html: string) {
50 return Array.from(html.matchAll(/<link\b[^>]*>/g), (match) => match[0])
51 .filter(
52 (tag) =>
53 /\brel="modulepreload"/.test(tag) ||
54 (/\brel="preload"/.test(tag) && /\bas="script"/.test(tag)),
55 )
56 .map((tag) => tag.match(/\bhref="([^"]+)"/)?.[1])
57 .filter((href): href is string => !!href)
59​
60test('alpha preloads the shared header chunk and the chunk it depends on', async ({
61 page,
62}) => {
63 const response = await page.request.get('/alpha')
64 expect(response.ok()).toBe(true)
65​
66 const hrefs = getScriptPreloadHrefs(await response.text())
67 expect(hrefs.length).toBeGreaterThan(0)
68​
69 const scripts = await Promise.all(
70 hrefs.map(async (href) => {
71 const script = await page.request.get(href)
72 expect(script.ok()).toBe(true)
73 return script.text()
74 }),
75 )
76​
77 expect(scripts.some((code) => code.includes('shared-header'))).toBe(true)
78 expect(scripts.some((code) => code.includes('repro-format-title'))).toBe(true)
79})
80​
81test('home does not load the shared header or route styles', async ({
82 page,
83}) => {
84 await page.goto('/')
85 await expect(page.getByTestId('global-styled')).toHaveCSS(
86 'background-color',
87 'rgb(59, 130, 246)',
88 )
89​
90 const css = await getStylesheetContent(page)
91 expect(css).not.toContain('--repro-shared-header')
92 expect(css).not.toContain('--repro-alpha')
93 expect(css).not.toContain('--repro-beta')
94})