summaryrefslogtreecommitdiffstats
path: root/test/app/tierSupport/TierSupportActions.test.js
blob: 30d3fe9e19d2a4a699112242f781bbe6ac6ebcb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk'
import {
  onNodeDetailsChange,
  splitPaneResize,
  onNodeMenuChange,
  clearVIData,
  setNotificationText,
  fetchSelectedNodeElement,
  querySelectedNodeElement
}
from 'app/tierSupport/TierSupportActions';
import {tierSupportActionTypes} from 'app/tierSupport/TierSupportConstants';
import {MESSAGE_LEVEL_WARNING} from 'utils/GlobalConstants';
import {globalInlineMessageBarActionTypes} from 'app/globalInlineMessageBar/GlobalInlineMessageBarConstants';
import {
  NO_RESULTS_FOUND,
  ERROR_RETRIEVING_DATA
} from 'app/networking/NetworkConstants';
import networkCall from 'app/networking/NetworkCalls';

const mockStore = configureStore([thunk]);

describe('TierSupportActionTests', () => {
  let store;

  beforeEach(() => {
    store = mockStore({ tierSupportReducer: {} });
  });

  describe('onNodeDetailsChange', () => {
    it('emits TS_GRAPH_NODE_SELECTED with payload', () => {
      // Given
      const newDetails = {
        testDetails: 'Test Details',
      };
      const expectedActions = [{
        type: tierSupportActionTypes.TS_GRAPH_NODE_SELECTED,
        data: newDetails
      }];

      // When
      store.dispatch(onNodeDetailsChange(newDetails));

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });
  });

  describe('splitPaneResize', () => {
    it('emits SPLIT_PANE_RESIZE action with payload', () => {
      // Given
      const initialLoad = {
        test: 'message'
      };
      const expectedActions = [{
        type: tierSupportActionTypes.SPLIT_PANE_RESIZE,
        data: initialLoad
      }];

      // When
      store.dispatch(splitPaneResize(initialLoad));

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });
  });

  describe('onNodeMenuChange', () => {
    it('emits TS_GRAPH_NODE_MENU_SELECTED action with payload', () => {
      // Given
      const selectedMenu = {
        test: 'menuData'
      };
      const expectedActions = [{
        type: tierSupportActionTypes.TS_GRAPH_NODE_MENU_SELECTED,
        data: selectedMenu
      }];

      // When
      store.dispatch(onNodeMenuChange(selectedMenu));

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });
  });

  describe('clearVIData', () => {
    it('emits TIER_SUPPORT_CLEAR_DATA action', () => {
      // Given
      const expectedActions = [{
        type: tierSupportActionTypes.TIER_SUPPORT_CLEAR_DATA
      }];

      // When
      store.dispatch(clearVIData());

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });
  });

  describe('fetchSelectedNodeElement', () => {
    it('emits actions with proper error message when 204 code returned', async () => {
      // Given
      const promise = () => getPromiseWithStatusCode(204);
      const expectedActions = [
        {
          type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK,
        },
        {
          type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
          data: {errorMsg: NO_RESULTS_FOUND}
        }
      ];

      // When
      await store.dispatch(fetchSelectedNodeElement(promise));

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });

    it('emits actions with proper error message when 3XX code returned', async () => {
      // Given
      const promise = () => getPromiseWithStatusCode(301);
      const expectedActions = [
        {
          type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK,
        },
        {
          type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
          data: {errorMsg: NO_RESULTS_FOUND}
        }
      ];

      // When
      await store.dispatch(fetchSelectedNodeElement(promise));

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });

    it('emits actions with proper error message when 5XX code returned', async () => {
      // Given
      const promise = () => getPromiseWithStatusCode(501);
      const expectedActions = [
        {
          type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK,
        },
        {
          type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
          data: {value: ERROR_RETRIEVING_DATA, errorMsg: ERROR_RETRIEVING_DATA}
        }
      ];

      // When
      await store.dispatch(fetchSelectedNodeElement(promise));

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });

    it('emits actions with payload when 200 code returned and nodes not empty', async () => {
      // Given
      const {nodes, node1} = prepareTestNodes();
      const promise = () => getNodesPromise(nodes);
      const expectedActions = [
        {
          type: tierSupportActionTypes.TS_NODE_SEARCH_RESULTS,
          data: {"nodes": nodes}
        },
        {
          type: tierSupportActionTypes.TS_GRAPH_NODE_SELECTED,
          data: node1
        },
        {
          type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK
        }
      ];

      // When
      await store.dispatch(fetchSelectedNodeElement(promise));

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });

    it('emits actions with payload when 200 code returned and nodes empty', async () => {
      // Given
      const promise = () => getNodesPromise([]);
      const expectedActions = [
        {
          type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
          data: {errorMsg: NO_RESULTS_FOUND}
        },
        {
          type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK
        }
      ];

      // When
      await store.dispatch(fetchSelectedNodeElement(promise));

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });
  });

  describe('querySelectedNodeElement', () => {
    const searchHash = "testHash";

    it('emits actions when fetchRequest defined ', async () => {
      // Given
      const promise = () => getNodesPromise([]);
      const expectedAction = [
        {
          type: tierSupportActionTypes.TIER_SUPPORT_ACTIVATE_BUSY_FEEDBACK,
        },
        {
          type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
          data: {errorMsg: NO_RESULTS_FOUND}
        },
        {
          type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK
        }
      ];

      // When
      await store.dispatch(querySelectedNodeElement(searchHash, promise));

      // Then
      expect(store.getActions()).toEqual(expectedAction);
    });

    it('builds request and emits actions when fetchRequest undefined ', async () => {
      // Given
      const stringifySpy = jest.spyOn(JSON, 'stringify');
      const promise = getNodesPromise([]);
      networkCall.fetchRequestObj = jest.fn(() => promise);
      const expectedStringifyInput = {
        hashId: searchHash
      };
      const expectedFetchRequestBody = "{\"hashId\":\"testHash\"}";
      const expectedAction = [
        {
          type: tierSupportActionTypes.TIER_SUPPORT_ACTIVATE_BUSY_FEEDBACK,
        },
        {
          type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
          data: {errorMsg: NO_RESULTS_FOUND}
        },
        {
          type: tierSupportActionTypes.TIER_SUPPORT_DISABLE_BUSY_FEEDBACK
        }
      ];

      // When
      await store.dispatch(querySelectedNodeElement(searchHash, undefined));

      // Then
      expect(stringifySpy).toHaveBeenCalledWith(expectedStringifyInput);
      expect(networkCall.fetchRequestObj).toHaveBeenCalledWith(
          expect.anything(),
          expect.anything(),
          expect.anything(),
          expectedFetchRequestBody
      );
      expect(store.getActions()).toEqual(expectedAction);
    });
  });

  describe('setNotificationText', () => {
  const msgSeverity = MESSAGE_LEVEL_WARNING;

    it('emits SET_GLOBAL_MESSAGE action with payload when msgText is not blank ', () => {
      // Given
      const msgText = 'some test text';
      const expectedActions = [{
        type: globalInlineMessageBarActionTypes.SET_GLOBAL_MESSAGE,
        data: {
          msgText: msgText,
          msgSeverity: msgSeverity
        }
      }];

      // When
      store.dispatch(setNotificationText(msgText, msgSeverity));

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });

    it('emits CLEAR_GLOBAL_MESSAGE when msgText is blank ', () => {
      // Given
      const msgText = '';
      const expectedActions = [{
        type: globalInlineMessageBarActionTypes.CLEAR_GLOBAL_MESSAGE
      }];

      // When
      store.dispatch(setNotificationText(msgText, msgSeverity));

      // Then
      expect(store.getActions()).toEqual(expectedActions);
    });
  });

  function getPromiseWithStatusCode(statusCode) {
    return new Promise(function(resolve) {
      resolve({status: statusCode});
    });
  }

  function getNodesPromise(nodes) {
    return new Promise(function (resolve) {
      resolve({
        status: 200,
        json: () => {
          return {
            nodes: nodes
          };
        }
      });
    });
  }

  function prepareTestNodes() {
    const node1 = prepareSelectedClassTestNode();
    const node2 = prepareOtherClassTestNode();
    return {
      nodes: [node1, node2],
      node1,
      node2
    }
  }

  function prepareOtherClassTestNode() {
    return  {
      id: '3899453d98c5b670952765974876e55ef954e0f8a930b1c',
      itemType: 'generic-vnf',
      nodeMeta: {
        className: 'someOtherClassName',
        nodeLabel1: 'Artic',
        nodeValidated: false,
        nodeLocation: 'bottom'
      },
      rootNode: false,
      index: 1
    };
  }

  function prepareSelectedClassTestNode() {
    return {
      id: '7352312c7bfa814c3071a803d98c5b670952765974876e55ef954e0f8a930b1c',
      itemType: 'complex',
      nodeMeta: {
        className: 'selectedSearchedNodeClass',
        nodeLabel1: 'Artic',
        nodeValidated: false,
        nodeLocation: 'bottom'
      },
      rootNode: false,
      index: 2
    };
  }
});