Open History to see the last commit
1import { ErrorBoundary, Suspense, createContext, useContext } from 'solid-js'
2import { renderToString, renderToStringAsync } from 'solid-js/web'
3import { afterEach, describe, expect, it, vi } from 'vitest'
4import { lazyRouteComponent } from '../../src/lazyRouteComponent'
5β
6afterEach(() => {
7 vi.unstubAllEnvs()
8})
9β
10describe('lazyRouteComponent (server)', () => {
11 it.each([
12 { exportName: undefined, preload: false },
13 { exportName: 'Missing', preload: false },
14 { exportName: undefined, preload: true },
15 { exportName: 'Missing', preload: true },
16 ])(
17 'renders a missing $exportName export as blank in production (preload: $preload)',
18 async ({ exportName, preload }) => {
19 vi.stubEnv('NODE_ENV', 'production')
20 const importer = vi.fn(
21 (): Promise<Record<string, () => null>> =>
22 Promise.resolve({
23 Other: () => null,
24 }),
25 )
26 const Page = lazyRouteComponent(importer, exportName)
27 const onError = vi.fn()
28 const onUnhandledRejection = vi.fn()
29 process.on('unhandledRejection', onUnhandledRejection)
30β
31 try {
32 if (preload) {
33 await Page.preload?.()
34 }
35β
36 const html = await renderToStringAsync(
37 () => (
38 <ErrorBoundary
39 fallback={(error) => {
40 onError(error)
41 return <span>Render failed</span>
42 }}
43 >
44 <Suspense fallback={<span>Pending</span>}>
45 <Page />
46 </Suspense>
47 </ErrorBoundary>
48 ),
49 { timeoutMs: 1_000 },
50 )
51 // Promise rejection events are dispatched after the current microtasks.
52 await new Promise<void>((resolve) => setImmediate(resolve))
53β
54 // Solid can retain hydration scripts and boundary comments without
55 // rendering content for the missing component.
56 const content = html
57 .replace(/<script\b[^>]*>[\s\S]*?<\/script>/g, '')
58 .replace(/<!--[\s\S]*?-->/g, '')
59 expect(content).toBe('')
60 expect(importer).toHaveBeenCalledOnce()
61 expect(onError).not.toHaveBeenCalled()
62 expect(onUnhandledRejection).not.toHaveBeenCalled()
63 } finally {
64 process.off('unhandledRejection', onUnhandledRejection)
65 }
66 },
67 )
68β
69 it('renders a preloaded component synchronously and reuses its import', async () => {
70 const importer = vi.fn(() =>
71 Promise.resolve({
72 default: (props: { label: string }) => <span>{props.label}</span>,
73 }),
74 )
75 const Page = lazyRouteComponent(importer)
76 const preload = Page.preload?.()
77 await preload
78β
79 const html = renderToString(() => (
80 <Suspense fallback={<span>Pending</span>}>
81 <Page label="Preloaded page" />
82 </Suspense>
83 ))
84β
85 expect(html).toContain('Preloaded page')
86 expect(html).not.toContain('Pending')
87 expect(Page.preload?.()).toBe(preload)
88 expect(importer).toHaveBeenCalledOnce()
89 })
90β
91 it('renders synchronously after an awaited retry of a failed import', async () => {
92 const failure = new Error('component download failed')
93 const Content = (props: { label: string }) => <span>{props.label}</span>
94 const importer = vi
95 .fn<() => Promise<{ default: typeof Content }>>()
96 .mockRejectedValueOnce(failure)
97 .mockResolvedValueOnce({ default: Content })
98 const Page = lazyRouteComponent(importer)
99β
100 await Page.preload?.()
101 expect(() => renderToString(() => <Page label="First attempt" />)).toThrow(
102 failure,
103 )
104β
105 const retry = Page.preload?.()
106 await retry
107 const html = renderToString(() => <Page label="Recovered page" />)
108β
109 expect(html).toContain('Recovered page')
110 expect(Page.preload?.()).toBe(retry)
111 expect(importer).toHaveBeenCalledTimes(2)
112 })
113β
114 it.each([false, true])(
115 'preserves provider context and component props (preload: %s)',
116 async (preload) => {
117 const Context = createContext('missing context')
118 const importer = vi.fn(() =>
119 Promise.resolve({
120 Page: (props: { label: string; children: string }) => (
121 <span>
122 {useContext(Context)}:{props.label}:{props.children}
123 </span>
124 ),
125 }),
126 )
127 const Page = lazyRouteComponent(importer, 'Page')
128 if (preload) {
129 await Page.preload?.()
130 }
131β
132 const html = await renderToStringAsync(() => (
133 <Context.Provider value="provided context">
134 <Suspense fallback={<span>Pending</span>}>
135 <Page label="page prop">child prop</Page>
136 </Suspense>
137 </Context.Provider>
138 ))
139β
140 expect(html).toContain('provided context')
141 expect(html).toContain('page prop')
142 expect(html).toContain('child prop')
143 expect(html).not.toContain('missing context')
144 expect(html).not.toContain('Pending')
145 expect(importer).toHaveBeenCalledOnce()
146 },
147 )
148})