|
4 | 4 | QueryCache,
|
5 | 5 | QueryClient,
|
6 | 6 | QueryFunction,
|
| 7 | + QueryFunctionContext, |
7 | 8 | QueryObserver,
|
8 | 9 | MutationObserver,
|
9 | 10 | } from '../..'
|
@@ -121,6 +122,70 @@ describe('queryClient', () => {
|
121 | 122 | queryClient.setQueryDefaults(key, queryOptions2)
|
122 | 123 | expect(queryClient.getQueryDefaults(key)).toMatchObject(queryOptions2)
|
123 | 124 | })
|
| 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 | + }) |
124 | 189 | })
|
125 | 190 |
|
126 | 191 | describe('setQueryData', () => {
|
|
0 commit comments