@@ -52,6 +52,38 @@ const suspendUser = async (setupActionSpies: () => void) => {
52
52
fireEvent . click ( confirmButton )
53
53
}
54
54
55
+ const deleteUser = async ( setupActionSpies : ( ) => void ) => {
56
+ // Get the first user in the table
57
+ const users = await screen . findAllByText ( / .* @ c o d e r .c o m / )
58
+ const firstUserRow = users [ 0 ] . closest ( "tr" )
59
+ if ( ! firstUserRow ) {
60
+ throw new Error ( "Error on get the first user row" )
61
+ }
62
+
63
+ // Click on the "more" button to display the "Suspend" option
64
+ const moreButton = within ( firstUserRow ) . getByLabelText ( "more" )
65
+
66
+ fireEvent . click ( moreButton )
67
+
68
+ const menu = await screen . findByRole ( "menu" )
69
+ const suspendButton = within ( menu ) . getByText ( UsersTableBodyLanguage . deleteMenuItem )
70
+
71
+ fireEvent . click ( suspendButton )
72
+
73
+ // Check if the confirm message is displayed
74
+ const confirmDialog = await screen . findByRole ( "dialog" )
75
+ expect ( confirmDialog ) . toHaveTextContent (
76
+ `${ UsersPageLanguage . deleteDialogMessagePrefix } ${ MockUser . username } ?` ,
77
+ )
78
+
79
+ // Setup spies to check the actions after
80
+ setupActionSpies ( )
81
+
82
+ // Click on the "Confirm" button
83
+ const confirmButton = within ( confirmDialog ) . getByText ( UsersPageLanguage . deleteDialogAction )
84
+ fireEvent . click ( confirmButton )
85
+ }
86
+
55
87
const activateUser = async ( setupActionSpies : ( ) => void ) => {
56
88
// Get the first user in the table
57
89
const users = await screen . findAllByText ( / .* @ c o d e r .c o m / )
@@ -229,6 +261,57 @@ describe("UsersPage", () => {
229
261
} )
230
262
} )
231
263
264
+ describe ( "delete user" , ( ) => {
265
+ describe ( "when it is success" , ( ) => {
266
+ it ( "shows a success message and refresh the page" , async ( ) => {
267
+ render (
268
+ < >
269
+ < UsersPage />
270
+ < GlobalSnackbar />
271
+ </ > ,
272
+ )
273
+
274
+ await deleteUser ( ( ) => {
275
+ jest . spyOn ( API , "deleteUser" ) . mockResolvedValueOnce ( undefined )
276
+ jest
277
+ . spyOn ( API , "getUsers" )
278
+ . mockImplementationOnce ( ( ) => Promise . resolve ( [ MockUser , MockUser2 ] ) )
279
+ } )
280
+
281
+ // Check if the success message is displayed
282
+ screen . findByText ( usersXServiceLanguage . deleteUserSuccess )
283
+
284
+ // Check if the API was called correctly
285
+ expect ( API . deleteUser ) . toBeCalledTimes ( 1 )
286
+ expect ( API . deleteUser ) . toBeCalledWith ( MockUser . id )
287
+
288
+ // Check if the users list was reload
289
+ await waitFor ( ( ) => expect ( API . getUsers ) . toBeCalledTimes ( 1 ) )
290
+ } )
291
+ } )
292
+ describe ( "when it fails" , ( ) => {
293
+ it ( "shows an error message" , async ( ) => {
294
+ render (
295
+ < >
296
+ < UsersPage />
297
+ < GlobalSnackbar />
298
+ </ > ,
299
+ )
300
+
301
+ await deleteUser ( ( ) => {
302
+ jest . spyOn ( API , "deleteUser" ) . mockRejectedValueOnce ( { } )
303
+ } )
304
+
305
+ // Check if the error message is displayed
306
+ screen . findByText ( usersXServiceLanguage . deleteUserError )
307
+
308
+ // Check if the API was called correctly
309
+ expect ( API . deleteUser ) . toBeCalledTimes ( 1 )
310
+ expect ( API . deleteUser ) . toBeCalledWith ( MockUser . id )
311
+ } )
312
+ } )
313
+ } )
314
+
232
315
describe ( "activate user" , ( ) => {
233
316
describe ( "when user is successfully activated" , ( ) => {
234
317
it ( "shows a success message and refreshes the page" , async ( ) => {
0 commit comments