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

ClientOnly.test.tsx

102 lines2.9 KB

1import { afterEach, describe, expect, it, vi } from 'vitest'
2import React from 'react'
3import ReactDOMServer from 'react-dom/server'
4import { act, cleanup, render, screen } from '@testing-library/react'
5import {
6 RouterProvider,
7 createMemoryHistory,
8 createRootRoute,
9 createRoute,
10 createRouter,
11} from '../src'
12import { ClientOnly } from '../src/ClientOnly'
13​
14afterEach(() => {
15 vi.resetAllMocks()
16 cleanup()
17})
18​
19function createTestRouter(opts: { isServer: boolean }) {
20 const history = createMemoryHistory({ initialEntries: ['/'] })
21​
22 const rootRoute = createRootRoute({})
23​
24 const indexRoute = createRoute({
25 getParentRoute: () => rootRoute,
26 path: '/',
27 component: () => (
28 <div>
29 <p>Index Route</p>
30 <ClientOnly fallback={<div data-testid="loading">Loading...</div>}>
31 <div data-testid="client-only-content">Client Only Content</div>
32 </ClientOnly>
33 </div>
34 ),
35 })
36 const otherRoute = createRoute({
37 getParentRoute: () => rootRoute,
38 path: '/other',
39 component: () => (
40 <div>
41 <p data-testid="other-route">Other Route</p>
42 </div>
43 ),
44 })
45​
46 const routeTree = rootRoute.addChildren([indexRoute, otherRoute])
47 const router = createRouter({ routeTree, history, ...opts })
48​
49 return {
50 router,
51 routes: { indexRoute },
52 }
54​
55describe('ClientOnly', () => {
56 it('should render fallback during SSR', async () => {
57 const { router } = createTestRouter({ isServer: true })
58 await router.load()
59​
60 // Initial render (SSR)
61 const html = ReactDOMServer.renderToString(
62 <RouterProvider router={router} />,
63 )
64 expect(html).include('Loading...')
65 expect(html).not.include('Client Only Content')
66 })
67​
68 it('should render client content after hydration', async () => {
69 const { router } = createTestRouter({ isServer: false })
70 await router.load()
71​
72 // Mock useSyncExternalStore to simulate hydration
73 vi.spyOn(React, 'useSyncExternalStore').mockImplementation(() => true)
74​
75 render(<RouterProvider router={router} />)
76​
77 expect(await screen.findByTestId('client-only-content')).toBeInTheDocument()
78 expect(screen.queryByText('Loading...')).not.toBeInTheDocument()
79 })
80​
81 it('should handle navigation with client-only content', async () => {
82 const { router } = createTestRouter({ isServer: false })
83 await router.load()
84​
85 // Simulate hydration
86 vi.spyOn(React, 'useSyncExternalStore').mockImplementation(() => true)
87​
88 // Re-render after hydration
89 render(<RouterProvider router={router} />)
90​
91 expect(await screen.findByTestId('client-only-content')).toBeInTheDocument()
92​
93 // Navigate to a different route and back
94 await act(() => router.navigate({ to: '/other' }))
95 expect(await screen.findByTestId('other-route')).toBeInTheDocument()
96​
97 await act(() => router.navigate({ to: '/' }))
98​
99 // Content should still be visible after navigation
100 expect(await screen.findByTestId('client-only-content')).toBeInTheDocument()
101 })
102})