1
+ import { AxiosError } from 'axios'
1
2
import { cloneDeep } from 'lodash'
2
3
import { apiService } from 'uiSrc/services'
4
+ import { addErrorNotification } from 'uiSrc/slices/app/notifications'
3
5
import { initialStateDefault , mockedStore } from 'uiSrc/utils/test-utils'
4
6
import reducer , {
5
7
initialState ,
@@ -28,6 +30,26 @@ describe('clientCerts slice', () => {
28
30
} )
29
31
} )
30
32
33
+ describe ( 'deleteClientCert' , ( ) => {
34
+ it ( 'should properly set loading = true' , ( ) => {
35
+ // Arrange
36
+ const state = {
37
+ ...initialState ,
38
+ loading : true ,
39
+ }
40
+
41
+ // Act
42
+ const nextState = reducer ( initialState , deleteClientCert ( ) )
43
+
44
+ const rootState = Object . assign ( initialStateDefault , {
45
+ connections : {
46
+ clientCerts : nextState ,
47
+ } ,
48
+ } )
49
+ expect ( clientCertsSelector ( rootState ) ) . toEqual ( state )
50
+ } )
51
+ }
52
+
31
53
describe ( 'loadClientCerts' , ( ) => {
32
54
it ( 'should properly set the state before the fetch data' , ( ) => {
33
55
// Arrange
@@ -49,6 +71,27 @@ describe('clientCerts slice', () => {
49
71
} )
50
72
} )
51
73
74
+ describe ( 'deleteClientCertSuccess' , ( ) => {
75
+ it ( 'should properly set the state with fetched data' , ( ) => {
76
+ // Arrange
77
+ const state = {
78
+ ...initialState ,
79
+ loading : false ,
80
+ }
81
+
82
+ // Act
83
+ const nextState = reducer ( initialState , deleteClientCertSuccess ( ) )
84
+
85
+ // Assert
86
+ const rootState = Object . assign ( initialStateDefault , {
87
+ connections : {
88
+ clientCerts : nextState ,
89
+ } ,
90
+ } )
91
+ expect ( clientCertsSelector ( rootState ) ) . toEqual ( state )
92
+ } )
93
+ }
94
+
52
95
describe ( 'loadClientCertsSuccess' , ( ) => {
53
96
it ( 'should properly set the state with fetched data' , ( ) => {
54
97
// Arrange
@@ -97,6 +140,30 @@ describe('clientCerts slice', () => {
97
140
} )
98
141
} )
99
142
143
+ describe ( 'deleteClientCertFailure' , ( ) => {
144
+ it ( 'should properly set the error' , ( ) => {
145
+ // Arrange
146
+ const data = 'some error'
147
+ const state = {
148
+ ...initialState ,
149
+ loading : false ,
150
+ error : data ,
151
+ data : [ ] ,
152
+ }
153
+
154
+ // Act
155
+ const nextState = reducer ( initialState , deleteClientCertFailure ( data ) )
156
+
157
+ // Assert
158
+ const rootState = Object . assign ( initialStateDefault , {
159
+ connections : {
160
+ clientCerts : nextState ,
161
+ } ,
162
+ } )
163
+ expect ( clientCertsSelector ( rootState ) ) . toEqual ( state )
164
+ } )
165
+ } )
166
+
100
167
describe ( 'loadClientCertsFailure' , ( ) => {
101
168
it ( 'should properly set the error' , ( ) => {
102
169
// Arrange
@@ -142,5 +209,68 @@ describe('clientCerts slice', () => {
142
209
]
143
210
expect ( store . getActions ( ) ) . toEqual ( expectedActions )
144
211
} )
212
+
213
+ it ( 'call both fetchClientCerts and deleteClientCertSuccess when delete is successed' , async ( ) => {
214
+ // Arrange delete
215
+ const responsePayload = { status : 200 }
216
+ apiService . delete = jest . fn ( ) . mockResolvedValue ( responsePayload )
217
+
218
+ // Arrange fetch
219
+ const data = [
220
+ { id : '70b95d32-c19d-4311-bb24-e684af12cf15' , name : 'client cert' } ,
221
+ ]
222
+ const fetchResponsePayload = { data, status : 200 }
223
+ apiService . get = jest . fn ( ) . mockResolvedValue ( fetchResponsePayload )
224
+
225
+ // mock function for onSuccessAction
226
+ const onSuccessAction = jest . fn ( )
227
+
228
+ const id = '70b95d32-c19d-4311-bb24-e684af12cf15'
229
+
230
+ const store = cloneDeep ( mockedStore )
231
+
232
+ // Act
233
+ await store . dispatch < any > ( deleteClientCert ( id , onSuccessAction ) )
234
+
235
+ // Assert onSuccessAction
236
+ expect ( onSuccessAction ) . toBeCalled ( )
237
+
238
+ // Assert
239
+ const expectedActions = [
240
+ deleteClientCert ( ) ,
241
+ deleteClientCertSuccess ( ) ,
242
+ loadClientCerts ( ) ,
243
+ loadClientCertsSuccess ( fetchResponsePayload . data ) ,
244
+ ]
245
+ expect ( store . getActions ( ) ) . toEqual ( expectedActions )
246
+ } )
247
+
248
+ it ( 'call both fetchClientCerts and deleteClientCertFailure when delete is failed' , async ( ) => {
249
+ // Arrange delete
250
+ const error = 'some error'
251
+ const responsePayload = {
252
+ response : { data : { message : error } , status : 500 } ,
253
+ }
254
+ apiService . delete = jest . fn ( ) . mockRejectedValueOnce ( responsePayload )
255
+
256
+ const onSuccessAction = jest . fn ( )
257
+ const id = '70b95d32-c19d-4311-bb24-e684af12cf15'
258
+
259
+ const store = cloneDeep ( mockedStore )
260
+
261
+ // Act
262
+ await store . dispatch < any > ( deleteClientCert ( id , onSuccessAction ) )
263
+
264
+ // assert that onSuccessAction is not called
265
+ expect ( onSuccessAction ) . not . toBeCalled ( )
266
+
267
+ // Assert
268
+ const expectedActions = [
269
+ deleteClientCert ( ) ,
270
+ addErrorNotification ( responsePayload as AxiosError ) ,
271
+ deleteClientCertFailure ( responsePayload . response . data . message ) ,
272
+ ]
273
+ expect ( store . getActions ( ) ) . toEqual ( expectedActions )
274
+ } )
145
275
} )
146
276
} )
0 commit comments