-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
packages/query-core/src/tests/queryObserver.types.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { describe, it } from 'vitest' | ||
import { QueryObserver } from '..' | ||
import { createQueryClient, doNotExecute } from './utils' | ||
import type { Equal, Expect } from './utils' | ||
|
||
describe('placeholderData', () => { | ||
describe('placeholderData function', () => { | ||
it('previousQuery should have typed queryKey', () => { | ||
doNotExecute(() => { | ||
const queryKey = ['SomeQuery', 42, { foo: 'bar' }] as const | ||
type QueryKey = typeof queryKey | ||
|
||
new QueryObserver(createQueryClient(), { | ||
queryKey, | ||
placeholderData: (_, previousQuery) => { | ||
const previousQueryKey = previousQuery?.queryKey | ||
|
||
const result: Expect< | ||
Equal<typeof previousQueryKey, QueryKey | undefined> | ||
> = true | ||
return result | ||
}, | ||
}) | ||
}) | ||
}) | ||
|
||
it('previousQuery should have typed error', () => { | ||
doNotExecute(() => { | ||
class CustomError extends Error { | ||
name = 'CustomError' as const | ||
} | ||
|
||
new QueryObserver<boolean, CustomError>(createQueryClient(), { | ||
placeholderData: (_, previousQuery) => { | ||
const error = previousQuery?.state.error | ||
|
||
const result: Expect< | ||
Equal<typeof error, CustomError | null | undefined> | ||
> = true | ||
return result | ||
}, | ||
}) | ||
}) | ||
}) | ||
|
||
it('previousData should have the same type as query data', () => { | ||
doNotExecute(() => { | ||
const queryData = { foo: 'bar' } as const | ||
type QueryData = typeof queryData | ||
|
||
new QueryObserver(createQueryClient(), { | ||
queryFn: () => queryData, | ||
select: (data) => data.foo, | ||
placeholderData: (previousData) => { | ||
const result: Expect< | ||
Equal<typeof previousData, QueryData | undefined> | ||
> = true | ||
return result ? previousData : undefined | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters