Skip to content

Commit

Permalink
feat: mutation cachetime
Browse files Browse the repository at this point in the history
try to make tests more resilient
  • Loading branch information
TkDodo committed Nov 17, 2021
1 parent d10cad0 commit 08c32a7
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/core/tests/mutationCache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ describe('mutationCache', () => {

expect(testCache.getAll()).toHaveLength(1)
await sleep(10)
expect(testCache.getAll()).toHaveLength(0)
await waitFor(() => {
expect(testCache.getAll()).toHaveLength(0)
})
expect(onSuccess).toHaveBeenCalledTimes(1)
})

Expand All @@ -144,7 +146,9 @@ describe('mutationCache', () => {
unsubscribe()
expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
await sleep(10)
expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
await waitFor(() => {
expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
})
})

test('should only remove when the last observer unsubscribes', async () => {
Expand Down Expand Up @@ -182,7 +186,9 @@ describe('mutationCache', () => {
expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
// wait for cacheTime to gc
await sleep(10)
expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
await waitFor(() => {
expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
})
})

test('should be garbage collected later when unsubscribed and mutation is loading', async () => {
Expand All @@ -206,7 +212,9 @@ describe('mutationCache', () => {
expect(queryClient.getMutationCache().getAll()).toHaveLength(1)
await sleep(10)
// should be removed after an additional cacheTime wait
expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
await waitFor(() => {
expect(queryClient.getMutationCache().getAll()).toHaveLength(0)
})
expect(onSuccess).toHaveBeenCalledTimes(1)
})

Expand Down

0 comments on commit 08c32a7

Please sign in to comment.