changed the header and license
[aai/sparky-fe.git] / test / tierSupport / autoCompleteSearchBar / autoCompleteSearchBarActions.test.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 import {expect, deep} from "chai";
22 import React from "react";
23 import {Provider} from "react-redux";
24 import sinon from "sinon";
25 import configureStore from "redux-mock-store";
26 import thunk from "redux-thunk";
27 import {storeCreator} from "app/AppStore.js";
28 import {
29         autoCompleteSearchBarActionTypes,
30         TS_BACKEND_SEARCH_SELECTED_NODE_URL,
31         ERROR_INVALID_SEARCH_TERMS,
32         TIER_SUPPORT_NETWORK_ERROR
33 } from 'generic-components/autoCompleteSearchBar/AutoCompleteSearchBarConstants.js';
34 import {
35         searchResultsFound,
36         clearSuggestionsTextField,
37         onSuggestionsChange,
38         onSuggestionsClearRequested,
39         getInvalidSearchInputEvent,
40         fetchRequestedValues,
41         fetchSelectedNodeElement,
42         queryRequestedValues
43 } from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarActions.js';
44 import {tierSupportActionTypes, TSUI_SEARCH_URL} from "app/tierSupport/TierSupportConstants.js";
45 import {getDynamicTSUISearchURL} from "app/tierSupport/TierSupportActions.js";
46 import * as networkCall from "app/networking/NetworkCalls.js";
47 import {POST,
48         POST_HEADER,
49         ERROR_RETRIEVING_DATA} from "app/networking/NetworkConstants.js";
50 import autoCompleteSearchBarTestConstants from "./autoCompleteSearchBarTestConstants";
51
52 const middlewares = [thunk]; // add your middlewares like `redux-thunk`
53 const mockStore = configureStore(middlewares);
54
55
56 describe('Core AutoCompleteSearchBar suite', function() {
57
58         describe('AutoCompleteSearchBar - Actions test ', function() {
59
60                 function createState(currentScreen, tierSupport) {
61                         return {
62                                 currentScreen: currentScreen,
63                                 tierSupport: tierSupport
64                         };
65                 }
66
67                 it('Expect CLEAR_SUGGESTIONS_TEXT_FIELD action being passed When clearSuggestionsTextField is dispatched.', function(){
68                         const store = mockStore({});
69                         function clearSuggestionsTextFieldSuccess() {
70                                 return {
71                                         type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD
72                                 }
73                         }
74
75                         store.dispatch(clearSuggestionsTextField());
76                         expect(store.getActions()[0]).to.deep.equal(clearSuggestionsTextFieldSuccess());
77                 });
78
79                 it('Expect CLEAR_SUGGESTIONS action being passed When onSuggestionsClearRequested is dispatched.', function() {
80                         const store = mockStore({});
81                         function onSuggestionsClearRequestedSuccess() {
82                                 return {
83                                         type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS
84                                 }
85                         }
86
87                         store.dispatch(onSuggestionsClearRequested());
88                         expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedSuccess());
89
90                 });
91
92                 it('Expect TS_NODE_SEARCH_INVALID_TERMS action being passed When getInvalidSearchInputEvent is dispatched.', function(){
93                         const store = mockStore({});
94                         const value = 'test';
95
96                         function onGetInvalidSearchInputEvent(){
97                                 return{
98                                         type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS,
99                                         data: {value: value, errorMsg: ERROR_INVALID_SEARCH_TERMS}
100                                 }
101                         }
102
103                         store.dispatch(getInvalidSearchInputEvent(value));
104                         expect(store.getActions()[0]).to.deep.equal(onGetInvalidSearchInputEvent());
105
106                 });
107
108         });
109
110         it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChangeSuccess is dispatched with tab key.', function() {
111                 const store = mockStore({});
112                 const value = 'test';
113
114                 function onSuggestionsChangeSuccess() {
115                         return {
116                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED,
117                                 data: value
118                         }
119                 }
120
121                 var event = {
122                         keyCode: 9
123                 };
124
125                 store.dispatch(onSuggestionsChange(event, value));
126                 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSuccess());
127
128         });
129
130         it('Expect SUGGESTION_CHANGED action being passed When onSuggestionsChange is dispatched with enter key.', function() {
131                 const store = mockStore({});
132                 const value = 'test';
133
134                 function onSuggestionsChangeSucessfull() {
135                         return {type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED, data: value};
136                 }
137
138                 var event = {
139                         keyCode: 13
140                 };
141
142                 store.dispatch(onSuggestionsChange(event, value));
143                 expect(store.getActions()[0]).to.deep.equal(onSuggestionsChangeSucessfull());
144         });
145
146         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 => {
147                 const store = mockStore({});
148                 const value = 'test';
149
150                 let mockNetwork = sinon.mock(networkCall);
151                 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1));
152                 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
153                 mockNetwork.verify();
154                 mockNetwork.restore();
155
156                 function onCreateSuggestionFoundEvent() {
157                         return {
158                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
159                                 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
160                         };
161                 }
162
163                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
164                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
165
166
167         });
168
169         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 => {
170                 const store = mockStore({});
171                 const value = 'test';
172
173                 let mockNetwork = sinon.mock(networkCall);
174                 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithOutHits));
175                 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
176                 mockNetwork.verify();
177                 mockNetwork.restore();
178                 function onCreateSuggestionNotFoundEvent() {
179                         return {
180                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND
181                         };
182                 }
183
184                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionNotFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
185                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
186         });
187
188         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 => {
189                 const store = mockStore({});
190                 const value = 'test';
191
192                 let mockNetwork = sinon.mock(networkCall);
193                 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.networkError));
194                 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
195                 mockNetwork.verify();
196                 mockNetwork.restore();
197
198                 function onGetInvalidQueryEvent() {
199                         return {
200                                 type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
201                                 data: {value: value, errorMsg: ERROR_RETRIEVING_DATA}
202                         };
203                 }
204
205                 setTimeout(() => {
206                         expect(store.getActions()[0].type.toString()).to.equal(tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR.toString()), autoCompleteSearchBarTestConstants.mockRequestTimeOut
207                 });
208                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
209         });
210
211         it('Expect fetchRequest being called once and SUGGESTION_FOUND action being when passed queryRequestedValues is dispatched, and network error is sent in mock', done => {
212                 const store = mockStore({});
213                 const value = 'test';
214
215                 let mockNetwork = sinon.mock(networkCall);
216                 mockNetwork.expects('fetchRequest').once().withArgs(TSUI_SEARCH_URL, POST, POST_HEADER, value).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1));
217                 store.dispatch(fetchRequestedValues(() => networkCall.fetchRequest(TSUI_SEARCH_URL, POST, POST_HEADER, value)));
218                 mockNetwork.verify();
219                 mockNetwork.restore();
220
221                 function onCreateSuggestionFoundEvent() {
222                         return {
223                                 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,
224                                 data: {suggestions : autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits }
225                         };
226                 }
227
228                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onCreateSuggestionFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
229                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
230         });
231
232         it('Expect TIER_SUPPORT_NETWORK_ERROR action being passed when clearSuggestionsTextField is dispatched with no mock, and network error is sent in mock', done => {
233                 const store = mockStore({});
234                 const value = 'test';
235
236                 store.dispatch(clearSuggestionsTextField());
237
238                 function onClearSuggestionsTextField() {
239                         return {type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD};
240                 }
241
242                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onClearSuggestionsTextField()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
243                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
244         });
245
246         it('Expect CLEAR_SUGGESTIONS action being passed when onSuggestionsClearRequested is dispatched with no mock, and network error is sent in mock', done => {
247                 const store = mockStore({});
248                 const value = 'test';
249
250                 store.dispatch(onSuggestionsClearRequested());
251
252                 function onSuggestionsClearRequestedExpected() {
253                         return{type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS};
254                 }
255
256                 setTimeout(() => expect(store.getActions()[0]).to.deep.equal(onSuggestionsClearRequestedExpected()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
257                 setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
258         });
259 });