Open History to see the last commit
1import { expect } from '@playwright/test'
2import { test } from '@tanstack/router-e2e-utils'
3β
4/**
5 * These tests verify the <ClientOnly> compiler optimization.
6 *
7 * The WindowSize component accesses `window` at module scope, which would
8 * throw if the module is ever imported on the server. The compiler optimization
9 * strips <ClientOnly> children from the server bundle, allowing DCE to remove
10 * the WindowSize import entirely. This prevents the server from crashing.
11 *
12 * If these tests pass in SSR mode, it proves the compiler is correctly
13 * removing client-only code from the server bundle.
14 */
15β
16test('ClientOnly renders fallback on server, then client content after hydration', async ({
17 page,
18}) => {
19 // Navigate directly to the client-only route
20 // If the compiler optimization isn't working, this would crash the server
21 // because WindowSize.tsx accesses `window` at module scope
22 await page.goto('/client-only')
23 await page.waitForURL('/client-only')
24β
25 // The heading should be visible
26 await expect(page.getByTestId('client-only-heading')).toContainText(
27 'Client Only Demo',
28 )
29β
30 // After hydration, the WindowSize component should render with actual values
31 // Wait for the client-side component to render
32 await expect(page.getByTestId('window-size')).toBeVisible()
33 await expect(page.getByTestId('window-width')).toContainText('Window width:')
34 await expect(page.getByTestId('window-height')).toContainText(
35 'Window height:',
36 )
37})
38β
39test('ClientOnly works with client-side navigation', async ({ page }) => {
40 // Start from home
41 await page.goto('/')
42 await page.waitForURL('/')
43β
44 // Navigate to client-only route via client-side navigation
45 await page.getByRole('link', { name: 'Client Only' }).click()
46 await page.waitForURL('/client-only')
47β
48 // The WindowSize component should render
49 await expect(page.getByTestId('window-size')).toBeVisible()
50 await expect(page.getByTestId('window-width')).toContainText('Window width:')
51})
52β
53test('ClientOnly component displays actual window dimensions', async ({
54 page,
55}) => {
56 // Set a specific viewport size
57 await page.setViewportSize({ width: 800, height: 600 })
58β
59 await page.goto('/client-only')
60 await page.waitForURL('/client-only')
61β
62 // Wait for client-side hydration
63 await expect(page.getByTestId('window-size')).toBeVisible()
64β
65 // Check that the displayed dimensions match the viewport
66 await expect(page.getByTestId('window-width')).toContainText(
67 'Window width: 800',
68 )
69 await expect(page.getByTestId('window-height')).toContainText(
70 'Window height: 600',
71 )
72})