1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+
7+ var _lodash = require ( 'lodash.flattendeep' ) ;
8+
9+ var _lodash2 = _interopRequireDefault ( _lodash ) ;
10+
11+ var _lodash3 = require ( 'lodash.find' ) ;
12+
13+ var _lodash4 = _interopRequireDefault ( _lodash3 ) ;
14+
15+ var _utils = require ( '../utils' ) ;
16+
17+ var _mockStore = require ( '../mockStore' ) ;
18+
19+ var _mockStore2 = _interopRequireDefault ( _mockStore ) ;
20+
21+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
22+
23+ function getDispatchedActions ( initialState , action ) {
24+ return new Promise ( function ( resolve , reject ) {
25+ var store = ( 0 , _mockStore2 . default ) ( ) ( initialState ) ;
26+ var dispatchResult = store . dispatch ( action ) ;
27+
28+ if ( dispatchResult instanceof Promise ) {
29+ dispatchResult . then ( function ( ) {
30+ resolve ( store . getActions ( ) ) ;
31+ } ) . catch ( function ( result ) {
32+ reject ( result ) ;
33+ } ) ;
34+ } else {
35+ resolve ( store . getActions ( ) ) ;
36+ }
37+ } ) ;
38+ }
39+
40+ function unrollActions ( initialState , expectedActions ) {
41+ var promises = [ ] ;
42+ var actions = ( 0 , _utils . toArray ) ( expectedActions ) ;
43+
44+ for ( var index = 0 ; index < actions . length ; index ++ ) {
45+ promises . push ( getDispatchedActions ( initialState , actions [ index ] ) ) ;
46+ }
47+
48+ return Promise . all ( promises ) . then ( function ( resultActions ) {
49+ return ( 0 , _lodash2 . default ) ( resultActions ) ;
50+ } ) ;
51+ }
52+
53+ function toDispatchActionsWithState ( initialState , actionUnderTest , expectedActions , done , fail ) {
54+ if ( ! ( 0 , _utils . isFunction ) ( actionUnderTest ) && ! ( 0 , _utils . isObject ) ( actionUnderTest ) ) {
55+ throw new Error ( 'The "actualAction" argument must be a function or object' ) ;
56+ }
57+
58+ if ( ! ( 0 , _utils . isFunction ) ( expectedActions ) && ! ( 0 , _utils . isObject ) ( expectedActions ) ) {
59+ throw new Error ( 'The "expectedActions" argument must be ' + 'an action creator function or an action object or an array of them' ) ;
60+ }
61+
62+ return getDispatchedActions ( initialState , actionUnderTest ) . then ( function ( dispatchedActions ) {
63+ return unrollActions ( initialState , expectedActions ) . then ( function ( expectedUnrolledActions ) {
64+ for ( var index = 0 ; index < expectedUnrolledActions . length ; index ++ ) {
65+ if ( ! ( 0 , _lodash4 . default ) ( dispatchedActions , expectedUnrolledActions [ index ] ) ) {
66+ throw new Error ( 'Expected action ' + JSON . stringify ( expectedUnrolledActions [ index ] ) + ' was not dispatched.' + ( '\nActual dispatched actions: ' + JSON . stringify ( dispatchedActions ) ) ) ;
67+ }
68+ }
69+ if ( ( 0 , _utils . isFunction ) ( done ) ) {
70+ done ( ) ;
71+ }
72+ } ) . catch ( function ( err ) {
73+ if ( ( 0 , _utils . isFunction ) ( fail ) ) {
74+ fail ( err ) ;
75+ return ;
76+ } else if ( ( 0 , _utils . isFunction ) ( done ) ) {
77+ done ( err ) ;
78+ return ;
79+ }
80+ throw new Error ( err ) ;
81+ } ) ;
82+ } ) ;
83+ }
84+
85+ exports . default = toDispatchActionsWithState ;
0 commit comments