|
| 1 | +import { describe, it } from 'vitest' |
| 2 | +import { QueryObserver } from '..' |
| 3 | +import { createQueryClient, doNotExecute } from './utils' |
| 4 | +import type { Equal, Expect } from './utils' |
| 5 | + |
| 6 | +describe('placeholderData', () => { |
| 7 | + describe('placeholderData function', () => { |
| 8 | + it('previousQuery should have typed queryKey', () => { |
| 9 | + doNotExecute(() => { |
| 10 | + const queryKey = ['SomeQuery', 42, { foo: 'bar' }] as const |
| 11 | + type QueryKey = typeof queryKey |
| 12 | + |
| 13 | + new QueryObserver(createQueryClient(), { |
| 14 | + queryKey, |
| 15 | + placeholderData: (_, previousQuery) => { |
| 16 | + const previousQueryKey = previousQuery?.queryKey |
| 17 | + |
| 18 | + const result: Expect< |
| 19 | + Equal<typeof previousQueryKey, QueryKey | undefined> |
| 20 | + > = true |
| 21 | + return result |
| 22 | + }, |
| 23 | + }) |
| 24 | + }) |
| 25 | + }) |
| 26 | + |
| 27 | + it('previousQuery should have typed error', () => { |
| 28 | + doNotExecute(() => { |
| 29 | + class CustomError extends Error { |
| 30 | + name = 'CustomError' as const |
| 31 | + } |
| 32 | + |
| 33 | + new QueryObserver<boolean, CustomError>(createQueryClient(), { |
| 34 | + placeholderData: (_, previousQuery) => { |
| 35 | + const error = previousQuery?.state.error |
| 36 | + |
| 37 | + const result: Expect< |
| 38 | + Equal<typeof error, CustomError | null | undefined> |
| 39 | + > = true |
| 40 | + return result |
| 41 | + }, |
| 42 | + }) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + it('previousData should have the same type as query data', () => { |
| 47 | + doNotExecute(() => { |
| 48 | + const queryData = { foo: 'bar' } as const |
| 49 | + type QueryData = typeof queryData |
| 50 | + |
| 51 | + new QueryObserver(createQueryClient(), { |
| 52 | + queryFn: () => queryData, |
| 53 | + select: (data) => data.foo, |
| 54 | + placeholderData: (previousData) => { |
| 55 | + const result: Expect< |
| 56 | + Equal<typeof previousData, QueryData | undefined> |
| 57 | + > = true |
| 58 | + return result ? previousData : undefined |
| 59 | + }, |
| 60 | + }) |
| 61 | + }) |
| 62 | + }) |
| 63 | + }) |
| 64 | +}) |
0 commit comments