Skip to content

Commit f5539e3

Browse files
committed
Add tests for matching behavior
1 parent 49e5175 commit f5539e3

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dist
22
node_modules
3-
yarn-error.log
3+
yarn-error.log
4+
coverage

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { configureStore, createAction, AnyAction } from '@reduxjs/toolkit'
1+
import {
2+
configureStore,
3+
createAction,
4+
AnyAction,
5+
isAnyOf,
6+
} from '@reduxjs/toolkit'
27
import {
38
createActionListenerMiddleware,
49
addListenerAction,
@@ -27,6 +32,9 @@ describe('createActionListenerMiddleware', () => {
2732
const testAction1 = createAction<string>('testAction1')
2833
type TestAction1 = ReturnType<typeof testAction1>
2934
const testAction2 = createAction<string>('testAction2')
35+
type TestAction2 = ReturnType<typeof testAction2>
36+
const testAction3 = createAction<string>('testAction3')
37+
type TestAction3 = ReturnType<typeof testAction3>
3038

3139
beforeEach(() => {
3240
middleware = createActionListenerMiddleware()
@@ -52,6 +60,44 @@ describe('createActionListenerMiddleware', () => {
5260
])
5361
})
5462

63+
test('can subscribe with a string action type', () => {
64+
const listener = jest.fn((_: AnyAction) => {})
65+
66+
store.dispatch(addListenerAction(testAction2.type, listener))
67+
68+
store.dispatch(testAction2('b'))
69+
expect(listener.mock.calls).toEqual([[testAction2('b'), middlewareApi]])
70+
71+
store.dispatch(removeListenerAction(testAction2.type, listener))
72+
73+
store.dispatch(testAction2('b'))
74+
expect(listener.mock.calls).toEqual([[testAction2('b'), middlewareApi]])
75+
})
76+
77+
test('can subscribe with a matcher function', () => {
78+
const listener = jest.fn((_: AnyAction) => {})
79+
80+
const isAction1Or2 = isAnyOf(testAction1, testAction2)
81+
82+
const unsubscribe = middleware.addListener(isAction1Or2, listener)
83+
84+
store.dispatch(testAction1('a'))
85+
store.dispatch(testAction2('b'))
86+
store.dispatch(testAction3('c'))
87+
expect(listener.mock.calls).toEqual([
88+
[testAction1('a'), middlewareApi],
89+
[testAction2('b'), middlewareApi],
90+
])
91+
92+
unsubscribe()
93+
94+
store.dispatch(testAction2('b'))
95+
expect(listener.mock.calls).toEqual([
96+
[testAction1('a'), middlewareApi],
97+
[testAction2('b'), middlewareApi],
98+
])
99+
})
100+
55101
test('subscribing with the same listener will not make it trigger twice (like EventTarget.addEventListener())', () => {
56102
const listener = jest.fn((_: TestAction1) => {})
57103

0 commit comments

Comments
 (0)