Open History to see the last commit
1/**
2 * IsRestoringProvider / useIsRestoring and QueryErrorResetBoundary /
3 * useQueryErrorResetBoundary β the persistence-restore gate and the
4 * error-boundary reset coordinator.
5 */
6import { beforeEach, describe, expect, it } from 'vitest'
7import { QueryClient } from '@tanstack/octane-query'
8import { mount, nextPaint } from '../_helpers'
9import { ResetApp, RestoringApp } from '../_fixtures/boundaries.tsrx'
10β
11let client: QueryClient
12beforeEach(() => {
13 client = new QueryClient({ defaultOptions: { queries: { retry: false } } })
14 client.mount()
15})
16β
17async function flush() {
18 for (let i = 0; i < 6; i++) {
19 await new Promise((r) => setTimeout(r, 0))
20 await nextPaint()
21 }
22}
23β
24describe('IsRestoring', () => {
25 it('does NOT subscribe/fetch while restoring', async () => {
26 let called = 0
27 const queryFn = () => {
28 called++
29 return Promise.resolve('x')
30 }
31 const r = mount(RestoringApp, { client, restoring: true, queryFn })
32 await flush()
33 expect(called).toBe(0)
34 expect(r.find('#status').textContent).toBe('pending')
35 r.unmount()
36 })
37β
38 it('fetches normally when not restoring', async () => {
39 let called = 0
40 const queryFn = () => {
41 called++
42 return Promise.resolve('y')
43 }
44 const r = mount(RestoringApp, { client, restoring: false, queryFn })
45 await flush()
46 expect(called).toBeGreaterThan(0)
47 expect(r.find('#status').textContent).toBe('data:y')
48 r.unmount()
49 })
50})
51β
52describe('QueryErrorResetBoundary', () => {
53 it('reset() lets the query retry instead of re-throwing', async () => {
54 let calls = 0
55 const queryFn = () => {
56 calls++
57 return calls === 1
58 ? Promise.reject(new Error('boom'))
59 : Promise.resolve('recovered')
60 }
61 const r = mount(ResetApp, { client, queryFn })
62 await flush()
63 // First fetch errored β @catch/<ErrorBoundary> fallback (the retry button).
64 expect(r.find('#retry').textContent).toBe('retry')
65 r.click('#retry')
66 await flush()
67 expect(r.find('#data').textContent).toBe('data:recovered')
68 r.unmount()
69 })
70})