Open History to see the last commit
1/**
2 * @tanstack/octane-query conformance β useQuery / useMutation / QueryClientProvider /
3 * useQueryClient on octane, driving the REAL @tanstack/query-core (observers +
4 * caches reused verbatim). Exercises the async query lifecycle (pending β
5 * success / error), context client resolution, and the mutation lifecycle.
6 */
7import { beforeEach, describe, expect, it } from 'vitest'
8import { QueryClient } from '@tanstack/octane-query'
9import { mount, nextPaint } from '../_helpers'
10import { App, MutApp, ProbeApp } from '../_fixtures/app.tsrx'
11β
12let client: QueryClient
13β
14beforeEach(() => {
15 client = new QueryClient({
16 defaultOptions: { queries: { retry: false }, mutations: {} },
17 })
18})
19β
20// query-core batches notifications on a macrotask; flush a few cycles + paints.
21async function flush() {
22 for (let i = 0; i < 5; i++) {
23 await new Promise((r) => setTimeout(r, 0))
24 await nextPaint()
25 }
26}
27β
28describe('useQuery lifecycle (via QueryClientProvider)', () => {
29 it('pending -> success', async () => {
30 let resolveFn: (v: string) => void = () => {}
31 const queryFn = () => new Promise<string>((r) => (resolveFn = r))
32 const r = mount(App, { client, queryFn })
33 expect(r.find('#status').textContent).toBe('pending')
34 await flush()
35 resolveFn('world')
36 await flush()
37 expect(r.find('#status').textContent).toBe('data:world')
38 r.unmount()
39 })
40β
41 it('pending -> error (retry disabled)', async () => {
42 let rejectFn: (e: Error) => void = () => {}
43 const queryFn = () => new Promise<string>((_res, rej) => (rejectFn = rej))
44 const r = mount(App, { client, queryFn })
45 expect(r.find('#status').textContent).toBe('pending')
46 await flush()
47 rejectFn(new Error('boom'))
48 await flush()
49 expect(r.find('#status').textContent).toBe('error:boom')
50 r.unmount()
51 })
52})
53β
54describe('useQueryClient', () => {
55 it('resolves the client provided by QueryClientProvider', async () => {
56 let seen: unknown = null
57 const r = mount(ProbeApp, { client, onClient: (c: unknown) => (seen = c) })
58 await nextPaint()
59 expect(r.find('#probe').textContent).toBe('ok')
60 expect(seen).toBe(client)
61 r.unmount()
62 })
63})
64β
65describe('useMutation lifecycle', () => {
66 it('idle -> pending -> success on mutate()', async () => {
67 let resolveFn: (v: string) => void = () => {}
68 const mutationFn = () => new Promise<string>((r) => (resolveFn = r))
69 const r = mount(MutApp, { client, mutationFn })
70 expect(r.find('#mstatus').textContent).toBe('idle')
71 r.click('#go')
72 await flush()
73 expect(r.find('#mstatus').textContent).toBe('pending')
74 resolveFn('done')
75 await flush()
76 expect(r.find('#mstatus').textContent).toBe('data:done')
77 r.unmount()
78 })
79})