11/* global describe, it */
22import { expect } from 'chai' ;
33import { reduxObservable } from 'redux-observable' ;
4- import { dispatchOnMount } from '../' ;
4+ import { dispatchOn } from '../' ;
55import { Component } from 'react' ;
66import { createStore , applyMiddleware } from 'redux' ;
77import * as Rx from 'rxjs' ;
88import 'babel-polyfill' ;
99
1010const { Observable } = Rx ;
1111
12- describe ( 'dispatchOnMount ' , ( ) => {
12+ describe ( 'dispatchOn ' , ( ) => {
1313 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' } ] ) ;
1535 } ) ;
1636
1737 it ( 'should wire a thunkservable to dispatch on componentDidMount' , ( ) => {
@@ -22,7 +42,7 @@ describe('dispatchOnMount', () => {
2242 } ;
2343 let store = createStore ( reducer , applyMiddleware ( reduxObservable ( ) ) ) ;
2444
25- @dispatchOnMount ( ( ) => ( ) => Observable . of ( { type : 'TEST' } ) )
45+ @dispatchOn ( { mount : [ ( ) => ( ) => Observable . of ( { type : 'TEST' } ) ] } )
2646 class TestComponent extends Component {
2747 }
2848
@@ -34,6 +54,65 @@ describe('dispatchOnMount', () => {
3454 expect ( reducedActions ) . to . deep . equal ( [ { type : '@@redux/INIT' } , { type : 'TEST' } ] ) ;
3555 } ) ;
3656
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+
37116 it ( 'should unsubscribe on componentWillUnmount' , ( ) => {
38117 let reducedActions = [ ] ;
39118 let reducer = ( state , action ) => {
@@ -56,7 +135,10 @@ describe('dispatchOnMount', () => {
56135 } ;
57136 } ) ;
58137
59- @dispatchOnMount ( ( ) => ( ) => source1 , ( ) => ( ) => source2 )
138+ @dispatchOn ( { mount : [
139+ ( ) => ( ) => source1 ,
140+ ( ) => ( ) => source2
141+ ] } )
60142 class TestComponent extends Component {
61143 }
62144
@@ -85,7 +167,10 @@ describe('dispatchOnMount', () => {
85167 let source1 = Observable . of ( { type : 'SOURCE1' } ) ;
86168 let source2 = Observable . of ( { type : 'SOURCE2' } ) ;
87169
88- @dispatchOnMount ( ( ) => ( ) => source1 , ( ) => ( ) => source2 )
170+ @dispatchOn ( { mount : [
171+ ( ) => ( ) => source1 ,
172+ ( ) => ( ) => source2
173+ ] } )
89174 class TestComponent extends Component {
90175 }
91176
@@ -111,7 +196,7 @@ describe('dispatchOnMount', () => {
111196
112197 let source2 = Observable . of ( { type : 'SOURCE2' } ) ;
113198
114- @dispatchOnMount ( ( ) => ( { type : 'PLAIN_ACTION' } ) , ( ) => ( ) => source2 )
199+ @dispatchOn ( { mount : [ ( ) => ( { type : 'PLAIN_ACTION' } ) , ( ) => ( ) => source2 ] } )
115200 class TestComponent extends Component {
116201 }
117202
@@ -139,9 +224,12 @@ describe('dispatchOnMount', () => {
139224 } ;
140225 let store = createStore ( reducer , applyMiddleware ( reduxObservable ( ) ) ) ;
141226
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+ } )
145233 class TestComponent extends Component {
146234 }
147235
0 commit comments