Skip to content

Commit a0858eb

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

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,84 @@ 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+
157+
vitest.runAllTimers()
158+
159+
expect(subscriptionNotifications).toBe(4)
160+
})
161+
162+
test('Only notifies once if several batched actions are dispatched in a row', () => {
163+
store.dispatch(incrementBatched())
164+
expect(subscriptionNotifications).toBe(0)
165+
store.dispatch(incrementBatched())
166+
expect(subscriptionNotifications).toBe(0)
167+
store.dispatch(incrementBatched())
168+
expect(subscriptionNotifications).toBe(0)
169+
store.dispatch(incrementBatched())
170+
171+
vitest.runAllTimers()
172+
173+
expect(subscriptionNotifications).toBe(1)
174+
})
175+
176+
test('Notifies immediately if a non-batched action is dispatched', () => {
177+
store.dispatch(incrementBatched())
178+
expect(subscriptionNotifications).toBe(0)
179+
store.dispatch(incrementBatched())
180+
expect(subscriptionNotifications).toBe(0)
181+
store.dispatch(decrementUnbatched())
182+
expect(subscriptionNotifications).toBe(1)
183+
store.dispatch(incrementBatched())
184+
185+
vitest.runAllTimers()
186+
187+
expect(subscriptionNotifications).toBe(2)
188+
})
189+
190+
test('Does not notify at end of tick if last action was normal priority', () => {
191+
store.dispatch(incrementBatched())
192+
expect(subscriptionNotifications).toBe(0)
193+
store.dispatch(incrementBatched())
194+
expect(subscriptionNotifications).toBe(0)
195+
store.dispatch(decrementUnbatched())
196+
expect(subscriptionNotifications).toBe(1)
197+
store.dispatch(incrementBatched())
198+
store.dispatch(decrementUnbatched())
199+
expect(subscriptionNotifications).toBe(2)
200+
store.dispatch(decrementUnbatched())
201+
expect(subscriptionNotifications).toBe(3)
202+
203+
vitest.runAllTimers()
204+
205+
expect(subscriptionNotifications).toBe(3)
206+
})
207+
},
208+
)

0 commit comments

Comments
 (0)