1
+ import nock from 'nock' ;
2
+ import sinon from 'sinon' ;
3
+ import expect from 'expect.js' ;
4
+ import configureStore from 'redux-mock-store' ;
5
+ import { createStore , applyMiddleware } from 'redux' ;
6
+ import { apiMiddleware , CALL_API } from 'redux-api-middleware' ;
7
+
8
+ import * as types from 'constants/action-types' ;
9
+ import { fetchLatestVersion , fetchGitVersion } from 'actions/version' ;
10
+
11
+ const createMockStore = configureStore ( [ apiMiddleware ] ) ;
12
+
13
+ describe ( 'redux-api-middleware::version' , ( ) => {
14
+ describe ( 'fetchLatestVersion successfully' , ( ) => {
15
+ const mockPayload = { message : 'success' } ;
16
+
17
+ beforeEach ( ( ) => {
18
+ nock ( 'http://localhost:8448' )
19
+ . get ( '/api/latestversion' )
20
+ . reply ( 200 , mockPayload ) ;
21
+ } ) ;
22
+
23
+ it ( 'should dispatch \'FETCH_LATEST_VERSION_REQUEST\'' , done => {
24
+ const store = createMockStore ( { } ) ;
25
+
26
+ store . subscribe ( ( ) => {
27
+ const dispatchedActions = store . getActions ( ) ;
28
+ if ( dispatchedActions . length === 1 ) {
29
+ expect ( dispatchedActions [ 0 ] . type ) . to . eql ( types . FETCH_LATEST_VERSION_REQUEST ) ;
30
+ done ( ) ;
31
+ }
32
+ } ) ;
33
+ store . dispatch ( fetchLatestVersion ( ) ) ;
34
+ } ) ;
35
+
36
+ it ( 'should dispatch \'FETCH_USER_CONFIG_SUCCESS\'' , done => {
37
+ const store = createMockStore ( { } ) ;
38
+
39
+ store . subscribe ( ( ) => {
40
+ const dispatchedActions = store . getActions ( ) ;
41
+ if ( dispatchedActions . length === 2 ) {
42
+ expect ( dispatchedActions [ 0 ] . type ) . to . eql ( types . FETCH_LATEST_VERSION_REQUEST ) ;
43
+ expect ( dispatchedActions [ 1 ] . type ) . to . eql ( types . FETCH_LATEST_VERSION_SUCCESS ) ;
44
+ expect ( dispatchedActions [ 1 ] . payload ) . to . eql ( mockPayload ) ;
45
+ done ( ) ;
46
+ }
47
+ } ) ;
48
+ store . dispatch ( fetchLatestVersion ( ) ) ;
49
+ } ) ;
50
+ } ) ;
51
+
52
+ describe ( 'fetchLatestVersion fails' , ( ) => {
53
+
54
+ beforeEach ( ( ) => {
55
+ nock ( 'http://localhost:8448' )
56
+ . get ( '/api/latestversion' )
57
+ . reply ( 500 , null ) ;
58
+ } ) ;
59
+
60
+ it ( 'should dispatch \'FETCH_LATEST_VERSION_FAILURE\'' , done => {
61
+ const store = createMockStore ( { } ) ;
62
+
63
+ store . subscribe ( ( ) => {
64
+ const dispatchedActions = store . getActions ( ) ;
65
+ if ( dispatchedActions . length === 2 ) {
66
+ expect ( dispatchedActions [ 0 ] . type ) . to . eql ( types . FETCH_LATEST_VERSION_REQUEST ) ;
67
+ expect ( dispatchedActions [ 1 ] . type ) . to . eql ( types . FETCH_LATEST_VERSION_FAILURE ) ;
68
+ expect ( dispatchedActions [ 1 ] . payload ) . to . be . an ( 'object' ) ;
69
+ done ( ) ;
70
+ }
71
+ } ) ;
72
+ store . dispatch ( fetchLatestVersion ( ) ) ;
73
+ } ) ;
74
+ } ) ;
75
+
76
+ describe ( 'fetchGitVersion successfully' , ( ) => {
77
+ const mockPayload = { message : 'success' } ;
78
+
79
+ beforeEach ( ( ) => {
80
+ nock ( 'http://localhost:8448' )
81
+ . get ( '/api/gitversion' )
82
+ . reply ( 200 , mockPayload ) ;
83
+ } ) ;
84
+
85
+ it ( 'should dispatch \'FETCH_GIT_VERSION_REQUEST\'' , done => {
86
+ const store = createMockStore ( { } ) ;
87
+
88
+ store . subscribe ( ( ) => {
89
+ const dispatchedActions = store . getActions ( ) ;
90
+ if ( dispatchedActions . length === 1 ) {
91
+ expect ( dispatchedActions [ 0 ] . type ) . to . eql ( types . FETCH_GIT_VERSION_REQUEST ) ;
92
+ done ( ) ;
93
+ }
94
+ } ) ;
95
+ store . dispatch ( fetchGitVersion ( ) ) ;
96
+ } ) ;
97
+
98
+ it ( 'should dispatch \'FETCH_GIT_VERSION_SUCCESS\'' , done => {
99
+ const store = createMockStore ( { } ) ;
100
+
101
+ store . subscribe ( ( ) => {
102
+ const dispatchedActions = store . getActions ( ) ;
103
+ if ( dispatchedActions . length === 2 ) {
104
+ expect ( dispatchedActions [ 0 ] . type ) . to . eql ( types . FETCH_GIT_VERSION_REQUEST ) ;
105
+ expect ( dispatchedActions [ 1 ] . type ) . to . eql ( types . FETCH_GIT_VERSION_SUCCESS ) ;
106
+ expect ( dispatchedActions [ 1 ] . payload ) . to . eql ( mockPayload ) ;
107
+ done ( ) ;
108
+ }
109
+ } ) ;
110
+ store . dispatch ( fetchGitVersion ( ) ) ;
111
+ } ) ;
112
+ } ) ;
113
+
114
+ describe ( 'fetchGitVersion fails' , ( ) => {
115
+
116
+ beforeEach ( ( ) => {
117
+ nock ( 'http://localhost:8448' )
118
+ . get ( '/api/gitversion' )
119
+ . reply ( 500 , null ) ;
120
+ } ) ;
121
+
122
+ it ( 'should dispatch \'FETCH_GIT_VERSION_FAILURE\'' , done => {
123
+ const store = createMockStore ( { } ) ;
124
+
125
+ store . subscribe ( ( ) => {
126
+ const dispatchedActions = store . getActions ( ) ;
127
+ if ( dispatchedActions . length === 2 ) {
128
+ expect ( dispatchedActions [ 0 ] . type ) . to . eql ( types . FETCH_GIT_VERSION_REQUEST ) ;
129
+ expect ( dispatchedActions [ 1 ] . type ) . to . eql ( types . FETCH_GIT_VERSION_FAILURE ) ;
130
+ expect ( dispatchedActions [ 1 ] . payload ) . to . be . an ( 'object' ) ;
131
+ done ( ) ;
132
+ }
133
+ } ) ;
134
+ store . dispatch ( fetchGitVersion ( ) ) ;
135
+ } ) ;
136
+ } ) ;
137
+ } ) ;
0 commit comments