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

rsc-react-cache.spec.ts

112 lines3.8 KB

1import { expect } from '@playwright/test'
2import { test } from '@tanstack/router-e2e-utils'
3import { waitForHydration } from './hydration'
4​
5test.describe('RSC React.cache Tests', () => {
6 test('Page loads with React.cache test button', async ({ page }) => {
7 await page.goto('/rsc-react-cache')
8 await page.waitForURL('/rsc-react-cache')
9​
10 // Verify page title
11 await expect(page.getByTestId('rsc-react-cache-title')).toHaveText(
12 'React.cache Demo',
13 )
14​
15 // Verify RSC content is rendered
16 await expect(page.getByTestId('rsc-react-cache-content')).toBeVisible()
17​
18 // Verify test link is present
19 await expect(page.getByTestId('test-cache-link')).toBeVisible()
20 })
21​
22 test('React.cache deduplicates function calls by argument', async ({
23 page,
24 }) => {
25 await page.goto('/rsc-react-cache')
26 await page.waitForURL('/rsc-react-cache')
27​
28 // Click the test link
29 await page.getByTestId('test-cache-link').click()
30 await page.waitForURL(/test-cache/)
31​
32 // Verify the result matches expected pattern:
33 // cacheFnCount = 2 (only 2 unique args: 'test1' and 'test2')
34 // nonCacheFnCount = 3 (all 3 calls run)
35 await expect(page.getByTestId('test-react-cache-result')).toHaveText(
36 '(cacheFnCount = 2, nonCacheFnCount = 3)',
37 )
38 })
39​
40 test('Cache status shows success when working', async ({ page }) => {
41 // First visit base route to reset counters
42 await page.goto('/rsc-react-cache')
43 await page.waitForURL('/rsc-react-cache')
44​
45 // Then navigate to test-cache
46 await page.goto('/rsc-react-cache?test-cache')
47 await page.waitForURL(/test-cache/)
48​
49 // Verify cache status shows success
50 await expect(page.getByTestId('cache-status')).toContainText(
51 'React.cache is working!',
52 )
53​
54 // Verify individual counts
55 await expect(page.getByTestId('cache-fn-count')).toContainText('2')
56 await expect(page.getByTestId('non-cache-fn-count')).toContainText('3')
57 })
58​
59 test('Cached results share the same randomValue for same args', async ({
60 page,
61 }) => {
62 // First visit base route to reset counters
63 await page.goto('/rsc-react-cache')
64 await page.waitForURL('/rsc-react-cache')
65​
66 // Then navigate to test-cache
67 await page.goto('/rsc-react-cache?test-cache')
68 await page.waitForURL(/test-cache/)
69​
70 // Get the random values from cached results
71 // Call 1 and Call 3 both use 'test1' so should have same randomValue
72 const randomValue1 = await page.getByTestId('random-value-1').textContent()
73 const randomValue2 = await page.getByTestId('random-value-2').textContent()
74 const randomValue3 = await page.getByTestId('random-value-3').textContent()
75​
76 // Call 1 ('test1') and Call 3 ('test1') should have same randomValue
77 expect(randomValue1).toBeTruthy()
78 expect(randomValue1).toBe(randomValue3)
79​
80 // Call 2 ('test2') should have different randomValue
81 expect(randomValue2).toBeTruthy()
82 expect(randomValue2).not.toBe(randomValue1)
83 })
84​
85 test('Server timestamp is present', async ({ page }) => {
86 await page.goto('/rsc-react-cache')
87 await page.waitForURL('/rsc-react-cache')
88​
89 await expect(page.getByTestId('rsc-server-timestamp')).toBeVisible()
90 const timestamp = await page
91 .getByTestId('rsc-server-timestamp')
92 .textContent()
93 expect(timestamp).toContain('Rendered:')
94 })
95​
96 test('Client-side navigation to React.cache page works', async ({ page }) => {
97 // Start at home
98 await page.goto('/')
99 await page.waitForURL('/')
100 await waitForHydration(page)
101​
102 // Navigate to React.cache page via nav bar
103 await page.getByTestId('nav-react-cache').click()
104 await page.waitForURL('/rsc-react-cache')
105​
106 // Verify page loaded correctly
107 await expect(page.getByTestId('rsc-react-cache-title')).toHaveText(
108 'React.cache Demo',
109 )
110 await expect(page.getByTestId('test-cache-link')).toBeVisible()
111 })
112})