Skip to content

Commit 072a430

Browse files
committed
Add test for getOriginalState behavior
1 parent d17b301 commit 072a430

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

packages/action-listener-middleware/src/tests/listenerMiddleware.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,48 @@ describe('createActionListenerMiddleware', () => {
376376
}
377377
)
378378

379+
test('Passes both getState and getOriginalState in the API', () => {
380+
const store = configureStore({
381+
reducer: counterSlice.reducer,
382+
middleware: (gDM) => gDM().prepend(middleware),
383+
})
384+
385+
let listener1Calls = 0
386+
middleware.addListener(
387+
increment,
388+
(action, listenerApi) => {
389+
// TODO getState functions aren't typed right here
390+
const stateBefore = listenerApi.getOriginalState() as CounterState
391+
const currentState = listenerApi.getOriginalState() as CounterState
392+
393+
listener1Calls++
394+
// In the "before" phase, we pass the same state
395+
expect(currentState).toBe(stateBefore)
396+
},
397+
{ when: 'beforeReducer' }
398+
)
399+
400+
let listener2Calls = 0
401+
middleware.addListener(
402+
increment,
403+
(action, listenerApi) => {
404+
// TODO getState functions aren't typed right here
405+
const stateBefore = listenerApi.getOriginalState() as CounterState
406+
const currentState = listenerApi.getOriginalState() as CounterState
407+
408+
listener2Calls++
409+
// In the "after" phase, we pass the new state for `getState`, and still have original state too
410+
expect(currentState.value).toBe(stateBefore.value + 1)
411+
},
412+
{ when: 'afterReducer' }
413+
)
414+
415+
store.dispatch(increment())
416+
417+
expect(listener1Calls).toBe(1)
418+
expect(listener2Calls).toBe(1)
419+
})
420+
379421
test('mixing "before" and "after"', () => {
380422
const calls: Function[] = []
381423
function before1() {

0 commit comments

Comments
 (0)