Skip to content

Commit 268e4f2

Browse files
minseong0324TkDodo
andauthored
test(query-core): add test case for query (#9145)
* test(query-core): add test case for query * test(query-core): use hashQueryKeyByOptions * add test case about "should call initialData function when it is a function" * reflect feedback --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent 7b0aaab commit 268e4f2

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

packages/query-core/src/__tests__/query.test.tsx

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ import {
55
sleep,
66
} from '@tanstack/query-test-utils'
77
import {
8+
Query,
89
QueryClient,
910
QueryObserver,
1011
dehydrate,
1112
hydrate,
1213
isCancelledError,
1314
} from '..'
15+
import { hashQueryKeyByOptions } from '../utils'
1416
import { mockOnlineManagerIsOnline, setIsServer } from './utils'
15-
import type { QueryCache, QueryFunctionContext, QueryObserverResult } from '..'
17+
import type {
18+
QueryCache,
19+
QueryFunctionContext,
20+
QueryKey,
21+
QueryObserverResult,
22+
} from '..'
1623

1724
describe('query', () => {
1825
let queryClient: QueryClient
@@ -1019,4 +1026,73 @@ describe('query', () => {
10191026
await vi.advanceTimersByTimeAsync(10)
10201027
expect(query.state.status).toBe('error')
10211028
})
1029+
1030+
test('should use persister if provided', async () => {
1031+
const key = queryKey()
1032+
1033+
await queryClient.prefetchQuery({
1034+
queryKey: key,
1035+
queryFn: () => 'data',
1036+
persister: () => Promise.resolve('persisted data'),
1037+
})
1038+
1039+
const query = queryCache.find({ queryKey: key })!
1040+
expect(query.state.data).toBe('persisted data')
1041+
})
1042+
1043+
test('should use queryFn from observer if not provided in options', async () => {
1044+
const key = queryKey()
1045+
const queryFn = () => Promise.resolve('data')
1046+
const observer = new QueryObserver(queryClient, {
1047+
queryKey: key,
1048+
queryFn: queryFn,
1049+
})
1050+
1051+
const query = new Query({
1052+
client: queryClient,
1053+
queryKey: key,
1054+
queryHash: hashQueryKeyByOptions(key),
1055+
})
1056+
1057+
query.addObserver(observer)
1058+
1059+
await query.fetch()
1060+
const result = await query.state.data
1061+
expect(result).toBe('data')
1062+
expect(query.options.queryFn).toBe(queryFn)
1063+
})
1064+
1065+
test('should log error when queryKey is not an array', async () => {
1066+
const consoleMock = vi.spyOn(console, 'error')
1067+
const key: unknown = 'string-key'
1068+
1069+
await queryClient.prefetchQuery({
1070+
queryKey: key as QueryKey,
1071+
queryFn: () => 'data',
1072+
})
1073+
1074+
expect(consoleMock).toHaveBeenCalledWith(
1075+
"As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']",
1076+
)
1077+
1078+
consoleMock.mockRestore()
1079+
})
1080+
1081+
test('should call initialData function when it is a function', () => {
1082+
const key = queryKey()
1083+
const initialDataFn = vi.fn(() => 'initial data')
1084+
1085+
const query = new Query({
1086+
client: queryClient,
1087+
queryKey: key,
1088+
queryHash: hashQueryKeyByOptions(key),
1089+
options: {
1090+
queryFn: () => 'data',
1091+
initialData: initialDataFn,
1092+
},
1093+
})
1094+
1095+
expect(initialDataFn).toHaveBeenCalledTimes(1)
1096+
expect(query.state.data).toBe('initial data')
1097+
})
10221098
})

0 commit comments

Comments
 (0)