1
- import { configureStore , createAction , AnyAction } from '@reduxjs/toolkit'
1
+ import {
2
+ configureStore ,
3
+ createAction ,
4
+ AnyAction ,
5
+ isAnyOf ,
6
+ } from '@reduxjs/toolkit'
2
7
import {
3
8
createActionListenerMiddleware ,
4
9
addListenerAction ,
@@ -27,6 +32,9 @@ describe('createActionListenerMiddleware', () => {
27
32
const testAction1 = createAction < string > ( 'testAction1' )
28
33
type TestAction1 = ReturnType < typeof testAction1 >
29
34
const testAction2 = createAction < string > ( 'testAction2' )
35
+ type TestAction2 = ReturnType < typeof testAction2 >
36
+ const testAction3 = createAction < string > ( 'testAction3' )
37
+ type TestAction3 = ReturnType < typeof testAction3 >
30
38
31
39
beforeEach ( ( ) => {
32
40
middleware = createActionListenerMiddleware ( )
@@ -52,6 +60,44 @@ describe('createActionListenerMiddleware', () => {
52
60
] )
53
61
} )
54
62
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
+
55
101
test ( 'subscribing with the same listener will not make it trigger twice (like EventTarget.addEventListener())' , ( ) => {
56
102
const listener = jest . fn ( ( _ : TestAction1 ) => { } )
57
103
0 commit comments