Skip to content

Commit 20120ae

Browse files
sukvvonTkDodo
andauthored
test(query-core/mutationObserver): add 'advanceTimersByTimeAsync' in all tests (#9312)
Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 46b5db9 commit 20120ae

File tree

1 file changed

+47
-53
lines changed

1 file changed

+47
-53
lines changed

packages/query-core/src/__tests__/mutationObserver.test.tsx

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ describe('mutationObserver', () => {
3131

3232
unsubscribe1()
3333

34-
await vi.waitFor(() => {
35-
// 1 call: loading
36-
expect(subscription1Handler).toBeCalledTimes(1)
37-
// 2 calls: loading, success
38-
expect(subscription2Handler).toBeCalledTimes(2)
39-
})
34+
expect(subscription1Handler).toBeCalledTimes(1)
35+
expect(subscription2Handler).toBeCalledTimes(1)
36+
37+
await vi.advanceTimersByTimeAsync(20)
38+
expect(subscription1Handler).toBeCalledTimes(1)
39+
expect(subscription2Handler).toBeCalledTimes(2)
4040

41-
// Clean-up
4241
unsubscribe2()
4342
})
4443

@@ -53,15 +52,14 @@ describe('mutationObserver', () => {
5352
const unsubscribe = mutation.subscribe(subscriptionHandler)
5453

5554
mutation.mutate('input')
56-
await vi.waitFor(() =>
57-
expect(queryClient.getMutationCache().findAll()).toHaveLength(1),
58-
)
55+
56+
await vi.advanceTimersByTimeAsync(5)
57+
expect(queryClient.getMutationCache().findAll()).toHaveLength(1)
5958

6059
unsubscribe()
6160

62-
await vi.waitFor(() =>
63-
expect(queryClient.getMutationCache().findAll()).toHaveLength(0),
64-
)
61+
await vi.advanceTimersByTimeAsync(10)
62+
expect(queryClient.getMutationCache().findAll()).toHaveLength(0)
6563
})
6664

6765
test('reset should remove observer to trigger GC', async () => {
@@ -75,15 +73,14 @@ describe('mutationObserver', () => {
7573
const unsubscribe = mutation.subscribe(subscriptionHandler)
7674

7775
mutation.mutate('input')
78-
await vi.waitFor(() =>
79-
expect(queryClient.getMutationCache().findAll()).toHaveLength(1),
80-
)
76+
77+
await vi.advanceTimersByTimeAsync(5)
78+
expect(queryClient.getMutationCache().findAll()).toHaveLength(1)
8179

8280
mutation.reset()
8381

84-
await vi.waitFor(() =>
85-
expect(queryClient.getMutationCache().findAll()).toHaveLength(0),
86-
)
82+
await vi.advanceTimersByTimeAsync(10)
83+
expect(queryClient.getMutationCache().findAll()).toHaveLength(0)
8784

8885
unsubscribe()
8986
})
@@ -101,12 +98,11 @@ describe('mutationObserver', () => {
10198

10299
mutation.mutate('input')
103100

104-
await vi.waitFor(() =>
105-
expect(mutation.getCurrentResult()).toMatchObject({
106-
status: 'success',
107-
data: 'input',
108-
}),
109-
)
101+
await vi.advanceTimersByTimeAsync(5)
102+
expect(mutation.getCurrentResult()).toMatchObject({
103+
status: 'success',
104+
data: 'input',
105+
})
110106

111107
mutation.setOptions({
112108
mutationKey: [...key, '2'],
@@ -132,17 +128,16 @@ describe('mutationObserver', () => {
132128

133129
mutationObserver.mutate('input')
134130

135-
await vi.waitFor(() =>
136-
expect(
137-
queryClient.getMutationCache().find({ mutationKey: [...key, '1'] }),
138-
).toMatchObject({
139-
options: { mutationKey: [...key, '1'] },
140-
state: {
141-
status: 'success',
142-
data: 'input',
143-
},
144-
}),
145-
)
131+
await vi.advanceTimersByTimeAsync(5)
132+
expect(
133+
queryClient.getMutationCache().find({ mutationKey: [...key, '1'] }),
134+
).toMatchObject({
135+
options: { mutationKey: [...key, '1'] },
136+
state: {
137+
status: 'success',
138+
data: 'input',
139+
},
140+
})
146141

147142
mutationObserver.setOptions({
148143
mutationKey: [...key, '2'],
@@ -173,15 +168,14 @@ describe('mutationObserver', () => {
173168

174169
mutationObserver.mutate('input')
175170

176-
await vi.waitFor(() =>
177-
expect(queryClient.getMutationCache().find({})).toMatchObject({
178-
options: { meta: { a: 1 } },
179-
state: {
180-
status: 'success',
181-
data: 'input',
182-
},
183-
}),
184-
)
171+
await vi.advanceTimersByTimeAsync(5)
172+
expect(queryClient.getMutationCache().find({})).toMatchObject({
173+
options: { meta: { a: 1 } },
174+
state: {
175+
status: 'success',
176+
data: 'input',
177+
},
178+
})
185179

186180
mutationObserver.setOptions({
187181
meta: { a: 2 },
@@ -251,14 +245,14 @@ describe('mutationObserver', () => {
251245
const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
252246

253247
mutationObserver.mutate('input').catch(() => undefined)
254-
await vi.waitFor(() =>
255-
expect(queryClient.getMutationCache().find({})).toMatchObject({
256-
options: { meta: { a: 1 } },
257-
state: {
258-
status: 'error',
259-
},
260-
}),
261-
)
248+
249+
await vi.advanceTimersByTimeAsync(5)
250+
expect(queryClient.getMutationCache().find({})).toMatchObject({
251+
options: { meta: { a: 1 } },
252+
state: {
253+
status: 'error',
254+
},
255+
})
262256

263257
mutationObserver.setOptions({
264258
meta: { a: 2 },

0 commit comments

Comments
 (0)