Add tests to Inventory module
[aai/sparky-fe.git] / test / app / inventory / InventoryActions.test.js
1 import configureMockStore from 'redux-mock-store';
2 import thunk from 'redux-thunk';
3 import fetchMock from 'fetch-mock';
4 import expect from 'expect';
5
6 import {onLoadTotalCountByDate, onCountByTypeLoad, onTopographicMapMounted} from 'app/inventory/InventoryActions';
7 import {InventoryActionTypes} from 'app/inventory/InventoryConstants';
8
9
10 const middlewares = [thunk];
11 const mockStore = configureMockStore(middlewares);
12
13
14 function mockRequestToEntityCountHistoryEndpoint(postfix, response) {
15     fetchMock.getOnce(
16         `http://localhost:/rest/visualization${postfix}`,
17         response
18     );
19 }
20
21 describe('InventoryActions', () => {
22
23     afterEach(() => {
24         fetchMock.restore()
25     });
26
27     describe('verify onLoadTotalCountByDate', () => {
28         it('creates COUNT_BY_DATE_SUCCESS response when there is no error', async () => {
29
30             // given
31             mockRequestToEntityCountHistoryEndpoint("/entityCountHistory?type=graph",{
32                 status: 200,
33                 body: {
34                     "result": {
35                         "keyA": "valueA",
36                         "keyB": "valueB"
37                     }
38                 }
39             });
40
41             const expectedActions = [
42                 {
43                     type: InventoryActionTypes.COUNT_BY_DATE_SUCCESS,
44                     data: {
45                         countByDate: {
46                             "keyA": "valueA",
47                             "keyB": "valueB"
48                         }
49                     }
50                 }];
51
52             const store = mockStore();
53
54             // when
55             await store.dispatch(onLoadTotalCountByDate());
56
57             // then
58             expect(store.getActions()).toEqual(expectedActions);
59         });
60
61
62         it('creates COUNT_BY_DATE_FAILED response when there is a problem with remote service', async () => {
63
64             // given
65             mockRequestToEntityCountHistoryEndpoint("/entityCountHistory?type=graph",{
66                 status: 500
67             });
68
69             const expectedActions = [
70                 {
71                     type: InventoryActionTypes.COUNT_BY_DATE_FAILED,
72                     data: {
73                         message: 'Error fetching data from server',
74                         severity: 'danger'
75                     }
76                 }];
77
78             const store = mockStore();
79
80             // when
81             await store.dispatch(onLoadTotalCountByDate());
82
83             // then
84             expect(store.getActions()).toEqual(expectedActions);
85         });
86     });
87
88
89
90     describe('verify onCountByTypeLoad', () => {
91         it('creates COUNT_BY_ENTITY_SUCCESS response when there is no error', async () => {
92
93             // given
94             mockRequestToEntityCountHistoryEndpoint("/entityCountHistory?type=table",{
95                 status: 200,
96                 body: {
97                     "result": {
98                         "keyA": "valueA",
99                         "keyB": "valueB"
100                     }
101                 }
102             });
103
104             const expectedActions = [
105                 {
106                     type: InventoryActionTypes.COUNT_BY_ENTITY_SUCCESS,
107                     data: {
108                         countByType: {
109                             "keyA": "valueA",
110                             "keyB": "valueB"
111                         }
112                     }
113                 }];
114
115             const store = mockStore();
116
117             // when
118             await store.dispatch(onCountByTypeLoad());
119
120             // then
121             expect(store.getActions()).toEqual(expectedActions);
122         });
123
124
125         it('creates COUNT_BY_ENTITY_FAILED response when there is a problem with remote service', async () => {
126
127             // given
128             mockRequestToEntityCountHistoryEndpoint("/entityCountHistory?type=table",{
129                 status: 500
130             });
131
132             const expectedActions = [
133                 {
134                     type: InventoryActionTypes.COUNT_BY_ENTITY_FAILED,
135                     data: {
136                         message: 'Error fetching data from server',
137                         severity: 'danger'
138                     }
139                 }];
140
141             const store = mockStore();
142
143             // when
144             await store.dispatch(onCountByTypeLoad());
145
146             // then
147             expect(store.getActions()).toEqual(expectedActions);
148         });
149     });
150
151
152
153     describe('verify onTopographicMapMounted', () => {
154         it('creates TOPOGRAPHIC_QUERY_SUCCESS response when there is no error', async () => {
155
156             // given
157             const requestObject = {
158                 entityType: "entityType"
159             };
160             mockRequestToEntityCountHistoryEndpoint("/geovisualization/?entity=entityType",{
161                 status: 200,
162                 body: {
163                     "plotPoints": {
164                         "keyA": "valueA",
165                         "keyB": "valueB"
166                     }
167                 }
168             });
169
170             const expectedActions = [
171                 {
172                     type: InventoryActionTypes.TOPOGRAPHIC_QUERY_SUCCESS,
173                     data: {
174                         plotPoints: {
175                             "keyA": "valueA",
176                             "keyB": "valueB"
177                         }
178                     }
179                 }];
180
181             const store = mockStore();
182
183             // when
184             await store.dispatch(onTopographicMapMounted(requestObject));
185
186             // then
187             expect(store.getActions()).toEqual(expectedActions);
188         });
189
190
191         it('creates TOPOGRAPHIC_QUERY_FAILED response when there is a problem with remote service', async () => {
192
193             // given
194             const requestObject = {
195                 entityType: "entityType"
196             };
197             mockRequestToEntityCountHistoryEndpoint("/geovisualization/?entity=entityType",{
198                 status: 500
199             });
200
201             const expectedActions = [
202                 {
203                     type: InventoryActionTypes.TOPOGRAPHIC_QUERY_FAILED,
204                     data: {
205                         message: 'Error fetching data from server',
206                         severity: 'danger'
207                     }
208                 }];
209
210             const store = mockStore();
211
212             // when
213             await store.dispatch(onTopographicMapMounted(requestObject));
214
215             // then
216             expect(store.getActions()).toEqual(expectedActions);
217         });
218     });
219 });