Skip to content

Commit f12ac8d

Browse files
committed
test(solid-router): cover Link head asset stability
1 parent 7b8f7e9 commit f12ac8d

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

packages/solid-router/tests/Scripts.test.tsx

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
import {
1111
HeadContent,
12+
Link,
1213
Outlet,
1314
RouterProvider,
1415
createBrowserHistory,
@@ -116,6 +117,96 @@ describe('ssr scripts', () => {
116117
'<script src="script.js"></script><script src="script3.js"></script>',
117118
)
118119
})
120+
121+
test('keeps manifest stylesheet links mounted when navigating with Link', async () => {
122+
const history = createBrowserHistory()
123+
124+
try {
125+
const rootRoute = createRootRoute({
126+
component: () => {
127+
return (
128+
<>
129+
<HeadContent />
130+
<Outlet />
131+
</>
132+
)
133+
},
134+
})
135+
136+
const indexRoute = createRoute({
137+
path: '/',
138+
getParentRoute: () => rootRoute,
139+
component: () => <Link to="/about">Go to about page</Link>,
140+
})
141+
142+
const aboutRoute = createRoute({
143+
path: '/about',
144+
getParentRoute: () => rootRoute,
145+
component: () => <div>About</div>,
146+
})
147+
148+
const router = createRouter({
149+
history,
150+
routeTree: rootRoute.addChildren([indexRoute, aboutRoute]),
151+
})
152+
153+
router.ssr = {
154+
manifest: {
155+
routes: {
156+
[rootRoute.id]: {
157+
assets: [
158+
{
159+
tag: 'link',
160+
attrs: {
161+
rel: 'stylesheet',
162+
href: '/main.css',
163+
},
164+
},
165+
],
166+
},
167+
},
168+
},
169+
} as any
170+
171+
await router.load()
172+
173+
render(() => <RouterProvider router={router} />)
174+
175+
const getStylesheetLink = () =>
176+
Array.from(
177+
document.head.querySelectorAll('link[rel="stylesheet"]'),
178+
).find((link) => link.getAttribute('href') === '/main.css')
179+
180+
await waitFor(() => {
181+
expect(getStylesheetLink()).toBeInstanceOf(HTMLLinkElement)
182+
})
183+
184+
const initialLink = getStylesheetLink()
185+
expect(initialLink).toBeInstanceOf(HTMLLinkElement)
186+
187+
fireEvent.click(screen.getByRole('link', { name: 'Go to about page' }))
188+
189+
await waitFor(() => {
190+
expect(router.state.location.pathname).toBe('/about')
191+
})
192+
193+
expect(getStylesheetLink()).toBe(initialLink)
194+
expect(
195+
Array.from(
196+
document.head.querySelectorAll('link[rel="stylesheet"]'),
197+
).filter((link) => link.getAttribute('href') === '/main.css'),
198+
).toHaveLength(1)
199+
} finally {
200+
history.destroy()
201+
document.head
202+
.querySelectorAll('link[rel="stylesheet"]')
203+
.forEach((link) => {
204+
if (link.getAttribute('href') === '/main.css') {
205+
link.remove()
206+
}
207+
})
208+
}
209+
})
119210
})
120211

121212
describe('ssr HeadContent', () => {

0 commit comments

Comments
 (0)