|
| 1 | +import { describe, it } from 'vitest' |
| 2 | +import { useQueryActions } from 'query/react:hooks/useQueryActions' |
| 3 | +import { createQuery } from 'query:index' |
| 4 | +import { act, Suspense } from 'react' |
| 5 | +import { createRoot } from 'react-dom/client' |
| 6 | + |
| 7 | +describe('useQueryActions', function () { |
| 8 | + it('can refetch data', async ({ expect }) => { |
| 9 | + let fetchCount = 0 |
| 10 | + |
| 11 | + function fetcher() { |
| 12 | + fetchCount++ |
| 13 | + return Promise.resolve('value-' + fetchCount) |
| 14 | + } |
| 15 | + |
| 16 | + const query = createQuery({ fetcher, expiration: () => 10000 }) |
| 17 | + const options = { query } |
| 18 | + const actionsRef: { current: ReturnType<typeof useQueryActions<string>> | null } = { |
| 19 | + current: null, |
| 20 | + } |
| 21 | + |
| 22 | + function Component() { |
| 23 | + const actions = useQueryActions<string>('/refetch-key', options) |
| 24 | + actionsRef.current = actions |
| 25 | + return null |
| 26 | + } |
| 27 | + |
| 28 | + const container = document.createElement('div') |
| 29 | + |
| 30 | + // oxlint-disable-next-line |
| 31 | + await act(async function () { |
| 32 | + createRoot(container).render( |
| 33 | + <Suspense fallback="loading"> |
| 34 | + <Component /> |
| 35 | + </Suspense> |
| 36 | + ) |
| 37 | + }) |
| 38 | + |
| 39 | + expect(actionsRef.current).not.toBeNull() |
| 40 | + expect(fetchCount).toBe(0) |
| 41 | + |
| 42 | + const result = await actionsRef.current!.refetch() |
| 43 | + |
| 44 | + expect(result).toBe('value-1') |
| 45 | + expect(fetchCount).toBe(1) |
| 46 | + }) |
| 47 | + |
| 48 | + it('can mutate data with correct types', async ({ expect }) => { |
| 49 | + function fetcher() { |
| 50 | + return Promise.resolve('initial') |
| 51 | + } |
| 52 | + |
| 53 | + const query = createQuery({ fetcher, expiration: () => 10000 }) |
| 54 | + const options = { query } |
| 55 | + const actionsRef: { current: ReturnType<typeof useQueryActions<string>> | null } = { |
| 56 | + current: null, |
| 57 | + } |
| 58 | + |
| 59 | + function Component() { |
| 60 | + const actions = useQueryActions<string>('/mutate-type-key', options) |
| 61 | + actionsRef.current = actions |
| 62 | + return null |
| 63 | + } |
| 64 | + |
| 65 | + const container = document.createElement('div') |
| 66 | + |
| 67 | + // oxlint-disable-next-line |
| 68 | + await act(async function () { |
| 69 | + createRoot(container).render( |
| 70 | + <Suspense fallback="loading"> |
| 71 | + <Component /> |
| 72 | + </Suspense> |
| 73 | + ) |
| 74 | + }) |
| 75 | + |
| 76 | + const result = await actionsRef.current!.mutate('new-value') |
| 77 | + |
| 78 | + expect(result).toBe('new-value') |
| 79 | + }) |
| 80 | + |
| 81 | + it('can forget cached data', async ({ expect }) => { |
| 82 | + function fetcher() { |
| 83 | + return Promise.resolve('data') |
| 84 | + } |
| 85 | + |
| 86 | + const query = createQuery({ fetcher, expiration: () => 10000 }) |
| 87 | + const options = { query } |
| 88 | + const actionsRef: { current: ReturnType<typeof useQueryActions<string>> | null } = { |
| 89 | + current: null, |
| 90 | + } |
| 91 | + |
| 92 | + // Pre-populate the cache so forget has something to remove. |
| 93 | + await query.query('/forget-action-key') |
| 94 | + |
| 95 | + function Component() { |
| 96 | + const actions = useQueryActions<string>('/forget-action-key', options) |
| 97 | + actionsRef.current = actions |
| 98 | + return null |
| 99 | + } |
| 100 | + |
| 101 | + const container = document.createElement('div') |
| 102 | + |
| 103 | + // oxlint-disable-next-line |
| 104 | + await act(async function () { |
| 105 | + createRoot(container).render( |
| 106 | + <Suspense fallback="loading"> |
| 107 | + <Component /> |
| 108 | + </Suspense> |
| 109 | + ) |
| 110 | + }) |
| 111 | + |
| 112 | + expect(actionsRef.current).not.toBeNull() |
| 113 | + expect(query.keys('items')).toContain('/forget-action-key') |
| 114 | + |
| 115 | + await actionsRef.current!.forget() |
| 116 | + |
| 117 | + expect(query.keys('items')).not.toContain('/forget-action-key') |
| 118 | + }) |
| 119 | +}) |
0 commit comments