From 3d18f60dcc6f55a9219bbfa0a8a848a930cfb0c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aar=C3=B3n=20Contreras?= Date: Tue, 7 Sep 2021 11:29:57 +0200 Subject: [PATCH 1/2] docs(useQuery): add docs for reset method --- docs/src/pages/reference/useQuery.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/src/pages/reference/useQuery.md b/docs/src/pages/reference/useQuery.md index d62f8bd736..dfe0a68fd6 100644 --- a/docs/src/pages/reference/useQuery.md +++ b/docs/src/pages/reference/useQuery.md @@ -24,6 +24,7 @@ const { isSuccess, refetch, remove, + reset, status, } = useQuery(queryKey, queryFn?, { cacheTime, @@ -228,3 +229,6 @@ const result = useQuery({ - If `cancelRefetch` is `true`, then the current request will be cancelled before a new request is made - `remove: () => void` - A function to remove the query from the cache. +- `reset: (options: { throwOnError: boolean, cancelRefetch: boolean }) => Promise` + - A function to manually reset the query to its initial state. + - `options` are used to call `refetch` on the query after it is resetted. From 03260150f4f36cc8fb35633e915965999855eebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aar=C3=B3n=20Contreras?= Date: Tue, 7 Sep 2021 11:31:24 +0200 Subject: [PATCH 2/2] test(useQuery): add tests for reset method --- src/react/tests/useQuery.test.tsx | 76 +++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/react/tests/useQuery.test.tsx b/src/react/tests/useQuery.test.tsx index 8eae5d51f7..a69d9e63e3 100644 --- a/src/react/tests/useQuery.test.tsx +++ b/src/react/tests/useQuery.test.tsx @@ -1032,6 +1032,82 @@ describe('useQuery', () => { expect(states[3]).toMatchObject({ data: 2 }) }) + it('should be able to reset a query', async () => { + const key = queryKey() + const states: UseQueryResult[] = [] + let count = 0 + + function Page() { + const state = useQuery(key, () => ++count) + + states.push(state) + + const { reset } = state + + React.useEffect(() => { + setActTimeout(() => { + reset() + }, 5) + }, [reset]) + + return null + } + + renderWithClient(queryClient, ) + + await sleep(20) + + expect(states.length).toBe(5) + // Initial + expect(states[0]).toMatchObject({ data: undefined }) + // Fetched + expect(states[1]).toMatchObject({ data: 1 }) + // Reset + expect(states[2]).toMatchObject({ data: undefined }) + // Initial (refetch) + expect(states[3]).toMatchObject({ data: undefined }) + // Fetched + expect(states[4]).toMatchObject({ data: 2 }) + }) + + it('should reset the query to its initial state', async () => { + const key = queryKey() + const states: UseQueryResult[] = [] + let count = 0 + + function Page() { + const state = useQuery(key, () => ++count, { initialData: 99 }) + + states.push(state) + + const { reset } = state + + React.useEffect(() => { + setActTimeout(() => { + reset() + }, 5) + }, [reset]) + + return null + } + + renderWithClient(queryClient, ) + + await sleep(20) + + expect(states.length).toBe(5) + // Initial + expect(states[0]).toMatchObject({ data: 99 }) + // Fetched + expect(states[1]).toMatchObject({ data: 1 }) + // Reset + expect(states[2]).toMatchObject({ data: 99 }) + // Initial (refetch) + expect(states[3]).toMatchObject({ data: 99 }) + // Fetched + expect(states[4]).toMatchObject({ data: 2 }) + }) + it('should share equal data structures between query results', async () => { const key = queryKey()