Open History to see the last commit
1import { ErrorBoundary, Suspense } from 'solid-js'
2import { describe, expect, it, vi } from 'vitest'
3import { renderToStream, renderToStringAsync } from 'solid-js/web'
4import { Await } from '../../src/awaited'
5β
6describe('Await (server)', () => {
7 it('flushes blank content while an Await without a fallback is pending', async () => {
8 let resolvePromise!: (value: string) => void
9 const promise = new Promise<string>((resolve) => {
10 resolvePromise = resolve
11 })
12 const records: Array<string> = []
13 const completed = renderToStream(() => (
14 <html>
15 <body>
16 <Await promise={promise}>{(value) => <span>{value}</span>}</Await>
17 </body>
18 </html>
19 )).pipeTo(
20 new WritableStream<Uint8Array>({
21 write(chunk) {
22 records.push(new TextDecoder().decode(chunk))
23 },
24 }),
25 )
26β
27 await vi.waitFor(() => expect(records.length).toBeGreaterThan(0))
28 expect(records.join('')).not.toContain('resolved')
29β
30 resolvePromise('resolved')
31 await completed
32 expect(records.join('')).toContain('resolved')
33 })
34β
35 it('renders an error boundary when an Await without a fallback rejects', async () => {
36 const html = await renderToStringAsync(() => (
37 <Suspense fallback={<span>outer pending</span>}>
38 <ErrorBoundary fallback={(error) => <span>{error.message}</span>}>
39 <Suspense fallback={<span>inner pending</span>}>
40 <Await promise={Promise.reject(new Error('rejected'))}>
41 {(value) => <span>{value}</span>}
42 </Await>
43 </Suspense>
44 </ErrorBoundary>
45 </Suspense>
46 ))
47β
48 expect(html).toContain('rejected')
49 expect(html).not.toContain('pending')
50 })
51β
52 it.each([
53 ['zero', 0],
54 ['false', false],
55 ['empty string', ''],
56 ['null', null],
57 ['undefined', undefined],
58 ])('renders a resolved %s value', async (_name, value) => {
59 const children = vi.fn(() => <span>resolved</span>)
60β
61 const html = await renderToStringAsync(() => (
62 <Await promise={Promise.resolve(value)} fallback={<span>pending</span>}>
63 {children}
64 </Await>
65 ))
66β
67 expect(children).toHaveBeenCalledOnce()
68 expect(children).toHaveBeenCalledWith(value)
69 expect(html).toContain('resolved')
70 expect(html).not.toContain('pending')
71 })
72})