source: node_modules/@reduxjs/toolkit/src/tests/autoBatchEnhancer.test.ts@ ba17441

Last change on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Added visualizations

  • Property mode set to 100644
File size: 6.1 KB
Line 
1import { configureStore } from '../configureStore'
2import { createSlice } from '../createSlice'
3import type { AutoBatchOptions } from '../autoBatchEnhancer'
4import { autoBatchEnhancer, prepareAutoBatched } from '../autoBatchEnhancer'
5import { delay } from '../utils'
6import { debounce } from 'lodash'
7
8interface CounterState {
9 value: number
10}
11
12const counterSlice = createSlice({
13 name: 'counter',
14 initialState: { value: 0 } as CounterState,
15 reducers: {
16 incrementBatched: {
17 // Batched, low-priority
18 reducer(state) {
19 state.value += 1
20 },
21 prepare: prepareAutoBatched<void>(),
22 },
23 // Not batched, normal priority
24 decrementUnbatched(state) {
25 state.value -= 1
26 },
27 },
28})
29const { incrementBatched, decrementUnbatched } = counterSlice.actions
30
31const makeStore = (autoBatchOptions?: AutoBatchOptions) => {
32 return configureStore({
33 reducer: counterSlice.reducer,
34 enhancers: (getDefaultEnhancers) =>
35 getDefaultEnhancers({
36 autoBatch: autoBatchOptions,
37 }),
38 })
39}
40
41let store: ReturnType<typeof makeStore>
42
43let subscriptionNotifications = 0
44
45const cases: AutoBatchOptions[] = [
46 { type: 'tick' },
47 { type: 'raf' },
48 { type: 'timer', timeout: 0 },
49 { type: 'timer', timeout: 10 },
50 { type: 'timer', timeout: 20 },
51 {
52 type: 'callback',
53 queueNotification: debounce((notify: () => void) => {
54 notify()
55 }, 5),
56 },
57]
58
59describe.each(cases)('autoBatchEnhancer: %j', (autoBatchOptions) => {
60 beforeEach(() => {
61 subscriptionNotifications = 0
62 store = makeStore(autoBatchOptions)
63
64 store.subscribe(() => {
65 subscriptionNotifications++
66 })
67 })
68 test('Does not alter normal subscription notification behavior', async () => {
69 store.dispatch(decrementUnbatched())
70 expect(subscriptionNotifications).toBe(1)
71 store.dispatch(decrementUnbatched())
72 expect(subscriptionNotifications).toBe(2)
73 store.dispatch(decrementUnbatched())
74 expect(subscriptionNotifications).toBe(3)
75 store.dispatch(decrementUnbatched())
76
77 await delay(25)
78
79 expect(subscriptionNotifications).toBe(4)
80 })
81
82 test('Only notifies once if several batched actions are dispatched in a row', async () => {
83 store.dispatch(incrementBatched())
84 expect(subscriptionNotifications).toBe(0)
85 store.dispatch(incrementBatched())
86 expect(subscriptionNotifications).toBe(0)
87 store.dispatch(incrementBatched())
88 expect(subscriptionNotifications).toBe(0)
89 store.dispatch(incrementBatched())
90
91 await delay(25)
92
93 expect(subscriptionNotifications).toBe(1)
94 })
95
96 test('Notifies immediately if a non-batched action is dispatched', async () => {
97 store.dispatch(incrementBatched())
98 expect(subscriptionNotifications).toBe(0)
99 store.dispatch(incrementBatched())
100 expect(subscriptionNotifications).toBe(0)
101 store.dispatch(decrementUnbatched())
102 expect(subscriptionNotifications).toBe(1)
103 store.dispatch(incrementBatched())
104
105 await delay(25)
106
107 expect(subscriptionNotifications).toBe(2)
108 })
109
110 test('Does not notify at end of tick if last action was normal priority', async () => {
111 store.dispatch(incrementBatched())
112 expect(subscriptionNotifications).toBe(0)
113 store.dispatch(incrementBatched())
114 expect(subscriptionNotifications).toBe(0)
115 store.dispatch(decrementUnbatched())
116 expect(subscriptionNotifications).toBe(1)
117 store.dispatch(incrementBatched())
118 store.dispatch(decrementUnbatched())
119 expect(subscriptionNotifications).toBe(2)
120 store.dispatch(decrementUnbatched())
121 expect(subscriptionNotifications).toBe(3)
122
123 await delay(25)
124
125 expect(subscriptionNotifications).toBe(3)
126 })
127})
128
129describe.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)
Note: See TracBrowser for help on using the repository browser.