Skip to content

Commit 4ebca6d

Browse files
Add failing test to capture issue #4693 (autoBatchEnhancer with fake timers)
1 parent 1b9ece9 commit 4ebca6d

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

packages/toolkit/src/tests/autoBatchEnhancer.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,82 @@ describe.each(cases)('autoBatchEnhancer: %j', (autoBatchOptions) => {
125125
expect(subscriptionNotifications).toBe(3)
126126
})
127127
})
128+
129+
describe.each(cases)(
130+
'autoBatchEnhancer with fake timers: %j',
131+
(autoBatchOptions) => {
132+
beforeAll(() => {
133+
vitest.useFakeTimers({
134+
toFake: ['setTimeout', 'queueMicrotask', 'requestAnimationFrame'],
135+
})
136+
})
137+
afterAll(() => {
138+
vitest.useRealTimers()
139+
})
140+
beforeEach(() => {
141+
subscriptionNotifications = 0
142+
store = makeStore(autoBatchOptions)
143+
144+
store.subscribe(() => {
145+
subscriptionNotifications++
146+
})
147+
})
148+
test('Does not alter normal subscription notification behavior', () => {
149+
store.dispatch(decrementUnbatched())
150+
expect(subscriptionNotifications).toBe(1)
151+
store.dispatch(decrementUnbatched())
152+
expect(subscriptionNotifications).toBe(2)
153+
store.dispatch(decrementUnbatched())
154+
expect(subscriptionNotifications).toBe(3)
155+
store.dispatch(decrementUnbatched())
156+
vitest.runAllTimers()
157+
158+
expect(subscriptionNotifications).toBe(4)
159+
})
160+
161+
test('Only notifies once if several batched actions are dispatched in a row', () => {
162+
store.dispatch(incrementBatched())
163+
expect(subscriptionNotifications).toBe(0)
164+
store.dispatch(incrementBatched())
165+
expect(subscriptionNotifications).toBe(0)
166+
store.dispatch(incrementBatched())
167+
expect(subscriptionNotifications).toBe(0)
168+
store.dispatch(incrementBatched())
169+
vitest.runAllTimers()
170+
171+
expect(subscriptionNotifications).toBe(1)
172+
})
173+
174+
test('Notifies immediately if a non-batched action is dispatched', () => {
175+
store.dispatch(incrementBatched())
176+
expect(subscriptionNotifications).toBe(0)
177+
store.dispatch(incrementBatched())
178+
expect(subscriptionNotifications).toBe(0)
179+
store.dispatch(decrementUnbatched())
180+
expect(subscriptionNotifications).toBe(1)
181+
store.dispatch(incrementBatched())
182+
183+
vitest.runAllTimers()
184+
185+
expect(subscriptionNotifications).toBe(2)
186+
})
187+
188+
test('Does not notify at end of tick if last action was normal priority', () => {
189+
store.dispatch(incrementBatched())
190+
expect(subscriptionNotifications).toBe(0)
191+
store.dispatch(incrementBatched())
192+
expect(subscriptionNotifications).toBe(0)
193+
store.dispatch(decrementUnbatched())
194+
expect(subscriptionNotifications).toBe(1)
195+
store.dispatch(incrementBatched())
196+
store.dispatch(decrementUnbatched())
197+
expect(subscriptionNotifications).toBe(2)
198+
store.dispatch(decrementUnbatched())
199+
expect(subscriptionNotifications).toBe(3)
200+
201+
vitest.runAllTimers()
202+
203+
expect(subscriptionNotifications).toBe(3)
204+
})
205+
},
206+
)

0 commit comments

Comments
 (0)