2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 import {expect, deep} from "chai";
24 import React from "react";
25 import {Provider} from "react-redux";
26 import sinon from "sinon";
27 import configureStore from "redux-mock-store";
28 import thunk from "redux-thunk";
29 import {storeCreator} from "app/AppStore.js";
31 autoCompleteSearchBarActionTypes,
32 TS_BACKEND_SEARCH_SELECTED_NODE_URL,
33 ERROR_INVALID_SEARCH_TERMS,
34 TIER_SUPPORT_NETWORK_ERROR
35 } from 'generic-components/autoCompleteSearchBar/AutoCompleteSearchBarConstants.js';
38 clearSuggestionsTextField,
40 onSuggestionsClearRequested,
41 getInvalidSearchInputEvent,
43 fetchSelectedNodeElement,
45 } from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarActions.js';
46 import {tierSupportActionTypes, TSUI_SEARCH_URL} from "app/tierSupport/TierSupportConstants.js";
47 import {getDynamicTSUISearchURL} from "app/tierSupport/TierSupportActions.js";
48 import * as networkCall from "app/networking/NetworkCalls.js";
51 ERROR_RETRIEVING_DATA} from "app/networking/NetworkConstants.js";
52 import autoCompleteSearchBarTestConstants from "./autoCompleteSearchBarTestConstants";
54 const middlewares = [thunk]; // add your middlewares like `redux-thunk`
55 const mockStore = configureStore(middlewares);
58 describe('Core AutoCompleteSearchBar suite', function() {
60 describe('AutoCompleteSearchBar - Actions test ', function() {
62 function createState(currentScreen, tierSupport) {
64 currentScreen: currentScreen,
65 tierSupport: tierSupport
69 it('Expect CLEAR_SUGGESTIONS_TEXT_FIELD action being passed When clearSuggestionsTextField is dispatched.', function(){
70 const store = mockStore({});
71 function clearSuggestionsTextFieldSuccess() {
73 type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD
77 store.dispatch(clearSuggestionsTextField());
78 expect(store.getActions()[0]).to.deep.equal(clearSuggestionsTextFieldSuccess());
81 it('Expect CLEAR_SUGGESTIONS action being passed When onSuggestionsClearRequested is dispatched.', function() {
82 const store = mockStore({});
83 function onSuggestionsClearRequestedSuccess() {
85 type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS
89 store.dispatch(onSuggestionsClearRequested());
90 expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedSuccess());
94 it('Expect TS_NODE_SEARCH_INVALID_TERMS action being passed When getInvalidSearchInputEvent is dispatched.', function(){
95 const store = mockStore({});
98 function onGetInvalidSearchInputEvent(){
100 type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS,
101 data: {value: value, errorMsg: ERROR_INVALID_SEARCH_TERMS}
105 store.dispatch(getInvalidSearchInputEvent(value));
106 expect(store.getActions()[0]).to.deep.equal(onGetInvalidSearchInputEvent());
112 it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChangeSuccess is dispatched with tab key.', function() {
113 const store = mockStore({});
114 const value = 'test';
116 function onSuggestionsChangeSuccess() {
118 type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED,
127 store.dispatch(onSuggestionsChange(event, value));
128 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSuccess());
132 it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChange is dispatched with enter key.', function() {
133 const store = mockStore({});
134 const value = 'test';
136 function onSuggestionsChangeSucessfull() {
137 return {type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED, data: value};
144 store.dispatch(onSuggestionsChange(event, value));
145 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSucessfull());
148 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 => {
149 const store = mockStore({});
150 const value = 'test';
152 let mockNetwork = sinon.mock(networkCall);
153 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1));
154 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
155 mockNetwork.verify();
156 mockNetwork.restore();
158 function onCreateSuggestionFoundEvent() {
160 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
161 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
165 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
166 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
171 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 => {
172 const store = mockStore({});
173 const value = 'test';
175 let mockNetwork = sinon.mock(networkCall);
176 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithOutHits));
177 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
178 mockNetwork.verify();
179 mockNetwork.restore();
180 function onCreateSuggestionNotFoundEvent() {
182 type: autoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND
186 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionNotFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
187 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
190 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 => {
191 const store = mockStore({});
192 const value = 'test';
194 let mockNetwork = sinon.mock(networkCall);
195 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.networkError));
196 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
197 mockNetwork.verify();
198 mockNetwork.restore();
200 function onGetInvalidQueryEvent() {
202 type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
203 data: {value: value, errorMsg: ERROR_RETRIEVING_DATA}
208 expect(store.getActions()[0].type.toString()).to.equal(tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR.toString()), autoCompleteSearchBarTestConstants.mockRequestTimeOut
210 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
213 it('Expect fetchRequest being called once and SUGGESTION_FOUND action being when passed queryRequestedValues is dispatched, and network error is sent in mock', done => {
214 const store = mockStore({});
215 const value = 'test';
217 let mockNetwork = sinon.mock(networkCall);
218 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1));
219 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
220 mockNetwork.verify();
221 mockNetwork.restore();
223 function onCreateSuggestionFoundEvent() {
225 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
226 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
230 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
231 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
234 it('Expect TIER_SUPPORT_NETWORK_ERROR action being passed when clearSuggestionsTextField is dispatched with no mock, and network error is sent in mock', done => {
235 const store = mockStore({});
236 const value = 'test';
238 store.dispatch(clearSuggestionsTextField());
240 function onClearSuggestionsTextField() {
241 return {type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD};
244 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onClearSuggestionsTextField()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
245 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
248 it('Expect CLEAR_SUGGESTIONS action being passed when onSuggestionsClearRequested is dispatched with no mock, and network error is sent in mock', done => {
249 const store = mockStore({});
250 const value = 'test';
252 store.dispatch(onSuggestionsClearRequested());
254 function onSuggestionsClearRequestedExpected() {
255 return{type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS};
258 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedExpected()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
259 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);