1
1
/* global describe, it */
2
2
import { expect } from 'chai' ;
3
3
import { reduxObservable } from 'redux-observable' ;
4
- import { dispatchOnMount } from '../' ;
4
+ import { dispatchOn } from '../' ;
5
5
import { Component } from 'react' ;
6
6
import { createStore , applyMiddleware } from 'redux' ;
7
7
import * as Rx from 'rxjs' ;
8
8
import 'babel-polyfill' ;
9
9
10
10
const { Observable } = Rx ;
11
11
12
- describe ( 'dispatchOnMount ' , ( ) => {
12
+ describe ( 'dispatchOn ' , ( ) => {
13
13
it ( 'should exist' , ( ) => {
14
- expect ( dispatchOnMount ) . to . be . a ( 'function' ) ;
14
+ expect ( dispatchOn ) . to . be . a ( 'function' ) ;
15
+ } ) ;
16
+
17
+ it ( 'should wire a thunkservable to dispatch on componentWillMount' , ( ) => {
18
+ let reducedActions = [ ] ;
19
+ let reducer = ( state , action ) => {
20
+ reducedActions . push ( action ) ;
21
+ return state ;
22
+ } ;
23
+ let store = createStore ( reducer , applyMiddleware ( reduxObservable ( ) ) ) ;
24
+
25
+ @dispatchOn ( { willMount : [ ( ) => ( ) => Observable . of ( { type : 'TEST' } ) ] } )
26
+ class TestComponent extends Component {
27
+ }
28
+
29
+ let comp = new TestComponent ( ) ;
30
+ // fake connection?
31
+ comp . context = { store } ;
32
+ comp . componentWillMount ( ) ;
33
+
34
+ expect ( reducedActions ) . to . deep . equal ( [ { type : '@@redux/INIT' } , { type : 'TEST' } ] ) ;
15
35
} ) ;
16
36
17
37
it ( 'should wire a thunkservable to dispatch on componentDidMount' , ( ) => {
@@ -22,7 +42,7 @@ describe('dispatchOnMount', () => {
22
42
} ;
23
43
let store = createStore ( reducer , applyMiddleware ( reduxObservable ( ) ) ) ;
24
44
25
- @dispatchOnMount ( ( ) => ( ) => Observable . of ( { type : 'TEST' } ) )
45
+ @dispatchOn ( { mount : [ ( ) => ( ) => Observable . of ( { type : 'TEST' } ) ] } )
26
46
class TestComponent extends Component {
27
47
}
28
48
@@ -34,6 +54,65 @@ describe('dispatchOnMount', () => {
34
54
expect ( reducedActions ) . to . deep . equal ( [ { type : '@@redux/INIT' } , { type : 'TEST' } ] ) ;
35
55
} ) ;
36
56
57
+ it ( 'should wire a thunkservable to dispatch on componentDidUpdate' , ( ) => {
58
+ let reducedActions = [ ] ;
59
+ let reducer = ( state , action ) => {
60
+ reducedActions . push ( action ) ;
61
+ return state ;
62
+ } ;
63
+ let store = createStore ( reducer , applyMiddleware ( reduxObservable ( ) ) ) ;
64
+ let _prevProps ;
65
+ let _thisProps ;
66
+
67
+ @dispatchOn ( { update : [ ( thisProps , prevProps ) => {
68
+ _thisProps = thisProps ;
69
+ _prevProps = prevProps ;
70
+ return ( ) => Observable . of ( { type : 'TEST' } ) ;
71
+ } ] } )
72
+ class TestComponent extends Component {
73
+ }
74
+
75
+ let comp = new TestComponent ( ) ;
76
+ // fake connection?
77
+ comp . context = { store } ;
78
+ comp . props = { some : 'props' } ;
79
+ comp . componentDidUpdate ( { prev : 'props' } ) ;
80
+
81
+ expect ( _thisProps ) . to . deep . equal ( { some : 'props' } ) ;
82
+ expect ( _prevProps ) . to . deep . equal ( { prev : 'props' } ) ;
83
+ expect ( reducedActions ) . to . deep . equal ( [ { type : '@@redux/INIT' } , { type : 'TEST' } ] ) ;
84
+ } ) ;
85
+
86
+ it ( 'should wire a thunkservable to dispatch on componentWillReceiveProps' , ( ) => {
87
+ let reducedActions = [ ] ;
88
+ let reducer = ( state , action ) => {
89
+ reducedActions . push ( action ) ;
90
+ return state ;
91
+ } ;
92
+ let store = createStore ( reducer , applyMiddleware ( reduxObservable ( ) ) ) ;
93
+
94
+ let _thisProps ;
95
+ let _nextProps ;
96
+
97
+ @dispatchOn ( { willRecieveProps : [ ( thisProps , nextProps ) => {
98
+ _thisProps = thisProps ;
99
+ _nextProps = nextProps ;
100
+ return ( ) => Observable . of ( { type : 'TEST' } ) ;
101
+ } ] } )
102
+ class TestComponent extends Component {
103
+ }
104
+
105
+ let comp = new TestComponent ( ) ;
106
+ // fake connection?
107
+ comp . context = { store } ;
108
+ comp . props = { some : 'props' } ;
109
+ comp . componentWillReceiveProps ( { next : 'props' } ) ;
110
+
111
+ expect ( _thisProps ) . to . deep . equal ( { some : 'props' } ) ;
112
+ expect ( _nextProps ) . to . deep . equal ( { next : 'props' } ) ;
113
+ expect ( reducedActions ) . to . deep . equal ( [ { type : '@@redux/INIT' } , { type : 'TEST' } ] ) ;
114
+ } ) ;
115
+
37
116
it ( 'should unsubscribe on componentWillUnmount' , ( ) => {
38
117
let reducedActions = [ ] ;
39
118
let reducer = ( state , action ) => {
@@ -56,7 +135,10 @@ describe('dispatchOnMount', () => {
56
135
} ;
57
136
} ) ;
58
137
59
- @dispatchOnMount ( ( ) => ( ) => source1 , ( ) => ( ) => source2 )
138
+ @dispatchOn ( { mount : [
139
+ ( ) => ( ) => source1 ,
140
+ ( ) => ( ) => source2
141
+ ] } )
60
142
class TestComponent extends Component {
61
143
}
62
144
@@ -85,7 +167,10 @@ describe('dispatchOnMount', () => {
85
167
let source1 = Observable . of ( { type : 'SOURCE1' } ) ;
86
168
let source2 = Observable . of ( { type : 'SOURCE2' } ) ;
87
169
88
- @dispatchOnMount ( ( ) => ( ) => source1 , ( ) => ( ) => source2 )
170
+ @dispatchOn ( { mount : [
171
+ ( ) => ( ) => source1 ,
172
+ ( ) => ( ) => source2
173
+ ] } )
89
174
class TestComponent extends Component {
90
175
}
91
176
@@ -111,7 +196,7 @@ describe('dispatchOnMount', () => {
111
196
112
197
let source2 = Observable . of ( { type : 'SOURCE2' } ) ;
113
198
114
- @dispatchOnMount ( ( ) => ( { type : 'PLAIN_ACTION' } ) , ( ) => ( ) => source2 )
199
+ @dispatchOn ( { mount : [ ( ) => ( { type : 'PLAIN_ACTION' } ) , ( ) => ( ) => source2 ] } )
115
200
class TestComponent extends Component {
116
201
}
117
202
@@ -139,9 +224,12 @@ describe('dispatchOnMount', () => {
139
224
} ;
140
225
let store = createStore ( reducer , applyMiddleware ( reduxObservable ( ) ) ) ;
141
226
142
- @dispatchOnMount (
143
- ( props ) => ( { type : 'PLAIN_ACTION' , value : props . value } ) ,
144
- ( props ) => ( ) => Observable . of ( { type : 'SOURCE2' , value : props . value } ) )
227
+ @dispatchOn ( {
228
+ mount : [
229
+ ( props ) => ( { type : 'PLAIN_ACTION' , value : props . value } ) ,
230
+ ( props ) => ( ) => Observable . of ( { type : 'SOURCE2' , value : props . value } )
231
+ ]
232
+ } )
145
233
class TestComponent extends Component {
146
234
}
147
235
0 commit comments