f3f1c1928ba725eabe0141109a67857530f52694
[aai/sparky-fe.git] / test / tierSupport / autoCompleteSearchBar / autoCompleteSearchBarReducer.test.js
1 /*\r
2  * ============LICENSE_START=======================================================\r
3  * SPARKY (AAI UI service)\r
4  * ================================================================================\r
5  * Copyright © 2017 AT&T Intellectual Property.\r
6  * Copyright © 2017 Amdocs\r
7  * All rights reserved.\r
8  * ================================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  *\r
13  *      http://www.apache.org/licenses/LICENSE-2.0\r
14  *\r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  * ============LICENSE_END=========================================================\r
21  *\r
22  * ECOMP and OpenECOMP are trademarks\r
23  * and service marks of AT&T Intellectual Property.\r
24  */\r
25 \r
26 import {expect} from 'chai';\r
27 import React from 'react';\r
28 import {Provider} from 'react-redux';\r
29 import TestUtils from 'react-dom/lib/ReactTestUtils';\r
30 import {tierSupportActionTypes} from 'app/tierSupport/TierSupportConstants.js';\r
31 import reducer from 'app/tierSupport/autoCompleteSearchBar/AutoCompleteSearchBarReducer.js';\r
32 import {MESSAGE_LEVEL_WARNING, MESSAGE_LEVEL_DANGER} from 'utils/GlobalConstants.js';\r
33 import {\r
34     autoCompleteSearchBarActionTypes,\r
35     NO_MATCHES_FOUND,\r
36     ERROR_INVALID_SEARCH_TERMS} from 'app/tierSupport/autoCompleteSearchBar/AutoCompleteSearchBarConstants.js';\r
37 import {\r
38     ERROR_RETRIEVING_DATA} from 'app/networking/NetworkConstants.js';\r
39 \r
40 describe('AutoCompleteSearchBar - Reducer test ', function() {\r
41 \r
42     const testSuggestionData = [\r
43         {_source:{ entityType: 'vserver',searchTags:'testing'}},\r
44         {_source:{ entityType: 'vserver',searchTags:'someTag'}},\r
45         {_source:{ entityType: 'vserver',searchTags:'test2'}}];\r
46 \r
47     it('AutoCompleteSearchBar reducer - Suggestion not found', function() {\r
48         const feedbackMsgText = '';\r
49         const suggestions = [];\r
50         const action = {type: autoCompleteSearchBarActionTypes.SUGGESTION_NOT_FOUND};\r
51         const initialState = {\r
52             suggestions: suggestions\r
53         };\r
54 \r
55         const newState = reducer(initialState, action);\r
56         expect(newState.suggestions).to.have.deep.property('[0]._source.entityType', 'No Matches Found');\r
57     });\r
58 \r
59     it('AutoCompleteSearchBar reducer - Suggestion found', function() {\r
60         const feedbackMsgText = '';\r
61         const action =\r
62             {\r
63                 type: autoCompleteSearchBarActionTypes.SUGGESTION_FOUND,\r
64                 data: {\r
65                     suggestions: testSuggestionData\r
66                 }\r
67             };\r
68         const initialState = {\r
69             suggestions: []\r
70         };\r
71 \r
72         const newState = reducer(initialState, action);\r
73         expect(newState.suggestions).to.have.deep.property('[0]._source.searchTags', 'testing');\r
74         expect(newState.suggestions).to.have.deep.property('[1]._source.searchTags', 'someTag');\r
75         expect(newState.suggestions).to.have.deep.property('[2]._source.searchTags', 'test2');\r
76     });\r
77 \r
78     it('AutoCompleteSearchBar reducer - Clear Suggestion field', function() {\r
79         const feedbackMsgText = '';\r
80         const action = {type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS_TEXT_FIELD, value: 'test'};\r
81         const initialState = {\r
82             suggestions: testSuggestionData\r
83         };\r
84 \r
85         const newState = reducer(initialState, action);\r
86         expect(newState.suggestions).to.be.empty;\r
87         expect(newState.value).to.equal('');\r
88     });\r
89 \r
90     it('AutoCompleteSearchBar reducer - Clear Suggestions', function() {\r
91         const feedbackMsgText = '';\r
92         const action = {type: autoCompleteSearchBarActionTypes.CLEAR_SUGGESTIONS, value: 'test'};\r
93         const initialState = {\r
94             suggestions: testSuggestionData\r
95         };\r
96 \r
97         const newState = reducer(initialState, action);\r
98         expect(newState.suggestions).to.be.empty;\r
99     });\r
100 \r
101     it('AutoCompleteSearchBar reducer - Suggestions changed', function() {\r
102         const feedbackMsgText = '';\r
103         const action = {type: autoCompleteSearchBarActionTypes.SUGGESTION_CHANGED, value: 'test'};\r
104         const initialState = {\r
105             suggestions: testSuggestionData\r
106         };\r
107 \r
108         const newState = reducer(initialState, action);\r
109         expect(newState.suggestions).to.have.deep.property('[0]._source.searchTags', 'testing');\r
110         expect(newState.suggestions).to.have.deep.property('[1]._source.searchTags', 'someTag');\r
111         expect(newState.suggestions).to.have.deep.property('[2]._source.searchTags', 'test2');\r
112     });\r
113 \r
114     it('AutoCompleteSearchBar reducer - Network error', function() {\r
115         const feedbackMsgText = '';\r
116         const feedbackMsgSeverity = '';\r
117         const action = {type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR, data: {value: 'test', errorMsg: ERROR_RETRIEVING_DATA}};\r
118         const initialState = {\r
119             suggestions: testSuggestionData\r
120         };\r
121 \r
122         const newState = reducer(initialState, action);\r
123         expect(newState.suggestions).to.be.empty;\r
124         expect(newState.feedbackMsgText).to.equal(ERROR_RETRIEVING_DATA);\r
125         expect(newState.feedbackMsgSeverity).to.equal(MESSAGE_LEVEL_DANGER);\r
126     });\r
127 \r
128     it('AutoCompleteSearchBar reducer - TS_NODE_SEARCH_INVALID_TERMS', function(){\r
129         const feedbackMsgText = ERROR_INVALID_SEARCH_TERMS;\r
130         const action = {type: tierSupportActionTypes.TS_NODE_SEARCH_INVALID_TERMS, data: {value: 'test', errorMsg: ERROR_INVALID_SEARCH_TERMS}};\r
131         const initialState = {\r
132             suggestions: testSuggestionData\r
133         };\r
134         const newState = reducer(initialState, action);\r
135         expect(newState.suggestions).to.be.empty;\r
136         expect(newState.cachedSuggestions).to.be.empty;\r
137         expect(newState.feedbackMsgText).to.equal(ERROR_INVALID_SEARCH_TERMS);\r
138         expect(newState.feedbackMsgSeverity).to.equal(MESSAGE_LEVEL_WARNING);\r
139     });\r
140 \r
141 });\r