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