File tree Expand file tree Collapse file tree 3 files changed +47
-2
lines changed Expand file tree Collapse file tree 3 files changed +47
-2
lines changed Original file line number Diff line number Diff line change 5
5
"proxy" : " http://www.weather.com.cn/" ,
6
6
"devDependencies" : {
7
7
"react-scripts" : " 0.8.4" ,
8
- "redux-immutable-state-invariant" : " ^1.2.4"
8
+ "redux-immutable-state-invariant" : " ^1.2.4" ,
9
+ "redux-mock-store" : " ^1.2.1" ,
10
+ "sinon" : " ^1.17.7"
9
11
},
10
12
"dependencies" : {
11
13
"react" : " ^15.4.1" ,
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ export const fetchWeather = (cityCode) => {
20
20
21
21
dispatch ( fetchWeatherStarted ( ) )
22
22
23
- fetch ( apiUrl ) . then ( ( response ) => {
23
+ return fetch ( apiUrl ) . then ( ( response ) => {
24
24
if ( response . status !== 200 ) {
25
25
throw new Error ( 'Fail to get response with status ' + response . status ) ;
26
26
}
Original file line number Diff line number Diff line change
1
+ import thunk from 'redux-thunk' ;
2
+ import { stub } from 'sinon' ;
3
+ import configureStore from 'redux-mock-store' ;
4
+
5
+ import * as actions from '../../src/weather/actions.js' ;
6
+ import * as actionTypes from '../../src/weather/actionTypes.js' ;
7
+
8
+ const middlewares = [ thunk ] ;
9
+ const createMockStore = configureStore ( middlewares ) ;
10
+
11
+
12
+ describe ( 'weather/actions' , ( ) => {
13
+ describe ( 'fetchWeather' , ( ) => {
14
+ let stubbedFetch ;
15
+ const store = createMockStore ( ) ;
16
+
17
+ beforeEach ( ( ) => {
18
+ stubbedFetch = stub ( global , 'fetch' ) ;
19
+ } ) ;
20
+
21
+ afterEach ( ( ) => {
22
+ stubbedFetch . restore ( ) ;
23
+ } ) ;
24
+
25
+ it ( 'should dispatch fetchWeatherSuccess action type on fetch success' , ( ) => {
26
+ const mockResponse = Promise . resolve ( {
27
+ status : 200 ,
28
+ json : ( ) => Promise . resolve ( {
29
+ weatherinfo : { }
30
+ } )
31
+ } ) ;
32
+ stubbedFetch . returns ( mockResponse ) ;
33
+
34
+ return store . dispatch ( actions . fetchWeather ( 1 ) ) . then ( ( ) => {
35
+ const dispatchedActions = store . getActions ( ) ;
36
+ expect ( dispatchedActions . length ) . toBe ( 2 ) ;
37
+ expect ( dispatchedActions [ 0 ] . type ) . toBe ( actionTypes . FETCH_STARTED ) ;
38
+ expect ( dispatchedActions [ 1 ] . type ) . toBe ( actionTypes . FETCH_SUCCESS ) ;
39
+ } ) ;
40
+ } ) ;
41
+
42
+ } ) ;
43
+ } ) ;
You can’t perform that action at this time.
0 commit comments