cleaned up code
[aai/sparky-fe.git] / test / tierSupport / autoCompleteSearchBar / autoCompleteSearchBarActions.test.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
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";
30 import {
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';
36 import {
37         searchResultsFound,
38         clearSuggestionsTextField,
39         onSuggestionsChange,
40         onSuggestionsClearRequested,
41         getInvalidSearchInputEvent,
42         fetchRequestedValues,
43         fetchSelectedNodeElement,
44         queryRequestedValues
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";
49 import {POST,
50         POST_HEADER,
51         ERROR_RETRIEVING_DATA} from "app/networking/NetworkConstants.js";
52 import autoCompleteSearchBarTestConstants from "./autoCompleteSearchBarTestConstants";
53
54 const middlewares = [thunk]; // add your middlewares like `redux-thunk`
55 const mockStore = configureStore(middlewares);
56
57
58 describe('Core AutoCompleteSearchBar suite', function() {
59
60         describe('AutoCompleteSearchBar - Actions test ', function() {
61
62                 function createState(currentScreen, tierSupport) {
63                         return {
64                                 currentScreen: currentScreen,
65                                 tierSupport: tierSupport
66                         };
67                 }
68
69                 it('Expect CLEAR_SUGGESTIONS_TEXT_FIELD action being passed When clearSuggestionsTextField is dispatched.', function(){
70                         const store = mockStore({});
71                         function clearSuggestionsTextFieldSuccess() {
72                                 return {
73                                         type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD
74                                 }
75                         }
76
77                         store.dispatch(clearSuggestionsTextField());
78                         expect(store.getActions()[0]).to.deep.equal(clearSuggestionsTextFieldSuccess());
79                 });
80
81                 it('Expect CLEAR_SUGGESTIONS action being passed When onSuggestionsClearRequested is dispatched.', function() {
82                         const store = mockStore({});
83                         function onSuggestionsClearRequestedSuccess() {
84                                 return {
85                                         type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS
86                                 }
87                         }
88
89                         store.dispatch(onSuggestionsClearRequested());
90                         expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedSuccess());
91
92                 });
93
94                 it('Expect TS_NODE_SEARCH_INVALID_TERMS action being passed When getInvalidSearchInputEvent is dispatched.', function(){
95                         const store = mockStore({});
96                         const value = 'test';
97
98                         function onGetInvalidSearchInputEvent(){
99                                 return{
100                                         type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS,
101                                         data: {value: value, errorMsg: ERROR_INVALID_SEARCH_TERMS}
102                                 }
103                         }
104
105                         store.dispatch(getInvalidSearchInputEvent(value));
106                         expect(store.getActions()[0]).to.deep.equal(onGetInvalidSearchInputEvent());
107
108                 });
109
110         });
111
112         it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChangeSuccess is dispatched with tab key.', function() {
113                 const store = mockStore({});
114                 const value = 'test';
115
116                 function onSuggestionsChangeSuccess() {
117                         return {
118                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED,
119                                 data: value
120                         }
121                 }
122
123                 var event = {
124                         keyCode: 9
125                 };
126
127                 store.dispatch(onSuggestionsChange(event, value));
128                 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSuccess());
129
130         });
131
132         it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChange is dispatched with enter key.', function() {
133                 const store = mockStore({});
134                 const value = 'test';
135
136                 function onSuggestionsChangeSucessfull() {
137                         return {type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED, data: value};
138                 }
139
140                 var event = {
141                         keyCode: 13
142                 };
143
144                 store.dispatch(onSuggestionsChange(event, value));
145                 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSucessfull());
146         });
147
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';
151
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();
157
158                 function onCreateSuggestionFoundEvent() {
159                         return {
160                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
161                                 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
162                         };
163                 }
164
165                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
166                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
167
168
169         });
170
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';
174
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() {
181                         return {
182                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND
183                         };
184                 }
185
186                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionNotFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
187                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
188         });
189
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';
193
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();
199
200                 function onGetInvalidQueryEvent() {
201                         return {
202                                 type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
203                                 data: {value: value, errorMsg: ERROR_RETRIEVING_DATA}
204                         };
205                 }
206
207                 setTimeout(() => {
208                         expect(store.getActions()[0].type.toString()).to.equal(tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR.toString()), autoCompleteSearchBarTestConstants.mockRequestTimeOut
209                 });
210                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
211         });
212
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';
216
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();
222
223                 function onCreateSuggestionFoundEvent() {
224                         return {
225                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
226                                 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
227                         };
228                 }
229
230                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
231                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
232         });
233
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';
237
238                 store.dispatch(clearSuggestionsTextField());
239
240                 function onClearSuggestionsTextField() {
241                         return {type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD};
242                 }
243
244                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onClearSuggestionsTextField()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
245                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
246         });
247
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';
251
252                 store.dispatch(onSuggestionsClearRequested());
253
254                 function onSuggestionsClearRequestedExpected() {
255                         return{type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS};
256                 }
257
258                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedExpected()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
259                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
260         });
261 });