summaryrefslogtreecommitdiffstats
path: root/test/tierSupport/autoCompleteSearchBar/autoCompleteSearchBar.test.js
blob: d56c44b51e27337a9a8418cb02ff4b4e9d306ece (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * ============LICENSE_START=======================================================
 * SPARKY (AAI UI service)
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property.
 * Copyright © 2017 Amdocs
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP and OpenECOMP are trademarks
 * and service marks of AT&T Intellectual Property.
 */

import {expect, deep} from 'chai';
import React from 'react';
import {Provider} from 'react-redux';
import sinon from 'sinon';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import {storeCreator} from 'app/AppStore.js';
import TestUtils from 'react-dom/lib/ReactTestUtils';
import {
	autoCompleteSearchBarActionTypes,
	ERROR_INVALID_SEARCH_TERMS,
	TS_BACKEND_SEARCH_SELECTED_NODE_URL,
	NO_MATCHES_FOUND
} from 'generic-components/autoCompleteSearchBar/AutoCompleteSearchBarConstants.js';
import {AutoCompleteSearchBar} from 'generic-components/autoCompleteSearchBar/AutoCompleteSearchBar.jsx';
import {ERROR_RETRIEVING_DATA, POST, POST_HEADER} from 'app/networking/NetworkConstants.js';
import {tierSupportActionTypes, TSUI_SEARCH_URL} from 'app/tierSupport/TierSupportConstants.js';
import {TABLE_DATA} from 'app/tierSupport/selectedNodeDetails/SelectedNodeDetailsConstants.js';
import {mount, shallow} from 'enzyme';
import i18n from 'utils/i18n/i18n';
import {
	queryRequestedValues,
	clearSuggestionsTextField,
	onSuggestionsChange,
	onSuggestionsClearRequested,
	querySelectedNodeElement,
	getInvalidSearchInputEvent
} from 'app/tierSupport/autoCompleteSearchBar/AutoCompleteSearchBarActions.js';
import * as networkCall from 'app/networking/NetworkCalls.js';
import autoCompleteSearchBarTestConstants from './autoCompleteSearchBarTestConstants';

const middlewares = [thunk]; // add your middlewares like `redux-thunk`
const mockStore = configureStore(middlewares);


describe('Core AutoCompleteSearchBar suite', function() {

  describe('AutoCompleteSearchBar - View test ', function() {
    let wrapper = undefined;

    beforeEach(() => {
      wrapper = mount(
				<AutoCompleteSearchBar
			onSuggestionsFetchRequested={() => {}}
			value=''
			suggestions={[]}
			cachedSuggestions={[]}
			feedbackMsgText=''
			feedbackMsgSeverity=''
				/>
			);
    });

    function createState(currentScreen, tierSupport) {
      return {
        currentScreen: currentScreen,
        tierSupport: tierSupport
      };
    }



    it('Test flow - test onNewSearch() success test: Expect tierSupportActionTypes.TS_NODE_SEARCH_RESULTS action being passed When the selected node is found in the main database and e', done => {
      const mStore = mockStore({});
      let mockNetwork = sinon.mock(networkCall);
      mockNetwork.expects('fetchRequest').once().withArgs(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword).returns(Promise.resolve(autoCompleteSearchBarTestConstants.validResponseJsonForNodeSearchFromFetchWithHits));
      let mockedNetworkFetchRequest = () => networkCall.fetchRequest(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword);
      wrapper.setProps({
        value: autoCompleteSearchBarTestConstants.nodeSearchKeyword,
        suggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits,
        cachedSuggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits,
        onNewSearch: (searchRequestObject, value) => {
          if (Object.keys(searchRequestObject).length === 0) {
            mStore.dispatch(getInvalidSearchInputEvent(autoCompleteSearchBarTestConstants.nodeSearchKeyword));
          } else {
            mStore.dispatch(querySelectedNodeElement(searchRequestObject, autoCompleteSearchBarTestConstants.nodeSearchKeyword, mockedNetworkFetchRequest));
          }
        }

      });

      function onSucessfullNodeDetailsFoundEvent(){
        return {
          type: tierSupportActionTypes.TS_NODE_SEARCH_RESULTS,
          data: autoCompleteSearchBarTestConstants.validResponseJsonForNodeSearchFromFetchWithHits
        };
      }
      wrapper.find('.auto-complete-search-button').simulate('click');
      mockNetwork.verify();
      mockNetwork.restore();
      setTimeout(() => expect(mStore.getActions()[0]).to.deep.equal(onSucessfullNodeDetailsFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
      setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
    });

    it('Test flow - test onNewSearch() failure test: Expect tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS action being passed When the selected node is not found in the main database and e', done => {

      const mStore = mockStore({});
      let mockNetwork = sinon.mock(networkCall);
      mockNetwork.expects('fetchRequest').once().withArgs(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword).returns(Promise.resolve(autoCompleteSearchBarTestConstants.inValidResponseJsonForNodeSearchFromFetchWithHits));
      let mockedNetworkFetchRequest = () => networkCall.fetchRequest(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword);
      wrapper.setProps({
        value: autoCompleteSearchBarTestConstants.nodeSearchKeyword,
        suggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits,
        cachedSuggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits,
        onNewSearch: (searchRequestObject, value) => {
          if (Object.keys(searchRequestObject).length === 0) {
            mStore.dispatch(getInvalidSearchInputEvent(autoCompleteSearchBarTestConstants.nodeSearchKeyword));
          } else {
            mStore.dispatch(querySelectedNodeElement(searchRequestObject, autoCompleteSearchBarTestConstants.nodeSearchKeyword, mockedNetworkFetchRequest));
          }
        }

      });

      function onNofullNodeDetailsFoundEvent(){
        return {
          type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
          data: {searchTag: autoCompleteSearchBarTestConstants.nodeSearchKeyword}
        };
      }
      wrapper.find('.auto-complete-search-button').simulate('click');
      mockNetwork.verify();
      mockNetwork.restore();
      setTimeout(() => {
        expect(mStore.getActions()[0]).to.deep.equal(onNofullNodeDetailsFoundEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut;
      });
      setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
    });

    it('Test flow - test onNewSearch() failure: Expect tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR action being passed When Network fails', done => {
      const mStore = mockStore({});
      let mockNetwork = sinon.mock(networkCall);
      mockNetwork.expects('fetchRequest').once().withArgs(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword).returns(Promise.resolve(autoCompleteSearchBarTestConstants.networkError));
      let mockedNetworkFetchRequest = () => networkCall.fetchRequest(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword);
      wrapper.setProps({
        value: autoCompleteSearchBarTestConstants.nodeSearchKeyword,
        suggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits,
        cachedSuggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits,
        onNewSearch: (searchRequestObject, value) => {
          if (Object.keys(searchRequestObject).length === 0) {
            mStore.dispatch(getInvalidSearchInputEvent(autoCompleteSearchBarTestConstants.nodeSearchKeyword));
          } else {
            mStore.dispatch(querySelectedNodeElement(searchRequestObject, autoCompleteSearchBarTestConstants.nodeSearchKeyword, mockedNetworkFetchRequest));
          }
        }

      });

      function onInvalidSelectedNodeSearchEvent(){
        return {
          type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
          data: {value: autoCompleteSearchBarTestConstants.nodeSearchKeyword, errorMsg: ERROR_RETRIEVING_DATA}
        };
      }
      wrapper.find('.auto-complete-search-button').simulate('click');
      mockNetwork.verify();
      mockNetwork.restore();
      setTimeout(() => {
        expect(mStore.getActions()[0]).to.deep.equal(onInvalidSelectedNodeSearchEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut;
      });
      setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
    });


    it('Test flow - test onNewSearch() failure: Expect tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS action being passed When no cached suggestions are found', done => {
      const mStore = mockStore({});
      let mockNetwork = sinon.mock(networkCall);
      mockNetwork.expects('fetchRequest').never().withArgs(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword).returns(Promise.resolve(autoCompleteSearchBarTestConstants.networkError));
      let mockedNetworkFetchRequest = () => networkCall.fetchRequest(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword);
      wrapper.setProps({
        value: autoCompleteSearchBarTestConstants.nodeSearchKeyword,
        suggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits,
        cachedSuggestions: [],
        onNewSearch: (searchRequestObject, value) => {
          if (Object.keys(searchRequestObject).length === 0) {
            mStore.dispatch(getInvalidSearchInputEvent(autoCompleteSearchBarTestConstants.nodeSearchKeyword));
          } else {
            mStore.dispatch(querySelectedNodeElement(searchRequestObject, autoCompleteSearchBarTestConstants.nodeSearchKeyword, mockedNetworkFetchRequest));
          }
        }

      });

      function onInvalidSearchInputEvent(){
        return {
          type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS,
          data: {value: autoCompleteSearchBarTestConstants.nodeSearchKeyword, errorMsg: ERROR_INVALID_SEARCH_TERMS}
        };
      }
      wrapper.find('.auto-complete-search-button').simulate('click');
      mockNetwork.verify();
      mockNetwork.restore();
      setTimeout(() => {
        expect(mStore.getActions()[0]).to.deep.equal(onInvalidSearchInputEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut;
      });
      setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
    });

    it('Test flow - test onNewSearch() failure: Expect no matches found When no cached suggestions does not have the node searched for', done => {
      const mStore = mockStore({});
      let mockNetwork = sinon.mock(networkCall);
      mockNetwork.expects('fetchRequest').never().withArgs(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword).returns(Promise.resolve(autoCompleteSearchBarTestConstants.networkError));
      let mockedNetworkFetchRequest = () => networkCall.fetchRequest(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword);
      wrapper.setProps({
        value: autoCompleteSearchBarTestConstants.nodeSearchKeyword,
        suggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits,
        cachedSuggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType2.hits.hits,
        onNewSearch: (searchRequestObject, value) => {
          if (Object.keys(searchRequestObject).length === 0) {
            mStore.dispatch(getInvalidSearchInputEvent(autoCompleteSearchBarTestConstants.nodeSearchKeyword));
          } else {
            mStore.dispatch(querySelectedNodeElement(searchRequestObject, autoCompleteSearchBarTestConstants.nodeSearchKeyword, mockedNetworkFetchRequest));
          }
        }

      });

      function onInvalidSearchInputEvent(){
        return {
          type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS,
          data: {value: autoCompleteSearchBarTestConstants.nodeSearchKeyword, errorMsg: ERROR_INVALID_SEARCH_TERMS}
        };
      }
      wrapper.find('.auto-complete-search-button').simulate('click');
      mockNetwork.verify();
      mockNetwork.restore();
      setTimeout(() => {
        expect(mStore.getActions()[0]).to.deep.equal(onInvalidSearchInputEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut;
      });
      setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
    });

    it('Test flow - test onNewSearch() failure: Expect no node search When no matches are found in lookup search', done => {
      const mStore = mockStore({});
      let mockNetwork = sinon.mock(networkCall);
      mockNetwork.expects('fetchRequest').never().withArgs(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword).returns(Promise.resolve(autoCompleteSearchBarTestConstants.networkError));
      let mockedNetworkFetchRequest = () => networkCall.fetchRequest(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST, POST_HEADER, autoCompleteSearchBarTestConstants.nodeSearchKeyword);
      wrapper.setProps({
        value: autoCompleteSearchBarTestConstants.multipleNodeSearchKeyword,
        suggestions: [{_source:{ entityType:i18n(NO_MATCHES_FOUND),searchTags:''}}],
        cachedSuggestions: [{_source:{ entityType:i18n(NO_MATCHES_FOUND),searchTags:''}}],
        onNewSearch: (searchRequestObject, value) => {
          if (Object.keys(searchRequestObject).length === 0) {
            mStore.dispatch(getInvalidSearchInputEvent(autoCompleteSearchBarTestConstants.nodeSearchKeyword));
          } else {
            mStore.dispatch(querySelectedNodeElement(searchRequestObject, autoCompleteSearchBarTestConstants.nodeSearchKeyword, mockedNetworkFetchRequest));
          }
        }

      });

      function onInvalidSearchInputEvent(){
        return {
          type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS,
          data: {value: autoCompleteSearchBarTestConstants.nodeSearchKeyword, errorMsg: ERROR_INVALID_SEARCH_TERMS}
        };

      }
      wrapper.find('.react-autosuggest__input').simulate('focus');
      wrapper.find('.auto-complete-search-button').simulate('click');
      mockNetwork.verify();
      mockNetwork.restore();
      setTimeout(() => {
        expect(mStore.getActions()[0]).to.deep.equal(onInvalidSearchInputEvent()), autoCompleteSearchBarTestConstants.mockRequestTimeOut;
      });
      setTimeout(() => done(), autoCompleteSearchBarTestConstants.mockRequestTimeOut);
    });

    it('Expect the list to be populated when a the auto suggest input box is focused', function() {
      const mStore = mockStore({});
      wrapper.setProps({
        value: autoCompleteSearchBarTestConstants.nodeSearchKeywordWithOutEqualSign,
        suggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits,
        cachedSuggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType1.hits.hits,

      });
      wrapper.find('.react-autosuggest__input').simulate('focus');
      expect(wrapper.find('Item').children()).to.have.length(6);
    });

    it('Expect small list to be populated when a the auto suggest input box is focused', function() {
      const mStore = mockStore({});
      wrapper.setProps({
        value: autoCompleteSearchBarTestConstants.nodeSearchKeyword,
        suggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType2.hits.hits,
        cachedSuggestions: autoCompleteSearchBarTestConstants.validResponseJsonForRequestFromFetchWithHitsType2.hits.hits,

      });
      wrapper.find('.react-autosuggest__input').simulate('focus');
      expect(wrapper.find('Item').children()).to.have.length(3);
    });
  });
});