Skip to content

Commit 83dfc24

Browse files
author
Guillaume Labat
committed
test(QueryClient): warning with defaults options
Highlight how query defaults registration order matters.
1 parent 4053ef0 commit 83dfc24

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/core/tests/queryClient.test.tsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
QueryCache,
55
QueryClient,
66
QueryFunction,
7+
QueryFunctionContext,
78
QueryObserver,
89
MutationObserver,
910
} from '../..'
@@ -121,6 +122,70 @@ describe('queryClient', () => {
121122
queryClient.setQueryDefaults(key, queryOptions2)
122123
expect(queryClient.getQueryDefaults(key)).toMatchObject(queryOptions2)
123124
})
125+
126+
test('should warn in dev if several query defaults match a given key', () => {
127+
// Check discussion here: https://github.com/tannerlinsley/react-query/discussions/3199
128+
const consoleWarnMock = jest.spyOn(console, 'warn')
129+
consoleWarnMock.mockImplementation(() => true)
130+
131+
const keyABCD = [
132+
{
133+
a: 'a',
134+
b: 'b',
135+
c: 'c',
136+
d: 'd',
137+
},
138+
]
139+
140+
// The key below "contains" keyABCD => it is more generic
141+
const keyABC = [
142+
{
143+
a: 'a',
144+
b: 'b',
145+
c: 'c',
146+
},
147+
]
148+
149+
// The defaults for query matching key "ABCD" (least generic)
150+
const defaultsOfABCD = {
151+
queryFn: function ABCDQueryFn() {
152+
return 'ABCD'
153+
},
154+
}
155+
156+
// The defaults for query matching key "ABC" (most generic)
157+
const defaultsOfABC = {
158+
queryFn: function ABCQueryFn() {
159+
return 'ABC'
160+
},
161+
}
162+
163+
// No defaults, no warning
164+
const noDefaults = queryClient.getQueryDefaults(keyABCD)
165+
expect(noDefaults).toBeUndefined()
166+
expect(consoleWarnMock).not.toHaveBeenCalled()
167+
168+
// If defaults for key ABCD are registered **before** the ones of key ABC (more generic)…
169+
queryClient.setQueryDefaults(keyABCD, defaultsOfABCD)
170+
queryClient.setQueryDefaults(keyABC, defaultsOfABC)
171+
// … then the "good" defaults are retrieved: we get the ones for key "ABCD"
172+
const goodDefaults = queryClient.getQueryDefaults(keyABCD)
173+
expect(goodDefaults).toBe(defaultsOfABCD)
174+
expect(consoleWarnMock).toHaveBeenCalledTimes(1)
175+
176+
// Let's reset the defaults query options and change the order of registration
177+
queryClient.queryDefaults.length = 0
178+
// The defaults for key ABC (more generic) are registered **before** the ones of key ABCD…
179+
queryClient.setQueryDefaults(keyABC, defaultsOfABC)
180+
queryClient.setQueryDefaults(keyABCD, defaultsOfABCD)
181+
// … then the "wrong" defaults are retrieved: we get the ones for key "ABC"
182+
const badDefaults = queryClient.getQueryDefaults(keyABCD)
183+
expect(badDefaults).not.toBe(defaultsOfABCD)
184+
expect(badDefaults).toBe(defaultsOfABC)
185+
expect(consoleWarnMock).toHaveBeenCalledTimes(2)
186+
187+
consoleWarnMock.mockRestore()
188+
})
124189
})
125190

126191
describe('setQueryData', () => {

0 commit comments

Comments
 (0)