Skip to content

Commit 77c817f

Browse files
authored
Reset query docs and tests (#1393)
* docs: Add resetQueries * test: Add resetQueries tests
1 parent 95fe412 commit 77c817f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

docs/src/pages/reference/QueryClient.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Its available methods are:
3232
- [`refetchQueries`](#queryclientrefetchqueries)
3333
- [`cancelQueries`](#queryclientcancelqueries)
3434
- [`removeQueries`](#queryclientremovequeries)
35+
- [`resetQueries`](#queryclientresetqueries)
3536
- [`isFetching`](#queryclientisfetching)
3637
- [`getDefaultOptions`](#queryclientsetdefaultoptions)
3738
- [`setDefaultOptions`](#queryclientgetdefaultoptions)
@@ -274,6 +275,30 @@ queryClient.removeQueries(queryKey, { exact: true })
274275

275276
This method does not return anything
276277

278+
## `queryClient.resetQueries`
279+
280+
The `resetQueries` method can be used to reset queries in the cache to their
281+
initial state based on their query keys or any other functionally accessible
282+
property/state of the query.
283+
284+
This will notify subscribers — unlike `clear`, which removes all
285+
subscribers — and reset the query to its pre-loaded state — unlike
286+
`invalidateQueries`. If a query has `initialData`, the query's data will be
287+
reset to that.
288+
289+
```js
290+
queryClient.resetQueries(queryKey, { exact: true })
291+
```
292+
293+
**Options**
294+
295+
- `queryKey?: QueryKey`: [Query Keys](../guides/query-keys)
296+
- `filters?: QueryFilters`: [Query Filters](../guides/query-filters)
297+
298+
**Returns**
299+
300+
This method does not return anything
301+
277302
## `queryClient.isFetching`
278303

279304
This `isFetching` method returns an `integer` representing how many queries, if any, in the cache are currently fetching (including background-fetching, loading new pages, or loading more infinite query results)

src/core/tests/queryCache.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,56 @@ describe('queryCache', () => {
948948
expect(queryFn2).toHaveBeenCalledTimes(1)
949949
})
950950

951+
test('should notify listeners when a query is reset', async () => {
952+
const key = queryKey()
953+
954+
const callback = jest.fn()
955+
956+
await queryClient.prefetchQuery(key, () => 'data')
957+
958+
queryCache.subscribe(callback)
959+
960+
queryClient.resetQueries(key)
961+
962+
expect(callback).toHaveBeenCalled()
963+
})
964+
965+
test('resetQueries should reset query', async () => {
966+
const key = queryKey()
967+
968+
await queryClient.prefetchQuery(key, () => 'data')
969+
970+
let state = queryClient.getQueryState(key)
971+
expect(state?.data).toEqual('data')
972+
expect(state?.status).toEqual('success')
973+
974+
queryClient.resetQueries(key)
975+
976+
state = queryClient.getQueryState(key)
977+
978+
expect(state).toBeTruthy()
979+
expect(state?.data).toBeUndefined()
980+
expect(state?.status).toEqual('idle')
981+
})
982+
983+
test('resetQueries should reset query data to initial data if set', async () => {
984+
const key = queryKey()
985+
986+
await queryClient.prefetchQuery(key, () => 'data', {
987+
initialData: 'initial',
988+
})
989+
990+
let state = queryClient.getQueryState(key)
991+
expect(state?.data).toEqual('data')
992+
993+
queryClient.resetQueries(key)
994+
995+
state = queryClient.getQueryState(key)
996+
997+
expect(state).toBeTruthy()
998+
expect(state?.data).toEqual('initial')
999+
})
1000+
9511001
test('find should filter correctly', async () => {
9521002
const key = queryKey()
9531003
const testCache = new QueryCache()

0 commit comments

Comments
 (0)