1
1
import configureStore from "redux-mock-store" ;
2
- import { put } from "redux-saga/effects" ;
2
+ import { put , take , all } from "redux-saga/effects" ;
3
3
4
4
import createSagaMiddleware from "../saga" ;
5
5
@@ -17,13 +17,94 @@ describe("saga middleware ", () => {
17
17
const { runSaga, sagaMiddleware } = createSagaMiddleware ( ) ;
18
18
19
19
const store = configureStore ( [ sagaMiddleware ] ) ( { } ) ;
20
- const action = { type : "probe " } ;
20
+ store . wrap = ( type ) => `test_ ${ type } ` ;
21
21
22
22
runSaga ( store , function * ( ) {
23
- yield put ( action ) ;
23
+ yield put ( { type : "probe" } ) ;
24
24
} ) ;
25
25
26
- expect ( store . getActions ( ) ) . toEqual ( [ action ] ) ;
26
+ expect ( store . getActions ( ) ) . toEqual ( [ { type : "test_probe" } ] ) ;
27
+ } ) ;
28
+
29
+ describe ( "intercepts effects" , ( ) => {
30
+ const { runSaga, sagaMiddleware } = createSagaMiddleware ( ) ;
31
+
32
+ const store = configureStore ( [ sagaMiddleware ] ) ( { } ) ;
33
+
34
+ store . wrap = ( type ) => ( ! type || type . startsWith ( "@@" ) ? type : `test_${ type } ` ) ;
35
+
36
+ beforeEach ( ( ) => {
37
+ store . clearActions ( ) ;
38
+ } ) ;
39
+
40
+ it ( "all([])" , ( ) => {
41
+ runSaga ( store , function * ( ) {
42
+ yield all ( [ ] ) ;
43
+ } ) ;
44
+ } ) ;
45
+
46
+ it ( "put({})" , ( ) => {
47
+ runSaga ( store , function * ( ) {
48
+ yield put ( { type : null } ) ;
49
+ } ) ;
50
+ expect ( store . getActions ( ) ) . toEqual ( [ { type : null } ] ) ;
51
+ } ) ;
52
+
53
+ it ( 'put({ type: "@@BROADCAST/THING" })' , ( ) => {
54
+ runSaga ( store , function * ( ) {
55
+ yield put ( { type : "@@BROADCAST/THING" } ) ;
56
+ } ) ;
57
+ expect ( store . getActions ( ) ) . toEqual ( [ { type : "@@BROADCAST/THING" } ] ) ;
58
+ } ) ;
59
+
60
+ it ( 'put({ type: "THING" })' , ( ) => {
61
+ runSaga ( store , function * ( ) {
62
+ yield put ( { type : "THING" } ) ;
63
+ } ) ;
64
+ expect ( store . getActions ( ) ) . toEqual ( [ { type : "test_THING" } ] ) ;
65
+ } ) ;
66
+
67
+ it ( 'take("*")' , ( ) => {
68
+ const sniff = [ ] ;
69
+ runSaga ( store , function * ( ) {
70
+ sniff . push ( yield take ( ) ) ;
71
+ sniff . push ( yield take ( "*" ) ) ;
72
+ } ) ;
73
+
74
+ store . dispatch ( { type : "probe-1" } ) ;
75
+ store . dispatch ( { type : "probe-2" } ) ;
76
+ expect ( sniff ) . toEqual ( [ { type : "probe-1" } , { type : "probe-2" } ] ) ;
77
+ } ) ;
78
+
79
+ it ( 'take("REQUEST")' , ( ) => {
80
+ const sniff = [ ] ;
81
+ runSaga ( store , function * ( ) {
82
+ sniff . push ( yield take ( "REQUEST" ) ) ;
83
+ } ) ;
84
+
85
+ store . dispatch ( { type : "test_REQUEST" } ) ;
86
+ expect ( sniff ) . toEqual ( [ { type : "test_REQUEST" } ] ) ;
87
+ } ) ;
88
+
89
+ it ( 'take(["REQUEST"])' , ( ) => {
90
+ const sniff = [ ] ;
91
+ runSaga ( store , function * ( ) {
92
+ sniff . push ( yield take ( [ "REQUEST" ] ) ) ;
93
+ } ) ;
94
+
95
+ store . dispatch ( { type : "test_REQUEST" } ) ;
96
+ expect ( sniff ) . toEqual ( [ { type : "test_REQUEST" } ] ) ;
97
+ } ) ;
98
+
99
+ it ( "take(() => true)" , ( ) => {
100
+ const sniff = [ ] ;
101
+ runSaga ( store , function * ( ) {
102
+ sniff . push ( yield take ( ( ) => true ) ) ;
103
+ } ) ;
104
+
105
+ store . dispatch ( { type : "test_REQUEST" } ) ;
106
+ expect ( sniff ) . toEqual ( [ ] ) ;
107
+ } ) ;
27
108
} ) ;
28
109
} ) ;
29
110
} ) ;
0 commit comments