Open History to see the last commit
1import { describe, expect, test } from 'vitest'
2import { act, render, screen } from '@testing-library/react'
3import ReactDOMServer from 'react-dom/server'
4β
5import {
6 HeadContent,
7 Outlet,
8 RouterProvider,
9 createMemoryHistory,
10 createRootRoute,
11 createRoute,
12 createRouter,
13} from '../src'
14import { Scripts } from '../src/Scripts'
15β
16describe('ssr scripts', () => {
17 test('it works', async () => {
18 const rootRoute = createRootRoute({
19 head: () => {
20 return {
21 scripts: [
22 {
23 src: 'script.js',
24 },
25 {
26 src: 'script2.js',
27 },
28 ],
29 }
30 },
31 component: () => {
32 return (
33 <div>
34 <div data-testid="root">root</div>
35 <Outlet />
36 <Scripts />
37 </div>
38 )
39 },
40 })
41β
42 const indexRoute = createRoute({
43 path: '/',
44 getParentRoute: () => rootRoute,
45 head: () => {
46 return {
47 scripts: [
48 {
49 src: 'script3.js',
50 },
51 ],
52 }
53 },
54 })
55β
56 const router = createRouter({
57 history: createMemoryHistory({
58 initialEntries: ['/'],
59 }),
60 routeTree: rootRoute.addChildren([indexRoute]),
61 isServer: true,
62 })
63β
64 await router.load()
65β
66 expect(router.state.matches.map((d) => d.headScripts).flat(1)).toEqual([
67 { src: 'script.js' },
68 { src: 'script2.js' },
69 { src: 'script3.js' },
70 ])
71 })
72β
73 test('excludes `undefined` script values', async () => {
74 const rootRoute = createRootRoute({
75 scripts: () => [
76 { src: 'script.js' },
77 undefined, // 'script2.js' opted out by certain conditions, such as `NODE_ENV=production`.
78 ],
79 component: () => {
80 return (
81 <div>
82 <div data-testid="root">root</div>
83 <Outlet />
84 <Scripts />
85 </div>
86 )
87 },
88 })
89β
90 const indexRoute = createRoute({
91 path: '/',
92 getParentRoute: () => rootRoute,
93 scripts: () => [{ src: 'script3.js' }],
94 component: () => {
95 return <div data-testid="index">index</div>
96 },
97 })
98β
99 const router = createRouter({
100 history: createMemoryHistory({
101 initialEntries: ['/'],
102 }),
103 routeTree: rootRoute.addChildren([indexRoute]),
104 isServer: true,
105 })
106β
107 await router.load()
108β
109 expect(router.state.matches.map((d) => d.scripts).flat(1)).toEqual([
110 { src: 'script.js' },
111 undefined,
112 { src: 'script3.js' },
113 ])
114β
115 const { container } = await act(() =>
116 render(<RouterProvider router={router} />),
117 )
118 expect(await screen.findByTestId('root')).toBeInTheDocument()
119 expect(await screen.findByTestId('index')).toBeInTheDocument()
120β
121 expect(container.innerHTML).toEqual(
122 `<div><div data-testid="root">root</div><div data-testid="index">index</div><script src="script.js"></script><script src="script3.js"></script></div>`,
123 )
124 })
125})
126β
127describe('ssr HeadContent', () => {
128 test('derives title, dedupes meta, and allows non-loader HeadContent', async () => {
129 const rootRoute = createRootRoute({
130 loader: () =>
131 new Promise((r) => setTimeout(r, 1)).then(() => ({
132 description: 'Root',
133 })),
134 head: ({ loaderData }) => {
135 return {
136 meta: [
137 {
138 title: 'Root',
139 },
140 {
141 name: 'description',
142 content: loaderData?.description,
143 },
144 {
145 name: 'image',
146 content: 'image.jpg',
147 },
148 {
149 property: 'og:image',
150 content: 'root-image.jpg',
151 },
152 {
153 property: 'og:description',
154 content: 'Root description',
155 },
156 ],
157 }
158 },
159 component: () => {
160 return <HeadContent />
161 },
162 })
163β
164 const indexRoute = createRoute({
165 path: '/',
166 getParentRoute: () => rootRoute,
167 loader: () =>
168 new Promise((r) => setTimeout(r, 2)).then(() => ({
169 description: 'Index',
170 })),
171 head: ({ loaderData }) => {
172 return {
173 meta: [
174 {
175 title: 'Index',
176 },
177 {
178 name: 'description',
179 content: loaderData?.description,
180 },
181 {
182 name: 'last-modified',
183 content: '2021-10-10',
184 },
185 {
186 property: 'og:image',
187 content: 'index-image.jpg',
188 },
189 ],
190 }
191 },
192 })
193β
194 const router = createRouter({
195 history: createMemoryHistory({
196 initialEntries: ['/'],
197 }),
198 routeTree: rootRoute.addChildren([indexRoute]),
199 isServer: true,
200 })
201β
202 await router.load()
203β
204 expect(router.state.matches.map((d) => d.meta).flat(1)).toEqual([
205 { title: 'Root' },
206 { name: 'description', content: 'Root' },
207 { name: 'image', content: 'image.jpg' },
208 { property: 'og:image', content: 'root-image.jpg' },
209 { property: 'og:description', content: 'Root description' },
210 { title: 'Index' },
211 { name: 'description', content: 'Index' },
212 { name: 'last-modified', content: '2021-10-10' },
213 { property: 'og:image', content: 'index-image.jpg' },
214 ])
215β
216 const html = ReactDOMServer.renderToString(
217 <RouterProvider router={router} />,
218 )
219 expect(html).toEqual(
220 `<title>Index</title><meta name="image" content="image.jpg"/><meta property="og:description" content="Root description"/><meta name="description" content="Index"/><meta name="last-modified" content="2021-10-10"/><meta property="og:image" content="index-image.jpg"/>`,
221 )
222 })
223})