1 import thunk from 'redux-thunk';
2 import randstr from 'randomstring';
3 import configureMockStore from 'redux-mock-store';
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'
12 import {globalInlineMessageBarActionTypes} from "app/globalInlineMessageBar/GlobalInlineMessageBarConstants";
13 import {globalAutoCompleteSearchBarActionTypes} from "app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarConstants";
15 contextHandlerActionTypes,
17 MULTIPLE_RESULT, SUBSCRIPTION_FAILED_MESSAGE,
18 WRONG_EXTERNAL_REQUEST_MESSAGE,
21 } from "app/contextHandler/ContextHandlerConstants";
22 import {PERSONALIZATION_FAILED_MESSAGE, personalizationActionTypes} from "app/personlaization/PersonalizationConstans";
24 describe('MainScreenWrapperReducerTests', () => {
25 fetch = require('jest-fetch-mock');
26 const mockStore = configureMockStore([thunk])();
27 const initialState = {Baz : 'Fooo'};
32 mockStore.clearActions();
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'};
42 describe('success tests', () => {
43 beforeEach(async () => {
45 fetch.once(JSON.stringify(response));
46 await mockStore.dispatch(extensibleViewNetworkCallback(requestUrl, postBody, paramName, curView));
49 it('action on success test', () => {
51 const [action, ...rest] = mockStore.getActions();
54 expect(rest).toEqual([]);
55 expect(action.type).toBe(aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_RESPONSE_RECEIVED);
58 it('reducer on success test', () => {
60 const [action, ..._] = mockStore.getActions();
64 extensibleViewNetworkCallbackData,
66 } = MainScreenWrapperReducer(initialState, action);
69 expect(rest).toEqual(initialState);
70 expect(extensibleViewNetworkCallbackData).toEqual({
77 describe('failure tests', () => {
78 beforeEach(async () => {
80 fetch.mockRejectOnce(error);
81 await mockStore.dispatch(extensibleViewNetworkCallback(requestUrl, postBody, paramName, curView));
84 it('action on failure test', () => {
86 const [firstAction, secondAction, ...tail] = mockStore.getActions();
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);
94 it('reducer on failure test', () => {
96 const [firstAction, secondAction, ..._] = mockStore.getActions();
99 const afterFirstState = MainScreenWrapperReducer(initialState, firstAction);
101 extensibleViewNetworkCallbackData,
103 } = MainScreenWrapperReducer(initialState, secondAction);
106 expect(afterFirstState).toEqual(initialState);
107 expect(rest).toEqual(initialState);
108 expect(extensibleViewNetworkCallbackData).toEqual({
116 describe.each([true, false])('showMainMenuTests', value => {
117 beforeEach(async () => {
119 await mockStore.dispatch(showMainMenu(value));
122 it('action on show: ' + value + ' test', () => {
124 const [action, ...rest] = mockStore.getActions();
127 expect(rest).toEqual([]);
128 expect(action.type).toBe(aaiActionTypes.AAI_SHOW_MENU);
131 it('reducer on show: ' + value + ' test', () => {
133 const [action, ..._] = mockStore.getActions();
140 } = MainScreenWrapperReducer(initialState, action);
143 expect(rest).toEqual(initialState);
144 expect(showMenu).toBe(value);
145 expect(toggleButtonActive).toBe(value);
149 describe('clearExtensibleViewDataTests', () => {
150 beforeEach(async () => {
152 await mockStore.dispatch(clearExtensibleViewData());
155 it('action test', () => {
157 const [action, ...rest] = mockStore.getActions();
160 expect(rest).toEqual([]);
161 expect(action.type).toBe(aaiActionTypes.EXTENSIBLE_VIEW_NETWORK_CALLBACK_CLEAR_DATA);
164 it('reducer test', () => {
166 const [action, ..._] = mockStore.getActions();
170 extensibleViewNetworkCallbackData,
172 } = MainScreenWrapperReducer(initialState, action);
174 expect(rest).toEqual(initialState);
175 expect(extensibleViewNetworkCallbackData).toEqual({});
179 describe('getInvalidSearchInputEventTests', () => {
180 const msg = randstr.generate();
182 beforeEach(async () => {
183 await mockStore.dispatch(getInvalidSearchInputEvent(msg));
186 it('action msg: ' + msg + ' test', () => {
188 const [action, ...rest] = mockStore.getActions();
191 expect(rest).toEqual([]);
192 expect(action.type).toBe(globalAutoCompleteSearchBarActionTypes.SEARCH_WARNING_EVENT);
195 it('reducer msg: ' + msg + ' test', () => {
197 const [action, ..._] = mockStore.getActions();
201 extensibleViewNetworkCallbackData,
203 } = MainScreenWrapperReducer(initialState, action);
206 expect(rest).toEqual(initialState);
207 expect(extensibleViewNetworkCallbackData).toEqual({clearView : true});
211 describe('externalUrlRequestTests', () => {
212 const someUrlParams = 'view=A&entityId=B&entityType=C';
215 title: 'on empty url params',
216 prepareMock: () => {},
218 expectedResponse: WRONG_EXTERNAL_REQUEST_MESSAGE
220 title: 'on request rejected by the server',
221 prepareMock: () => fetch.mockRejectOnce('401'),
222 urlParams: someUrlParams,
223 expectedResponse: FAILED_REQUEST
225 title: 'on empty suggestions',
226 prepareMock: () => fetch.once(JSON.stringify({})),
227 urlParams: someUrlParams,
228 expectedResponse: WRONG_RESULT
230 title: 'on no results',
231 prepareMock: () => fetch.once(JSON.stringify({totalFound: 0, suggestions: []})),
232 urlParams: someUrlParams,
233 expectedResponse: ZERO_RESULT
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 () => {
243 await mockStore.dispatch(externalUrlRequest(urlParams));
246 it('action ' + title + ' test', () => {
248 const [action, ...rest] = mockStore.getActions();
251 expect(rest).toEqual([]);
252 expect(action.type).toBe(globalInlineMessageBarActionTypes.SET_GLOBAL_MESSAGE);
253 expect(action.data.msgText).toEqual(expectedResponse);
256 it('reducer ' + title + ' test', () => {
258 const [action, ..._] = mockStore.getActions();
261 const state = MainScreenWrapperReducer(initialState, action);
264 expect(state).toEqual(initialState);
268 describe("success tests", () => {
270 const response = {totalFound: 1, suggestions: ['Foo']};
272 beforeEach(async () => {
275 fetch.once(JSON.stringify(response));
277 await mockStore.dispatch(externalUrlRequest(someUrlParams));
280 it('action on exactly one suggestion test', () => {
282 const [firstAction, secondAction, ...rest] = mockStore.getActions();
285 expect(rest).toEqual([]);
286 expect(firstAction.type).toBe( globalInlineMessageBarActionTypes.CLEAR_GLOBAL_MESSAGE);
287 expect(secondAction.type).toBe(contextHandlerActionTypes.SINGLE_SUGGESTION_FOUND);
290 it('reducer on exactly one suggestion test', () => {
292 const [firstAction, secondAction, ..._] = mockStore.getActions();
295 const state = MainScreenWrapperReducer(initialState, firstAction);
297 externalRequestFound,
299 } = MainScreenWrapperReducer(state, secondAction);
302 expect(state).toEqual(initialState);
303 expect(rest).toEqual(initialState);
304 expect(externalRequestFound).toEqual({suggestion: response.suggestions[0]});
309 describe('setSecondaryTitleTests', () => {
311 const title = randstr.generate();
313 beforeEach(async () => {
315 await mockStore.dispatch(setSecondaryTitle(title));
318 it('action test', () => {
320 const [action, ...rest] = mockStore.getActions();
323 expect(rest).toEqual([]);
324 expect(action.type).toBe(aaiActionTypes.SET_SECONDARY_TITLE);
327 it('reducer test', () => {
329 const [action, ..._] = mockStore.getActions();
335 } = MainScreenWrapperReducer(initialState, action);
338 expect(rest).toEqual(initialState);
339 expect(secondaryTitle).toEqual(title);
343 describe('getSubscriptionPayloadTests', () => {
344 describe('failure tests', () => {
345 beforeEach(async () => {
347 fetch.mockRejectOnce(error);
348 await mockStore.dispatch(getSubscriptionPayload());
351 it('action on request rejected by server test', async () => {
353 const [action, ...rest] = mockStore.getActions();
356 expect(rest).toEqual([]);
357 expect(action.type).toBe(globalInlineMessageBarActionTypes.SET_GLOBAL_MESSAGE);
358 expect(action.data.msgText).toEqual(SUBSCRIPTION_FAILED_MESSAGE);
361 it('reducer on request rejected by server test', async () => {
363 const [action, ..._] = mockStore.getActions();
366 const state = MainScreenWrapperReducer(initialState, action);
369 expect(state).toEqual(initialState);
373 const subscriptionData = 'Foo';
376 title: 'on disabled subscription',
377 payload: {subscriptionEnabled: false},
378 type: contextHandlerActionTypes.SUBSCRIPTION_PAYLOAD_EMPTY,
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 () => {
388 fetch.once(JSON.stringify(payload));
389 await mockStore.dispatch(getSubscriptionPayload());
392 it('action ' + title + ' test', () => {
394 const [action, ...rest] = mockStore.getActions();
397 expect(rest).toEqual([]);
398 expect(action.type).toBe(type);
401 it('reducer ' + title + ' test', () => {
403 const [action, ..._] = mockStore.getActions();
410 } = MainScreenWrapperReducer(initialState, action);
413 expect(rest).toEqual(initialState);
414 expect(subscriptionEnabled).toBe(payload.subscriptionEnabled);
415 expect(subscriptionPayload).toEqual(detail);
420 describe("getPersonalizationDetailsTests", () => {
421 describe('failure tests', () => {
422 beforeEach(async () => {
424 fetch.mockRejectOnce(error);
425 await mockStore.dispatch(getPersonalizationDetails());
428 it('action on request rejected by server test', () => {
430 const [action, ...rest] = mockStore.getActions();
433 expect(rest).toEqual([]);
434 expect(action.type).toBe(globalInlineMessageBarActionTypes.SET_GLOBAL_MESSAGE);
435 expect(action.data.msgText).toEqual(PERSONALIZATION_FAILED_MESSAGE);
438 it('reducer on request rejected by server test', () => {
440 const [action, ..._] = mockStore.getActions();
443 const state = MainScreenWrapperReducer(initialState, action);
446 expect(state).toEqual(initialState);
450 describe('success tests', () => {
451 const personalizationPayload = {topLeftHeader: 'Foo', htmlDocumentTitle: 'Bar'};
453 beforeEach(async () => {
455 fetch.once(JSON.stringify(personalizationPayload));
456 await mockStore.dispatch(getPersonalizationDetails());
459 it('action on personalization payload found test', () => {
461 const [action, ...rest] = mockStore.getActions();
464 expect(rest).toEqual([]);
465 expect(action.type).toBe(personalizationActionTypes.PERSONALIZATION_PAYLOAD_FOUND);
468 it("Reducer on personalization payload found test", async () => {
470 const [action, ..._] = mockStore.getActions();
474 aaiTopLeftPersonalizedHeader,
475 aaiPersonalizedHtmlDocumentTitle,
477 } = MainScreenWrapperReducer(initialState, action);
480 expect(rest).toEqual(initialState);
481 expect(aaiTopLeftPersonalizedHeader).toEqual(personalizationPayload.topLeftHeader);
482 expect(aaiPersonalizedHtmlDocumentTitle).toEqual(personalizationPayload.htmlDocumentTitle);