2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 import {expect, deep} from "chai";
27 import React from "react";
28 import {Provider} from "react-redux";
29 import sinon from "sinon";
30 import configureStore from "redux-mock-store";
31 import thunk from "redux-thunk";
32 import {storeCreator} from "app/AppStore.js";
34 autoCompleteSearchBarActionTypes,
35 TS_BACKEND_SEARCH_SELECTED_NODE_URL,
36 ERROR_INVALID_SEARCH_TERMS,
37 TIER_SUPPORT_NETWORK_ERROR
38 } from "app/tierSupport/autoCompleteSearchBar/AutoCompleteSearchBarConstants.js";
41 clearSuggestionsTextField,
43 onSuggestionsClearRequested,
44 getInvalidSearchInputEvent,
46 fetchSelectedNodeElement,
48 } from "app/tierSupport/autoCompleteSearchBar/AutoCompleteSearchBarActions.js";
49 import {tierSupportActionTypes, TSUI_SEARCH_URL} from "app/tierSupport/TierSupportConstants.js";
50 import {TABLE_DATA} from "app/tierSupport/selectedNodeDetails/SelectedNodeDetailsConstants.js";
51 import {getDynamicTSUISearchURL} from "app/tierSupport/TierSupportActions.js";
52 import * as networkCall from "app/networking/NetworkCalls.js";
55 ERROR_RETRIEVING_DATA} from "app/networking/NetworkConstants.js";
56 import autoCompleteSearchBarTestConstants from "./autoCompleteSearchBarTestConstants";
58 const middlewares = [thunk]; // add your middlewares like `redux-thunk`
59 const mockStore = configureStore(middlewares);
62 describe('Core AutoCompleteSearchBar suite', function() {
64 describe('AutoCompleteSearchBar - Actions test ', function() {
66 function createState(currentScreen, tierSupport) {
68 currentScreen: currentScreen,
69 tierSupport: tierSupport
73 it('Expect CLEAR_SUGGESTIONS_TEXT_FIELD action being passed When clearSuggestionsTextField is dispatched.', function(){
74 const store = mockStore({});
75 function clearSuggestionsTextFieldSuccess() {
77 type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD
81 store.dispatch(clearSuggestionsTextField());
82 expect(store.getActions()[0]).to.deep.equal(clearSuggestionsTextFieldSuccess());
85 it('Expect CLEAR_SUGGESTIONS action being passed When onSuggestionsClearRequested is dispatched.', function() {
86 const store = mockStore({});
87 function onSuggestionsClearRequestedSuccess() {
89 type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS
93 store.dispatch(onSuggestionsClearRequested());
94 expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedSuccess());
98 it('Expect TS_NODE_SEARCH_INVALID_TERMS action being passed When getInvalidSearchInputEvent is dispatched.', function(){
99 const store = mockStore({});
100 const value = 'test';
102 function onGetInvalidSearchInputEvent(){
104 type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS,
105 data: {value: value, errorMsg: ERROR_INVALID_SEARCH_TERMS}
109 store.dispatch(getInvalidSearchInputEvent(value));
110 expect(store.getActions()[0]).to.deep.equal(onGetInvalidSearchInputEvent());
116 it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChangeSuccess is dispatched with tab key.', function() {
117 const store = mockStore({});
118 const value = 'test';
120 function onSuggestionsChangeSuccess() {
122 type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED,
131 store.dispatch(onSuggestionsChange(event, value));
132 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSuccess());
136 it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChange is dispatched with enter key.', function() {
137 const store = mockStore({});
138 const value = 'test';
140 function onSuggestionsChangeSucessfull() {
141 return {type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED, data: value};
148 store.dispatch(onSuggestionsChange(event, value));
149 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSucessfull());
152 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 => {
153 const store = mockStore({});
154 const value = 'test';
156 let mockNetwork = sinon.mock(networkCall);
157 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1));
158 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
159 mockNetwork.verify();
160 mockNetwork.restore();
162 function onCreateSuggestionFoundEvent() {
164 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
165 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
169 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
170 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
175 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 => {
176 const store = mockStore({});
177 const value = 'test';
179 let mockNetwork = sinon.mock(networkCall);
180 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithOutHits));
181 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
182 mockNetwork.verify();
183 mockNetwork.restore();
184 function onCreateSuggestionNotFoundEvent() {
186 type: autoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND
190 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionNotFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
191 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
194 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 => {
195 const store = mockStore({});
196 const value = 'test';
198 let mockNetwork = sinon.mock(networkCall);
199 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.networkError));
200 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
201 mockNetwork.verify();
202 mockNetwork.restore();
204 function onGetInvalidQueryEvent() {
206 type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
207 data: {value: value, errorMsg: ERROR_RETRIEVING_DATA}
212 expect(store.getActions()[0].type.toString()).to.equal(tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR.toString()), autoCompleteSearchBarTestConstants.mockRequestTimeOut
214 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
217 it('Expect fetchRequest being called once and SUGGESTION_FOUND action being when passed queryRequestedValues is dispatched, and network error is sent in mock', done => {
218 const store = mockStore({});
219 const value = 'test';
221 let mockNetwork = sinon.mock(networkCall);
222 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1));
223 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
224 mockNetwork.verify();
225 mockNetwork.restore();
227 function onCreateSuggestionFoundEvent() {
229 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
230 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
234 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
235 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
238 it('Expect TIER_SUPPORT_NETWORK_ERROR action being passed when clearSuggestionsTextField is dispatched with no mock, and network error is sent in mock', done => {
239 const store = mockStore({});
240 const value = 'test';
242 store.dispatch(clearSuggestionsTextField());
244 function onClearSuggestionsTextField() {
245 return {type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD};
248 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onClearSuggestionsTextField()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
249 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
252 it('Expect CLEAR_SUGGESTIONS action being passed when onSuggestionsClearRequested is dispatched with no mock, and network error is sent in mock', done => {
253 const store = mockStore({});
254 const value = 'test';
256 store.dispatch(onSuggestionsClearRequested());
258 function onSuggestionsClearRequestedExpected() {
259 return{type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS};
262 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedExpected()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
263 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);