Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

suspense-first-load-error.test.tsx

161 lines5.8 KB

1/**
2 * First-load failure at the error boundary witness.
3 *
4 * Authority: maintainer decision for this branch, recorded in
5 * docs/guides/error-handling.md ("Using Suspense and Error Boundaries"). A
6 * failed first load reaches the nearest error boundary as an Error. When the
7 * sync adapter's cause is an Error, the boundary receives that exact object.
8 * When it is not, such as an HTTP response body, the boundary receives an
9 * Error whose message is the problem's message (`health.error.message`) and
10 * whose `cause` is the exact value the adapter reported. Solid's own resource
11 * wrapper would otherwise turn a non-Error into a generic "Unknown error".
12 * The failure can happen while sync starts or after the query has suspended.
13 * A crash during the first load follows the same rule.
14 */
15import { describe, expect, it } from 'vitest'
16import { render, waitFor } from '@solidjs/testing-library'
17import { createCollection } from '@tanstack/db'
18import { ErrorBoundary, Suspense } from 'solid-js'
19import { useLiveQuery } from '../src/useLiveQuery'
20​
21type Post = { id: string; title: string }
22​
23describe(`useLiveQuery first-load failure at an error boundary`, () => {
24 it.each([
25 { kind: `an Error`, deferred: false, isError: true },
26 { kind: `an Error`, deferred: true, isError: true },
27 { kind: `not an Error`, deferred: false, isError: false },
28 { kind: `not an Error`, deferred: true, isError: false },
29 ])(
30 `sends a first-load cause that is $kind as an Error (deferred: $deferred)`,
31 async ({ deferred, isError }) => {
32 const cause: unknown = isError
33 ? new Error(`first load failed`)
34 : { status: 500 }
35 let fail: ((error: unknown) => void) | undefined
36 const source = createCollection<Post>({
37 id: `solid-first-load-cause-${isError}-${deferred}`,
38 getKey: (post) => post.id,
39 sync: {
40 sync: (params) => {
41 if (deferred) fail = params.fail
42 else params.fail(cause)
43 },
44 },
45 })
46 const caught: Array<unknown> = []
47 function Titles() {
48 const query = useLiveQuery((q) => q.from({ post: source }))
49 return <div>{query().length} posts</div>
50 }
51 const view = render(() => (
52 <ErrorBoundary
53 fallback={(error) => {
54 caught.push(error)
55 return <div>Boundary caught</div>
56 }}
57 >
58 <Suspense fallback={<div>Loading</div>}>
59 <Titles />
60 </Suspense>
61 </ErrorBoundary>
62 ))
63 try {
64 if (deferred) {
65 await waitFor(() => expect(fail).toBeDefined())
66 fail!(cause)
67 }
68 await waitFor(() =>
69 expect(view.getByText(`Boundary caught`)).toBeTruthy(),
70 )
71 expect(source.status).toBe(`error`)
72 expect(source.health.error?.cause).toBe(cause)
73 const thrown = caught[0]
74 if (isError) {
75 expect(thrown, `an Error cause is thrown as is`).toBe(cause)
76 } else {
77 expect(thrown, `a non-Error cause is wrapped`).toBeInstanceOf(Error)
78 expect((thrown as Error).message).toBe(source.health.error?.message)
79 expect((thrown as Error).cause).toBe(cause)
80 }
81 } finally {
82 view.unmount()
83 await source.cleanup()
84 }
85 },
86 )
87​
88 // A crash while the query waits for its first load reaches the boundary
89 // under the same rule. (The shared conformance cells
90 // `crashed-status-during-first-load-*` check that the status is `crashed`.)
91 // A sync function that throws crashes the Collection while it starts; a
92 // later crash is the one core raises for a broken local store, and the
93 // witness raises it the same way.
94 it.each([
95 { kind: `an Error`, deferred: false, isError: true },
96 { kind: `an Error`, deferred: true, isError: true },
97 { kind: `not an Error`, deferred: false, isError: false },
98 { kind: `not an Error`, deferred: true, isError: false },
99 ])(
100 `sends a crash cause that is $kind as an Error (deferred: $deferred)`,
101 async ({ deferred, isError }) => {
102 const cause: unknown = isError
103 ? new Error(`local store broke`)
104 : { code: `SQLITE_CORRUPT` }
105 let started = false
106 const source = createCollection<Post>({
107 id: `solid-crash-cause-${isError}-${deferred}`,
108 getKey: (post) => post.id,
109 sync: {
110 sync: () => {
111 started = true
112 if (!deferred) throw cause
113 },
114 },
115 })
116 const caught: Array<unknown> = []
117 function Titles() {
118 const query = useLiveQuery((q) => q.from({ post: source }))
119 return <div>{query().length} posts</div>
120 }
121 const view = render(() => (
122 <ErrorBoundary
123 fallback={(error) => {
124 caught.push(error)
125 return <div>Boundary caught</div>
126 }}
127 >
128 <Suspense fallback={<div>Loading</div>}>
129 <Titles />
130 </Suspense>
131 </ErrorBoundary>
132 ))
133 try {
134 if (deferred) {
135 await waitFor(() => expect(started).toBe(true))
136 ;(
137 source as unknown as {
138 _lifecycle: { crash: (error: unknown) => void }
139 }
140 )._lifecycle.crash(cause)
141 }
142 await waitFor(() =>
143 expect(view.getByText(`Boundary caught`)).toBeTruthy(),
144 )
145 expect(source.status).toBe(`crashed`)
146 expect(source.health.error?.cause).toBe(cause)
147 const thrown = caught[0]
148 if (isError) {
149 expect(thrown, `an Error cause is thrown as is`).toBe(cause)
150 } else {
151 expect(thrown, `a non-Error cause is wrapped`).toBeInstanceOf(Error)
152 expect((thrown as Error).message).toBe(source.health.error?.message)
153 expect((thrown as Error).cause).toBe(cause)
154 }
155 } finally {
156 view.unmount()
157 await source.cleanup().catch(() => undefined)
158 }
159 },
160 )
161})