react 16 upgrade
[sdc.git] / openecomp-ui / test / utils / errorResponseHandler.test.js
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import deepFreeze from 'deep-freeze';
17
18 import { cloneAndSet } from '../../test-utils/Util.js';
19 import store from 'sdc-app/AppStore.js';
20 import errorResponseHandler from 'nfvo-utils/ErrorResponseHandler.js';
21 import { typeEnum as modalType } from 'nfvo-components/modal/GlobalModalConstants.js';
22
23 describe('Error Response Handler Util', () => {
24     beforeEach(function() {
25         deepFreeze(store.getState());
26     });
27
28     it('validating error in policyException', () => {
29         let textStatus = '',
30             errorThrown = '';
31         let xhr = {
32             data: {
33                 requestError: {
34                     policyException: {
35                         messageId: 'SVC4122',
36                         text: 'Error: Invalid data.'
37                     }
38                 }
39             }
40         };
41         deepFreeze(xhr);
42
43         const errorNotification = {
44             title: 'Error: SVC4122',
45             msg: 'Error: Invalid data.',
46             type: modalType.ERROR
47         };
48
49         const expectedStore = cloneAndSet(
50             store.getState(),
51             'modal',
52             errorNotification
53         );
54
55         errorResponseHandler(xhr, textStatus, errorThrown);
56
57         expect(store.getState()).toEqual(expectedStore);
58     });
59
60     it('validating error in serviceException with variables', () => {
61         let textStatus = '',
62             errorThrown = '';
63         let xhr = {
64             data: {
65                 requestError: {
66                     serviceException: {
67                         messageId: 'SVC4122',
68                         text: "Error: Invalid artifact type '%1'.",
69                         variables: ['newType']
70                     }
71                 }
72             }
73         };
74         deepFreeze(xhr);
75
76         const errorNotification = {
77             title: 'Error: SVC4122',
78             msg: 'Error: Invalid artifact type newType.',
79             type: modalType.ERROR
80         };
81
82         const expectedStore = cloneAndSet(
83             store.getState(),
84             'modal',
85             errorNotification
86         );
87
88         errorResponseHandler(xhr, textStatus, errorThrown);
89
90         expect(store.getState()).toEqual(expectedStore);
91     });
92
93     it('validating error in response', () => {
94         let textStatus = '',
95             errorThrown = '';
96         let xhr = {
97             data: {
98                 status: 'AA',
99                 message: 'Error: Invalid data.'
100             }
101         };
102         deepFreeze(xhr);
103
104         const errorNotification = {
105             title: 'AA',
106             msg: 'Error: Invalid data.',
107             type: modalType.ERROR
108         };
109
110         const expectedStore = cloneAndSet(
111             store.getState(),
112             'modal',
113             errorNotification
114         );
115
116         errorResponseHandler(xhr, textStatus, errorThrown);
117
118         expect(store.getState()).toEqual(expectedStore);
119     });
120
121     it('validating error in request', () => {
122         let textStatus = '',
123             errorThrown = '';
124         let xhr = {
125             statusText: '500',
126             responseText: 'Internal server error.'
127         };
128         deepFreeze(xhr);
129
130         const errorNotification = {
131             title: '500',
132             msg: 'Internal server error.',
133             type: modalType.ERROR
134         };
135
136         const expectedStore = cloneAndSet(
137             store.getState(),
138             'modal',
139             errorNotification
140         );
141
142         errorResponseHandler(xhr, textStatus, errorThrown);
143
144         expect(store.getState()).toEqual(expectedStore);
145     });
146 });