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