Skip to content

test: add invalidate query tests #1052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/core/tests/queryCache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,28 @@ describe('queryCache', () => {
expect(queryFn2).toHaveBeenCalledTimes(2)
})

test('invalidateQueries should not refetch inactive queries by default', async () => {
const key1 = queryKey()
const key2 = queryKey()
const queryFn1 = jest.fn()
const queryFn2 = jest.fn()
const cache = new QueryCache()
await cache.fetchQuery(key1, queryFn1)
await cache.fetchQuery(key2, queryFn2)
const query1 = cache.getQuery(key1)!
const observer = new QueryObserver({
...query1.config,
enabled: false,
staleTime: Infinity,
})
observer.subscribe()
await cache.invalidateQueries(key1)
observer.unsubscribe()
cache.clear()
expect(queryFn1).toHaveBeenCalledTimes(1)
expect(queryFn2).toHaveBeenCalledTimes(1)
})

test('getQueries should return queries that partially match queryKey', async () => {
const key1 = queryKey()
const key2 = queryKey()
Expand Down
40 changes: 40 additions & 0 deletions src/react/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,46 @@ describe('useQuery', () => {
})
})

it('should not refetch disabled query when invalidated with invalidateQueries', async () => {
const key = queryKey()
const states: QueryResult<number>[] = []
let count = 0

function Page() {
const state = useQuery(
key,
async () => {
await sleep(10)
count++
return count
},
{ enabled: false }
)

states.push(state)

React.useEffect(() => {
setTimeout(() => {
queryCache.invalidateQueries(key)
}, 20)
}, [])

return null
}

render(<Page />)

await waitForMs(100)

expect(states.length).toBe(1)
expect(states[0]).toMatchObject({
data: undefined,
isFetching: false,
isSuccess: false,
isStale: true,
})
})

it('should keep the previous data when keepPreviousData is set', async () => {
const key = queryKey()
const states: QueryResult<number>[] = []
Expand Down