Skip to content

Commit

Permalink
Add failing test to capture issue reduxjs#4693 (autoBatchEnhancer wit…
Browse files Browse the repository at this point in the history
…h fake timers)
  • Loading branch information
ensconced committed Nov 11, 2024
1 parent 1b9ece9 commit a0858eb
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/toolkit/src/tests/autoBatchEnhancer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,84 @@ describe.each(cases)('autoBatchEnhancer: %j', (autoBatchOptions) => {
expect(subscriptionNotifications).toBe(3)
})
})

describe.each(cases)(
'autoBatchEnhancer with fake timers: %j',
(autoBatchOptions) => {
beforeAll(() => {
vitest.useFakeTimers({
toFake: ['setTimeout', 'queueMicrotask', 'requestAnimationFrame'],
})
})
afterAll(() => {
vitest.useRealTimers()
})
beforeEach(() => {
subscriptionNotifications = 0
store = makeStore(autoBatchOptions)

store.subscribe(() => {
subscriptionNotifications++
})
})
test('Does not alter normal subscription notification behavior', () => {
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(1)
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(2)
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(3)
store.dispatch(decrementUnbatched())

vitest.runAllTimers()

expect(subscriptionNotifications).toBe(4)
})

test('Only notifies once if several batched actions are dispatched in a row', () => {
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(incrementBatched())

vitest.runAllTimers()

expect(subscriptionNotifications).toBe(1)
})

test('Notifies immediately if a non-batched action is dispatched', () => {
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(1)
store.dispatch(incrementBatched())

vitest.runAllTimers()

expect(subscriptionNotifications).toBe(2)
})

test('Does not notify at end of tick if last action was normal priority', () => {
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(incrementBatched())
expect(subscriptionNotifications).toBe(0)
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(1)
store.dispatch(incrementBatched())
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(2)
store.dispatch(decrementUnbatched())
expect(subscriptionNotifications).toBe(3)

vitest.runAllTimers()

expect(subscriptionNotifications).toBe(3)
})
},
)

0 comments on commit a0858eb

Please sign in to comment.