Merge "Add tests to Inventory module"
[aai/sparky-fe.git] / test / MainScreenWrapperReducer.test.js
1 import thunk from 'redux-thunk';
2 import randstr from 'randomstring';
3 import configureMockStore from 'redux-mock-store';
4
5 import MainScreenWrapperReducer from 'app/MainScreenWrapperReducer'
6 import {aaiActionTypes} from 'app/MainScreenWrapperConstants';
7 import {getPersonalizationDetails} from 'app/personlaization/PersonalizationActions';
8 import {getInvalidSearchInputEvent} from'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarActions';
9 import {extensibleViewNetworkCallback, showMainMenu, clearExtensibleViewData, setSecondaryTitle} from 'app/MainScreenWrapperActionHelper';
10 import {externalUrlRequest, getSubscriptionPayload} from 'app/contextHandler/ContextHandlerActions'
11
12 import {globalInlineMessageBarActionTypes} from "app/globalInlineMessageBar/GlobalInlineMessageBarConstants";
13 import {globalAutoCompleteSearchBarActionTypes} from "app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarConstants";
14 import {
15     contextHandlerActionTypes,
16     FAILED_REQUEST,
17     MULTIPLE_RESULT, SUBSCRIPTION_FAILED_MESSAGE,
18     WRONG_EXTERNAL_REQUEST_MESSAGE,
19     WRONG_RESULT,
20     ZERO_RESULT
21 } from "app/contextHandler/ContextHandlerConstants";
22 import {PERSONALIZATION_FAILED_MESSAGE, personalizationActionTypes} from "app/personlaization/PersonalizationConstans";
23
24 describe('MainScreenWrapperReducerTests', () => {
25     fetch = require('jest-fetch-mock');
26     const mockStore = configureMockStore([thunk])();
27     const initialState = {Baz : 'Fooo'};
28     const error = '401';
29
30     beforeEach(() => {
31         fetch.resetMocks();
32         mockStore.clearActions();
33     });
34
35     describe('extensibleViewNetworkCallbackTests', () => {
36         const paramName = 'boo';
37         const postBody = 'baz';
38         const curView = {Boz : 'Fooz'};
39         const requestUrl = 'www.foo.com';
40         const response = {Foo: 'Bar'};
41         
42         describe('success tests', () => {
43             beforeEach(async () => {
44                 //given
45                 fetch.once(JSON.stringify(response));
46                 await mockStore.dispatch(extensibleViewNetworkCallback(requestUrl, postBody, paramName, curView));
47             });
48
49             it('action on success test', () => {
50                 //when
51                 const [action, ...rest] = mockStore.getActions();
52
53                 //then
54                 expect(rest).toEqual([]);
55                 expect(action.type).toBe(aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_RESPONSE_RECEIVED);
56             });
57
58             it('reducer on success test', () => {
59                 //given
60                 const [action, ..._] = mockStore.getActions();
61
62                 //when
63                 const {
64                     extensibleViewNetworkCallbackData,
65                     ...rest
66                 } = MainScreenWrapperReducer(initialState, action);
67
68                 //then
69                 expect(rest).toEqual(initialState);
70                 expect(extensibleViewNetworkCallbackData).toEqual({
71                     boo: response,
72                     ...curView
73                 });
74             });
75         });
76
77         describe('failure tests', () => {
78             beforeEach(async () => {
79                 //given
80                 fetch.mockRejectOnce(error);
81                 await mockStore.dispatch(extensibleViewNetworkCallback(requestUrl, postBody, paramName, curView));
82             });
83
84             it('action on failure test', () => {
85                 //given
86                 const [firstAction, secondAction, ...tail] = mockStore.getActions();
87
88                 //then
89                 expect(tail).toEqual([]);
90                 expect(firstAction.type).toEqual(globalInlineMessageBarActionTypes.SET_GLOBAL_MESSAGE);
91                 expect(secondAction.type).toEqual(aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_RESPONSE_RECEIVED);
92             });
93
94             it('reducer on failure test', () => {
95                 //given
96                 const [firstAction, secondAction, ..._] = mockStore.getActions();
97
98                 //when
99                 const afterFirstState = MainScreenWrapperReducer(initialState, firstAction);
100                 const {
101                     extensibleViewNetworkCallbackData,
102                     ...rest
103                 } = MainScreenWrapperReducer(initialState, secondAction);
104
105                 //then
106                 expect(afterFirstState).toEqual(initialState);
107                 expect(rest).toEqual(initialState);
108                 expect(extensibleViewNetworkCallbackData).toEqual({
109                     boo: {},
110                     ...curView
111                 });
112             });
113         });
114     });
115
116     describe.each([true, false])('showMainMenuTests', value => {
117         beforeEach(async () => {
118             //given
119             await mockStore.dispatch(showMainMenu(value));
120         });
121
122         it('action on show: ' + value + ' test', () => {
123             //when
124             const [action, ...rest] = mockStore.getActions();
125
126             //then
127             expect(rest).toEqual([]);
128             expect(action.type).toBe(aaiActionTypes.AAI_SHOW_MENU);
129         });
130
131         it('reducer on show: ' + value + ' test', () => {
132             //given
133             const [action, ..._] = mockStore.getActions();
134
135             //when
136             const {
137                 showMenu,
138                 toggleButtonActive,
139                 ...rest
140             } = MainScreenWrapperReducer(initialState, action);
141
142             //then
143             expect(rest).toEqual(initialState);
144             expect(showMenu).toBe(value);
145             expect(toggleButtonActive).toBe(value);
146         });
147     });
148
149     describe('clearExtensibleViewDataTests', () => {
150         beforeEach(async () => {
151             //given
152             await mockStore.dispatch(clearExtensibleViewData());
153         });
154
155         it('action test', () => {
156             //when
157             const [action, ...rest] = mockStore.getActions();
158
159             //then
160             expect(rest).toEqual([]);
161             expect(action.type).toBe(aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_CLEAR_DATA);
162         });
163
164         it('reducer test', () => {
165             //given
166             const [action, ..._] = mockStore.getActions();
167
168             //when
169             const {
170                 extensibleViewNetworkCallbackData,
171                 ...rest
172             } = MainScreenWrapperReducer(initialState, action);
173
174             expect(rest).toEqual(initialState);
175             expect(extensibleViewNetworkCallbackData).toEqual({});
176         });
177     });
178
179     describe('getInvalidSearchInputEventTests', () => {
180         const msg = randstr.generate();
181
182         beforeEach(async () => {
183             await mockStore.dispatch(getInvalidSearchInputEvent(msg));
184         });
185
186         it('action msg: ' + msg + ' test', () => {
187             //when
188             const [action, ...rest] = mockStore.getActions();
189
190             //then
191             expect(rest).toEqual([]);
192             expect(action.type).toBe(globalAutoCompleteSearchBarActionTypes.SEARCH_WARNING_EVENT);
193         });
194
195         it('reducer msg: ' + msg + ' test', () => {
196             //given
197             const [action, ..._] = mockStore.getActions();
198
199             //when
200             const {
201                 extensibleViewNetworkCallbackData,
202                 ...rest
203             } = MainScreenWrapperReducer(initialState, action);
204
205             //then
206             expect(rest).toEqual(initialState);
207             expect(extensibleViewNetworkCallbackData).toEqual({clearView : true});
208         });
209     });
210
211     describe('externalUrlRequestTests', () => {
212         const someUrlParams = 'view=A&entityId=B&entityType=C';
213
214         describe.each([{
215             title: 'on empty url params',
216             prepareMock: () => {},
217             urlParams: '',
218             expectedResponse: WRONG_EXTERNAL_REQUEST_MESSAGE
219         }, {
220             title: 'on request rejected by the server',
221             prepareMock: () => fetch.mockRejectOnce('401'),
222             urlParams: someUrlParams,
223             expectedResponse: FAILED_REQUEST
224         }, {
225             title: 'on empty suggestions',
226             prepareMock: () => fetch.once(JSON.stringify({})),
227             urlParams: someUrlParams,
228             expectedResponse: WRONG_RESULT
229         }, {
230             title: 'on no results',
231             prepareMock: () => fetch.once(JSON.stringify({totalFound: 0, suggestions: []})),
232             urlParams: someUrlParams,
233             expectedResponse: ZERO_RESULT
234         }, {
235             title: 'on multiple results',
236             prepareMock: () => fetch.once(JSON.stringify({totalFound: 2, suggestions: ['Foo', 'Bar']})),
237             urlParams: someUrlParams,
238             expectedResponse: MULTIPLE_RESULT
239         }])('failure tests', ({title, prepareMock, urlParams, expectedResponse}) => {
240             beforeEach(async () => {
241                 //given
242                 prepareMock();
243                 await mockStore.dispatch(externalUrlRequest(urlParams));
244             });
245
246             it('action ' + title + ' test', () => {
247                 //when
248                 const [action, ...rest] = mockStore.getActions();
249
250                 //then
251                 expect(rest).toEqual([]);
252                 expect(action.type).toBe(globalInlineMessageBarActionTypes.SET_GLOBAL_MESSAGE);
253                 expect(action.data.msgText).toEqual(expectedResponse);
254             });
255
256             it('reducer ' + title + ' test', () => {
257                 //given
258                 const [action, ..._] = mockStore.getActions();
259
260                 //when
261                 const state = MainScreenWrapperReducer(initialState, action);
262
263                 //then
264                 expect(state).toEqual(initialState);
265             });
266         });
267
268         describe("success tests", () => {
269             //given
270             const response = {totalFound: 1, suggestions: ['Foo']};
271
272             beforeEach(async () => {
273                 //given
274                 fetch.resetMocks();
275                 fetch.once(JSON.stringify(response));
276
277                 await mockStore.dispatch(externalUrlRequest(someUrlParams));
278             });
279
280             it('action on exactly one suggestion test', () => {
281                 //when
282                 const [firstAction, secondAction, ...rest] = mockStore.getActions();
283
284                 //then
285                 expect(rest).toEqual([]);
286                 expect(firstAction.type).toBe( globalInlineMessageBarActionTypes.CLEAR_GLOBAL_MESSAGE);
287                 expect(secondAction.type).toBe(contextHandlerActionTypes.SINGLE_SUGGESTION_FOUND);
288             });
289
290             it('reducer on exactly one suggestion test', () => {
291                 //given
292                 const [firstAction, secondAction, ..._] = mockStore.getActions();
293
294                 //when
295                 const state = MainScreenWrapperReducer(initialState, firstAction);
296                 const {
297                     externalRequestFound,
298                     ...rest
299                 } = MainScreenWrapperReducer(state, secondAction);
300
301                 //then
302                 expect(state).toEqual(initialState);
303                 expect(rest).toEqual(initialState);
304                 expect(externalRequestFound).toEqual({suggestion: response.suggestions[0]});
305             });
306         });
307     });
308
309     describe('setSecondaryTitleTests', () => {
310         //given
311         const title = randstr.generate();
312
313         beforeEach(async () => {
314             //given
315             await mockStore.dispatch(setSecondaryTitle(title));
316         });
317
318         it('action test', () => {
319             //when
320             const [action, ...rest] = mockStore.getActions();
321
322             //then
323             expect(rest).toEqual([]);
324             expect(action.type).toBe(aaiActionTypes.SET_SECONDARY_TITLE);
325         });
326
327         it('reducer test', () => {
328             //given
329             const [action, ..._] = mockStore.getActions();
330
331             //when
332             const {
333                 secondaryTitle,
334                 ...rest
335             } = MainScreenWrapperReducer(initialState, action);
336
337              //then
338             expect(rest).toEqual(initialState);
339             expect(secondaryTitle).toEqual(title);
340         });
341     });
342
343     describe('getSubscriptionPayloadTests', () => {
344         describe('failure tests', () => {
345             beforeEach(async () => {
346                 //given
347                 fetch.mockRejectOnce(error);
348                 await mockStore.dispatch(getSubscriptionPayload());
349             });
350
351             it('action on request rejected by server test', async () => {
352                 //when
353                 const [action, ...rest] = mockStore.getActions();
354
355                 //then
356                 expect(rest).toEqual([]);
357                 expect(action.type).toBe(globalInlineMessageBarActionTypes.SET_GLOBAL_MESSAGE);
358                 expect(action.data.msgText).toEqual(SUBSCRIPTION_FAILED_MESSAGE);
359             });
360
361             it('reducer on request rejected by server test', async () => {
362                 //given
363                 const [action, ..._] = mockStore.getActions();
364
365                 //when
366                 const state = MainScreenWrapperReducer(initialState, action);
367
368                 //then
369                 expect(state).toEqual(initialState);
370             });
371         });
372
373         const subscriptionData = 'Foo';
374
375         describe.each([{
376             title: 'on disabled subscription',
377             payload: {subscriptionEnabled: false},
378             type: contextHandlerActionTypes.SUBSCRIPTION_PAYLOAD_EMPTY,
379             detail : undefined
380         }, {
381             title: 'on enabled subscription',
382             payload: {subscriptionEnabled: true, subscriptionDetails: subscriptionData},
383             type: contextHandlerActionTypes.SUBSCRIPTION_PAYLOAD_FOUND,
384             detail : subscriptionData
385         }])('success tests', ({title, payload, type, detail}) => {
386             beforeEach(async () => {
387                 //given
388                 fetch.once(JSON.stringify(payload));
389                 await mockStore.dispatch(getSubscriptionPayload());
390             });
391
392             it('action ' + title + ' test', () => {
393                 //when
394                 const [action, ...rest] = mockStore.getActions();
395
396                 //then
397                 expect(rest).toEqual([]);
398                 expect(action.type).toBe(type);
399             });
400
401             it('reducer '  + title + '  test', () => {
402                 //given
403                 const [action, ..._] = mockStore.getActions();
404
405                 //when
406                 const {
407                     subscriptionEnabled,
408                     subscriptionPayload,
409                     ...rest
410                 } = MainScreenWrapperReducer(initialState, action);
411
412                 //then
413                 expect(rest).toEqual(initialState);
414                 expect(subscriptionEnabled).toBe(payload.subscriptionEnabled);
415                 expect(subscriptionPayload).toEqual(detail);
416             });
417         });
418     });
419
420     describe("getPersonalizationDetailsTests", () => {
421         describe('failure tests', () => {
422             beforeEach(async () => {
423                 //given
424                 fetch.mockRejectOnce(error);
425                 await mockStore.dispatch(getPersonalizationDetails());
426             });
427
428             it('action on request rejected by server test', () => {
429                 //when
430                 const [action, ...rest] = mockStore.getActions();
431
432                 //then
433                 expect(rest).toEqual([]);
434                 expect(action.type).toBe(globalInlineMessageBarActionTypes.SET_GLOBAL_MESSAGE);
435                 expect(action.data.msgText).toEqual(PERSONALIZATION_FAILED_MESSAGE);
436             });
437
438             it('reducer on request rejected by server test', () => {
439                 //given
440                 const [action, ..._] = mockStore.getActions();
441
442                 //when
443                 const state = MainScreenWrapperReducer(initialState, action);
444
445                 //then
446                 expect(state).toEqual(initialState);
447             });
448         });
449
450         describe('success tests', () => {
451             const personalizationPayload = {topLeftHeader: 'Foo', htmlDocumentTitle: 'Bar'};
452
453             beforeEach(async () => {
454                 //given
455                 fetch.once(JSON.stringify(personalizationPayload));
456                 await mockStore.dispatch(getPersonalizationDetails());
457             });
458
459             it('action on personalization payload found test', () => {
460                 //when
461                 const [action, ...rest] = mockStore.getActions();
462
463                 //then
464                 expect(rest).toEqual([]);
465                 expect(action.type).toBe(personalizationActionTypes.PERSONALIZATION_PAYLOAD_FOUND);
466             });
467
468             it("Reducer on personalization payload found test", async () => {
469                 //given
470                 const [action, ..._] = mockStore.getActions();
471
472                 //when
473                 const {
474                     aaiTopLeftPersonalizedHeader,
475                     aaiPersonalizedHtmlDocumentTitle,
476                     ...rest
477                 } = MainScreenWrapperReducer(initialState, action);
478
479                 //then
480                 expect(rest).toEqual(initialState);
481                 expect(aaiTopLeftPersonalizedHeader).toEqual(personalizationPayload.topLeftHeader);
482                 expect(aaiPersonalizedHtmlDocumentTitle).toEqual(personalizationPayload.htmlDocumentTitle);
483             });
484         });
485     });
486 });