2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 import {expect, deep} from "chai";
22 import React from "react";
23 import {Provider} from "react-redux";
24 import sinon from "sinon";
25 import configureStore from "redux-mock-store";
26 import thunk from "redux-thunk";
27 import {storeCreator} from "app/AppStore.js";
29 autoCompleteSearchBarActionTypes,
30 TS_BACKEND_SEARCH_SELECTED_NODE_URL,
31 ERROR_INVALID_SEARCH_TERMS,
32 TIER_SUPPORT_NETWORK_ERROR
33 } from 'generic-components/autoCompleteSearchBar/AutoCompleteSearchBarConstants.js';
36 clearSuggestionsTextField,
38 onSuggestionsClearRequested,
39 getInvalidSearchInputEvent,
41 fetchSelectedNodeElement,
43 } from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarActions.js';
44 import {tierSupportActionTypes, TSUI_SEARCH_URL} from "app/tierSupport/TierSupportConstants.js";
45 import {getDynamicTSUISearchURL} from "app/tierSupport/TierSupportActions.js";
46 import * as networkCall from "app/networking/NetworkCalls.js";
49 ERROR_RETRIEVING_DATA} from "app/networking/NetworkConstants.js";
50 import autoCompleteSearchBarTestConstants from "./autoCompleteSearchBarTestConstants";
52 const middlewares = [thunk]; // add your middlewares like `redux-thunk`
53 const mockStore = configureStore(middlewares);
56 describe('Core AutoCompleteSearchBar suite', function() {
58 describe('AutoCompleteSearchBar - Actions test ', function() {
60 function createState(currentScreen, tierSupport) {
62 currentScreen: currentScreen,
63 tierSupport: tierSupport
67 it('Expect CLEAR_SUGGESTIONS_TEXT_FIELD action being passed When clearSuggestionsTextField is dispatched.', function(){
68 const store = mockStore({});
69 function clearSuggestionsTextFieldSuccess() {
71 type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD
75 store.dispatch(clearSuggestionsTextField());
76 expect(store.getActions()[0]).to.deep.equal(clearSuggestionsTextFieldSuccess());
79 it('Expect CLEAR_SUGGESTIONS action being passed When onSuggestionsClearRequested is dispatched.', function() {
80 const store = mockStore({});
81 function onSuggestionsClearRequestedSuccess() {
83 type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS
87 store.dispatch(onSuggestionsClearRequested());
88 expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedSuccess());
92 it('Expect TS_NODE_SEARCH_INVALID_TERMS action being passed When getInvalidSearchInputEvent is dispatched.', function(){
93 const store = mockStore({});
96 function onGetInvalidSearchInputEvent(){
98 type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS,
99 data: {value: value, errorMsg: ERROR_INVALID_SEARCH_TERMS}
103 store.dispatch(getInvalidSearchInputEvent(value));
104 expect(store.getActions()[0]).to.deep.equal(onGetInvalidSearchInputEvent());
110 it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChangeSuccess is dispatched with tab key.', function() {
111 const store = mockStore({});
112 const value = 'test';
114 function onSuggestionsChangeSuccess() {
116 type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED,
125 store.dispatch(onSuggestionsChange(event, value));
126 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSuccess());
130 it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChange is dispatched with enter key.', function() {
131 const store = mockStore({});
132 const value = 'test';
134 function onSuggestionsChangeSucessfull() {
135 return {type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED, data: value};
142 store.dispatch(onSuggestionsChange(event, value));
143 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSucessfull());
146 it('Expect fetchRequest being called once and SUGGESTION_FOUND action being when passed fetchRequestedValues is dispatched, and a valid response is sent in mock', done => {
147 const store = mockStore({});
148 const value = 'test';
150 let mockNetwork = sinon.mock(networkCall);
151 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1));
152 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
153 mockNetwork.verify();
154 mockNetwork.restore();
156 function onCreateSuggestionFoundEvent() {
158 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
159 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
163 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
164 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
169 it('Expect fetchRequest being called once and SUGGESTION_NOT_FOUND action being when passed fetchRequestedValues is dispatched, and a valid response with no hits is sent in mock', done => {
170 const store = mockStore({});
171 const value = 'test';
173 let mockNetwork = sinon.mock(networkCall);
174 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithOutHits));
175 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
176 mockNetwork.verify();
177 mockNetwork.restore();
178 function onCreateSuggestionNotFoundEvent() {
180 type: autoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND
184 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionNotFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
185 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
188 it('Expect fetchRequest being called once and TIER_SUPPORT_NETWORK_ERROR action being when passed fetchRequestedValues is dispatched, and network error is sent in mock', done => {
189 const store = mockStore({});
190 const value = 'test';
192 let mockNetwork = sinon.mock(networkCall);
193 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.networkError));
194 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
195 mockNetwork.verify();
196 mockNetwork.restore();
198 function onGetInvalidQueryEvent() {
200 type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
201 data: {value: value, errorMsg: ERROR_RETRIEVING_DATA}
206 expect(store.getActions()[0].type.toString()).to.equal(tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR.toString()), autoCompleteSearchBarTestConstants.mockRequestTimeOut
208 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
211 it('Expect fetchRequest being called once and SUGGESTION_FOUND action being when passed queryRequestedValues is dispatched, and network error is sent in mock', done => {
212 const store = mockStore({});
213 const value = 'test';
215 let mockNetwork = sinon.mock(networkCall);
216 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1));
217 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
218 mockNetwork.verify();
219 mockNetwork.restore();
221 function onCreateSuggestionFoundEvent() {
223 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
224 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
228 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
229 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
232 it('Expect TIER_SUPPORT_NETWORK_ERROR action being passed when clearSuggestionsTextField is dispatched with no mock, and network error is sent in mock', done => {
233 const store = mockStore({});
234 const value = 'test';
236 store.dispatch(clearSuggestionsTextField());
238 function onClearSuggestionsTextField() {
239 return {type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD};
242 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onClearSuggestionsTextField()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
243 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
246 it('Expect CLEAR_SUGGESTIONS action being passed when onSuggestionsClearRequested is dispatched with no mock, and network error is sent in mock', done => {
247 const store = mockStore({});
248 const value = 'test';
250 store.dispatch(onSuggestionsClearRequested());
252 function onSuggestionsClearRequestedExpected() {
253 return{type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS};
256 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedExpected()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
257 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);