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