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 "app/tierSupport/autoCompleteSearchBar/AutoCompleteSearchBarConstants.js";
38 clearSuggestionsTextField,
40 onSuggestionsClearRequested,
41 getInvalidSearchInputEvent,
43 fetchSelectedNodeElement,
45 } from "app/tierSupport/autoCompleteSearchBar/AutoCompleteSearchBarActions.js";
46 import {tierSupportActionTypes, TSUI_SEARCH_URL} from "app/tierSupport/TierSupportConstants.js";
47 import {TABLE_DATA} from "app/tierSupport/selectedNodeDetails/SelectedNodeDetailsConstants.js";
48 import {getDynamicTSUISearchURL} from "app/tierSupport/TierSupportActions.js";
49 import * as networkCall from "app/networking/NetworkCalls.js";
52 ERROR_RETRIEVING_DATA} from "app/networking/NetworkConstants.js";
53 import autoCompleteSearchBarTestConstants from "./autoCompleteSearchBarTestConstants";
55 const middlewares = [thunk]; // add your middlewares like `redux-thunk`
56 const mockStore = configureStore(middlewares);
59 describe('Core AutoCompleteSearchBar suite', function() {
61 describe('AutoCompleteSearchBar - Actions test ', function() {
63 function createState(currentScreen, tierSupport) {
65 currentScreen: currentScreen,
66 tierSupport: tierSupport
70 it('Expect CLEAR_SUGGESTIONS_TEXT_FIELD action being passed When clearSuggestionsTextField is dispatched.', function(){
71 const store = mockStore({});
72 function clearSuggestionsTextFieldSuccess() {
74 type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD
78 store.dispatch(clearSuggestionsTextField());
79 expect(store.getActions()[0]).to.deep.equal(clearSuggestionsTextFieldSuccess());
82 it('Expect CLEAR_SUGGESTIONS action being passed When onSuggestionsClearRequested is dispatched.', function() {
83 const store = mockStore({});
84 function onSuggestionsClearRequestedSuccess() {
86 type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS
90 store.dispatch(onSuggestionsClearRequested());
91 expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedSuccess());
95 it('Expect TS_NODE_SEARCH_INVALID_TERMS action being passed When getInvalidSearchInputEvent is dispatched.', function(){
96 const store = mockStore({});
99 function onGetInvalidSearchInputEvent(){
101 type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS,
102 data: {value: value, errorMsg: ERROR_INVALID_SEARCH_TERMS}
106 store.dispatch(getInvalidSearchInputEvent(value));
107 expect(store.getActions()[0]).to.deep.equal(onGetInvalidSearchInputEvent());
113 it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChangeSuccess is dispatched with tab key.', function() {
114 const store = mockStore({});
115 const value = 'test';
117 function onSuggestionsChangeSuccess() {
119 type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED,
128 store.dispatch(onSuggestionsChange(event, value));
129 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSuccess());
133 it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChange is dispatched with enter key.', function() {
134 const store = mockStore({});
135 const value = 'test';
137 function onSuggestionsChangeSucessfull() {
138 return {type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED, data: value};
145 store.dispatch(onSuggestionsChange(event, value));
146 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSucessfull());
149 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 => {
150 const store = mockStore({});
151 const value = 'test';
153 let mockNetwork = sinon.mock(networkCall);
154 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1));
155 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
156 mockNetwork.verify();
157 mockNetwork.restore();
159 function onCreateSuggestionFoundEvent() {
161 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
162 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
166 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
167 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
172 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 => {
173 const store = mockStore({});
174 const value = 'test';
176 let mockNetwork = sinon.mock(networkCall);
177 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithOutHits));
178 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
179 mockNetwork.verify();
180 mockNetwork.restore();
181 function onCreateSuggestionNotFoundEvent() {
183 type: autoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND
187 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionNotFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
188 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
191 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 => {
192 const store = mockStore({});
193 const value = 'test';
195 let mockNetwork = sinon.mock(networkCall);
196 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.networkError));
197 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
198 mockNetwork.verify();
199 mockNetwork.restore();
201 function onGetInvalidQueryEvent() {
203 type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
204 data: {value: value, errorMsg: ERROR_RETRIEVING_DATA}
209 expect(store.getActions()[0].type.toString()).to.equal(tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR.toString()), autoCompleteSearchBarTestConstants.mockRequestTimeOut
211 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
214 it('Expect fetchRequest being called once and SUGGESTION_FOUND action being when passed queryRequestedValues is dispatched, and network error is sent in mock', done => {
215 const store = mockStore({});
216 const value = 'test';
218 let mockNetwork = sinon.mock(networkCall);
219 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1));
220 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
221 mockNetwork.verify();
222 mockNetwork.restore();
224 function onCreateSuggestionFoundEvent() {
226 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
227 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
231 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
232 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
235 it('Expect TIER_SUPPORT_NETWORK_ERROR action being passed when clearSuggestionsTextField is dispatched with no mock, and network error is sent in mock', done => {
236 const store = mockStore({});
237 const value = 'test';
239 store.dispatch(clearSuggestionsTextField());
241 function onClearSuggestionsTextField() {
242 return {type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD};
245 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onClearSuggestionsTextField()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
246 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
249 it('Expect CLEAR_SUGGESTIONS action being passed when onSuggestionsClearRequested is dispatched with no mock, and network error is sent in mock', done => {
250 const store = mockStore({});
251 const value = 'test';
253 store.dispatch(onSuggestionsClearRequested());
255 function onSuggestionsClearRequestedExpected() {
256 return{type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS};
259 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedExpected()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
260 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);