TierSupportActions test
[aai/sparky-fe.git] / test / app / tierSupport / TierSupportActions.test.js
1 import configureStore from 'redux-mock-store';
2 import thunk from 'redux-thunk'
3 import {
4   onNodeDetailsChange,
5   splitPaneResize,
6   onNodeMenuChange,
7   clearVIData,
8   setNotificationText,
9   fetchSelectedNodeElement,
10   querySelectedNodeElement
11 }
12 from 'app/tierSupport/TierSupportActions';
13 import {tierSupportActionTypes} from 'app/tierSupport/TierSupportConstants';
14 import {MESSAGE_LEVEL_WARNING} from 'utils/GlobalConstants';
15 import {globalInlineMessageBarActionTypes} from 'app/globalInlineMessageBar/GlobalInlineMessageBarConstants';
16 import {
17   NO_RESULTS_FOUND,
18   ERROR_RETRIEVING_DATA
19 } from 'app/networking/NetworkConstants';
20 import networkCall from 'app/networking/NetworkCalls';
21
22 const mockStore = configureStore([thunk]);
23
24 describe('TierSupportActionTests', () => {
25   let store;
26
27   beforeEach(() => {
28     store = mockStore({ tierSupportReducer: {} });
29   });
30
31   describe('onNodeDetailsChange', () => {
32     it('emits TS_GRAPH_NODE_SELECTED with payload', () => {
33       // Given
34       const newDetails = {
35         testDetails: 'Test Details',
36       };
37       const expectedActions = [{
38         type: tierSupportActionTypes.TS_GRAPH_NODE_SELECTED,
39         data: newDetails
40       }];
41
42       // When
43       store.dispatch(onNodeDetailsChange(newDetails));
44
45       // Then
46       expect(store.getActions()).toEqual(expectedActions);
47     });
48   });
49
50   describe('splitPaneResize', () => {
51     it('emits SPLIT_PANE_RESIZE action with payload', () => {
52       // Given
53       const initialLoad = {
54         test: 'message'
55       };
56       const expectedActions = [{
57         type: tierSupportActionTypes.SPLIT_PANE_RESIZE,
58         data: initialLoad
59       }];
60
61       // When
62       store.dispatch(splitPaneResize(initialLoad));
63
64       // Then
65       expect(store.getActions()).toEqual(expectedActions);
66     });
67   });
68
69   describe('onNodeMenuChange', () => {
70     it('emits TS_GRAPH_NODE_MENU_SELECTED action with payload', () => {
71       // Given
72       const selectedMenu = {
73         test: 'menuData'
74       };
75       const expectedActions = [{
76         type: tierSupportActionTypes.TS_GRAPH_NODE_MENU_SELECTED,
77         data: selectedMenu
78       }];
79
80       // When
81       store.dispatch(onNodeMenuChange(selectedMenu));
82
83       // Then
84       expect(store.getActions()).toEqual(expectedActions);
85     });
86   });
87
88   describe('clearVIData', () => {
89     it('emits TIER_SUPPORT_CLEAR_DATA action', () => {
90       // Given
91       const expectedActions = [{
92         type: tierSupportActionTypes.TIER_SUPPORT_CLEAR_DATA
93       }];
94
95       // When
96       store.dispatch(clearVIData());
97
98       // Then
99       expect(store.getActions()).toEqual(expectedActions);
100     });
101   });
102
103   describe('fetchSelectedNodeElement', () => {
104     it('emits actions with proper error message when 204 code returned', async () => {
105       // Given
106       const promise = () => getPromiseWithStatusCode(204);
107       const expectedActions = [
108         {
109           type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK,
110         },
111         {
112           type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
113           data: {errorMsg: NO_RESULTS_FOUND}
114         }
115       ];
116
117       // When
118       await store.dispatch(fetchSelectedNodeElement(promise));
119
120       // Then
121       expect(store.getActions()).toEqual(expectedActions);
122     });
123
124     it('emits actions with proper error message when 3XX code returned', async () => {
125       // Given
126       const promise = () => getPromiseWithStatusCode(301);
127       const expectedActions = [
128         {
129           type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK,
130         },
131         {
132           type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
133           data: {errorMsg: NO_RESULTS_FOUND}
134         }
135       ];
136
137       // When
138       await store.dispatch(fetchSelectedNodeElement(promise));
139
140       // Then
141       expect(store.getActions()).toEqual(expectedActions);
142     });
143
144     it('emits actions with proper error message when 5XX code returned', async () => {
145       // Given
146       const promise = () => getPromiseWithStatusCode(501);
147       const expectedActions = [
148         {
149           type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK,
150         },
151         {
152           type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
153           data: {value: ERROR_RETRIEVING_DATA, errorMsg: ERROR_RETRIEVING_DATA}
154         }
155       ];
156
157       // When
158       await store.dispatch(fetchSelectedNodeElement(promise));
159
160       // Then
161       expect(store.getActions()).toEqual(expectedActions);
162     });
163
164     it('emits actions with payload when 200 code returned and nodes not empty', async () => {
165       // Given
166       const {nodes, node1} = prepareTestNodes();
167       const promise = () => getNodesPromise(nodes);
168       const expectedActions = [
169         {
170           type: tierSupportActionTypes.TS_NODE_SEARCH_RESULTS,
171           data: {"nodes": nodes}
172         },
173         {
174           type: tierSupportActionTypes.TS_GRAPH_NODE_SELECTED,
175           data: node1
176         },
177         {
178           type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK
179         }
180       ];
181
182       // When
183       await store.dispatch(fetchSelectedNodeElement(promise));
184
185       // Then
186       expect(store.getActions()).toEqual(expectedActions);
187     });
188
189     it('emits actions with payload when 200 code returned and nodes empty', async () => {
190       // Given
191       const promise = () => getNodesPromise([]);
192       const expectedActions = [
193         {
194           type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
195           data: {errorMsg: NO_RESULTS_FOUND}
196         },
197         {
198           type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK
199         }
200       ];
201
202       // When
203       await store.dispatch(fetchSelectedNodeElement(promise));
204
205       // Then
206       expect(store.getActions()).toEqual(expectedActions);
207     });
208   });
209
210   describe('querySelectedNodeElement', () => {
211     const searchHash = "testHash";
212
213     it('emits actions when fetchRequest defined ', async () => {
214       // Given
215       const promise = () => getNodesPromise([]);
216       const expectedAction = [
217         {
218           type: tierSupportActionTypes.TIER_SUPPORT_ACTIVATE_BUSY_FEEDBACK,
219         },
220         {
221           type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
222           data: {errorMsg: NO_RESULTS_FOUND}
223         },
224         {
225           type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK
226         }
227       ];
228
229       // When
230       await store.dispatch(querySelectedNodeElement(searchHash, promise));
231
232       // Then
233       expect(store.getActions()).toEqual(expectedAction);
234     });
235
236     it('builds request and emits actions when fetchRequest undefined ', async () => {
237       // Given
238       const stringifySpy = jest.spyOn(JSON, 'stringify');
239       const promise = getNodesPromise([]);
240       networkCall.fetchRequestObj = jest.fn(() => promise);
241       const expectedStringifyInput = {
242         hashId: searchHash
243       };
244       const expectedFetchRequestBody = "{\"hashId\":\"testHash\"}";
245       const expectedAction = [
246         {
247           type: tierSupportActionTypes.TIER_SUPPORT_ACTIVATE_BUSY_FEEDBACK,
248         },
249         {
250           type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
251           data: {errorMsg: NO_RESULTS_FOUND}
252         },
253         {
254           type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK
255         }
256       ];
257
258       // When
259       await store.dispatch(querySelectedNodeElement(searchHash, undefined));
260
261       // Then
262       expect(stringifySpy).toHaveBeenCalledWith(expectedStringifyInput);
263       expect(networkCall.fetchRequestObj).toHaveBeenCalledWith(
264           expect.anything(),
265           expect.anything(),
266           expect.anything(),
267           expectedFetchRequestBody
268       );
269       expect(store.getActions()).toEqual(expectedAction);
270     });
271   });
272
273   describe('setNotificationText', () => {
274   const msgSeverity = MESSAGE_LEVEL_WARNING;
275
276     it('emits SET_GLOBAL_MESSAGE action with payload when msgText is not blank ', () => {
277       // Given
278       const msgText = 'some test text';
279       const expectedActions = [{
280         type: globalInlineMessageBarActionTypes.SET_GLOBAL_MESSAGE,
281         data: {
282           msgText: msgText,
283           msgSeverity: msgSeverity
284         }
285       }];
286
287       // When
288       store.dispatch(setNotificationText(msgText, msgSeverity));
289
290       // Then
291       expect(store.getActions()).toEqual(expectedActions);
292     });
293
294     it('emits CLEAR_GLOBAL_MESSAGE when msgText is blank ', () => {
295       // Given
296       const msgText = '';
297       const expectedActions = [{
298         type: globalInlineMessageBarActionTypes.CLEAR_GLOBAL_MESSAGE
299       }];
300
301       // When
302       store.dispatch(setNotificationText(msgText, msgSeverity));
303
304       // Then
305       expect(store.getActions()).toEqual(expectedActions);
306     });
307   });
308
309   function getPromiseWithStatusCode(statusCode) {
310     return new Promise(function(resolve) {
311       resolve({status: statusCode});
312     });
313   }
314
315   function getNodesPromise(nodes) {
316     return new Promise(function (resolve) {
317       resolve({
318         status: 200,
319         json: () => {
320           return {
321             nodes: nodes
322           };
323         }
324       });
325     });
326   }
327
328   function prepareTestNodes() {
329     const node1 = prepareSelectedClassTestNode();
330     const node2 = prepareOtherClassTestNode();
331     return {
332       nodes: [node1, node2],
333       node1,
334       node2
335     }
336   }
337
338   function prepareOtherClassTestNode() {
339     return  {
340       id: '3899453d98c5b670952765974876e55ef954e0f8a930b1c',
341       itemType: 'generic-vnf',
342       nodeMeta: {
343         className: 'someOtherClassName',
344         nodeLabel1: 'Artic',
345         nodeValidated: false,
346         nodeLocation: 'bottom'
347       },
348       rootNode: false,
349       index: 1
350     };
351   }
352
353   function prepareSelectedClassTestNode() {
354     return {
355       id: '7352312c7bfa814c3071a803d98c5b670952765974876e55ef954e0f8a930b1c',
356       itemType: 'complex',
357       nodeMeta: {
358         className: 'selectedSearchedNodeClass',
359         nodeLabel1: 'Artic',
360         nodeValidated: false,
361         nodeLocation: 'bottom'
362       },
363       rootNode: false,
364       index: 2
365     };
366   }
367 });