Initial coomit for AAI-UI(sparky-fe)
[aai/sparky-fe.git] / test / tierSupport / autoCompleteSearchBar / autoCompleteSearchBarActions.test.js
1 /*
2  * ============LICENSE_START=======================================================
3  * SPARKY (AAI UI service)
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
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";
33 import {
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";
39 import {
40         searchResultsFound,
41         clearSuggestionsTextField,
42         onSuggestionsChange,
43         onSuggestionsClearRequested,
44         getInvalidSearchInputEvent,
45         fetchRequestedValues,
46         fetchSelectedNodeElement,
47         queryRequestedValues
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";
53 import {POST,
54         POST_HEADER,
55         ERROR_RETRIEVING_DATA} from "app/networking/NetworkConstants.js";
56 import autoCompleteSearchBarTestConstants from "./autoCompleteSearchBarTestConstants";
57
58 const middlewares = [thunk]; // add your middlewares like `redux-thunk`
59 const mockStore = configureStore(middlewares);
60
61
62 describe('Core AutoCompleteSearchBar suite', function() {
63
64         describe('AutoCompleteSearchBar - Actions test ', function() {
65
66                 function createState(currentScreen, tierSupport) {
67                         return {
68                                 currentScreen: currentScreen,
69                                 tierSupport: tierSupport
70                         };
71                 }
72
73                 it('Expect CLEAR_SUGGESTIONS_TEXT_FIELD action being passed When clearSuggestionsTextField is dispatched.', function(){
74                         const store = mockStore({});
75                         function clearSuggestionsTextFieldSuccess() {
76                                 return {
77                                         type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD
78                                 }
79                         }
80
81                         store.dispatch(clearSuggestionsTextField());
82                         expect(store.getActions()[0]).to.deep.equal(clearSuggestionsTextFieldSuccess());
83                 });
84
85                 it('Expect CLEAR_SUGGESTIONS action being passed When onSuggestionsClearRequested is dispatched.', function() {
86                         const store = mockStore({});
87                         function onSuggestionsClearRequestedSuccess() {
88                                 return {
89                                         type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS
90                                 }
91                         }
92
93                         store.dispatch(onSuggestionsClearRequested());
94                         expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedSuccess());
95
96                 });
97
98                 it('Expect TS_NODE_SEARCH_INVALID_TERMS action being passed When getInvalidSearchInputEvent is dispatched.', function(){
99                         const store = mockStore({});
100                         const value = 'test';
101
102                         function onGetInvalidSearchInputEvent(){
103                                 return{
104                                         type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS,
105                                         data: {value: value, errorMsg: ERROR_INVALID_SEARCH_TERMS}
106                                 }
107                         }
108
109                         store.dispatch(getInvalidSearchInputEvent(value));
110                         expect(store.getActions()[0]).to.deep.equal(onGetInvalidSearchInputEvent());
111
112                 });
113
114         });
115
116         it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChangeSuccess is dispatched with tab key.', function() {
117                 const store = mockStore({});
118                 const value = 'test';
119
120                 function onSuggestionsChangeSuccess() {
121                         return {
122                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED,
123                                 data: value
124                         }
125                 }
126
127                 var event = {
128                         keyCode: 9
129                 };
130
131                 store.dispatch(onSuggestionsChange(event, value));
132                 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSuccess());
133
134         });
135
136         it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChange is dispatched with enter key.', function() {
137                 const store = mockStore({});
138                 const value = 'test';
139
140                 function onSuggestionsChangeSucessfull() {
141                         return {type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED, data: value};
142                 }
143
144                 var event = {
145                         keyCode: 13
146                 };
147
148                 store.dispatch(onSuggestionsChange(event, value));
149                 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSucessfull());
150         });
151
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';
155
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();
161
162                 function onCreateSuggestionFoundEvent() {
163                         return {
164                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
165                                 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
166                         };
167                 }
168
169                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
170                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
171
172
173         });
174
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';
178
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() {
185                         return {
186                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND
187                         };
188                 }
189
190                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionNotFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
191                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
192         });
193
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';
197
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();
203
204                 function onGetInvalidQueryEvent() {
205                         return {
206                                 type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
207                                 data: {value: value, errorMsg: ERROR_RETRIEVING_DATA}
208                         };
209                 }
210
211                 setTimeout(() => {
212                         expect(store.getActions()[0].type.toString()).to.equal(tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR.toString()), autoCompleteSearchBarTestConstants.mockRequestTimeOut
213                 });
214                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
215         });
216
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';
220
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();
226
227                 function onCreateSuggestionFoundEvent() {
228                         return {
229                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
230                                 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
231                         };
232                 }
233
234                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
235                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
236         });
237
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';
241
242                 store.dispatch(clearSuggestionsTextField());
243
244                 function onClearSuggestionsTextField() {
245                         return {type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD};
246                 }
247
248                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onClearSuggestionsTextField()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
249                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
250         });
251
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';
255
256                 store.dispatch(onSuggestionsClearRequested());
257
258                 function onSuggestionsClearRequestedExpected() {
259                         return{type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS};
260                 }
261
262                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedExpected()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
263                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
264         });
265 });