@@ -7,6 +7,33 @@ import { MockUser, MockUser2, render } from "../../testHelpers"
7
7
import { Language as usersXServiceLanguage } from "../../xServices/users/usersXService"
8
8
import { Language as UsersPageLanguage , UsersPage } from "./UsersPage"
9
9
10
+ const suspendUser = async ( setupActionSpies :( ) => void ) => {
11
+ // Get the first user in the table
12
+ const users = await screen . findAllByText ( / .* @ c o d e r .c o m / )
13
+ const firstUserRow = users [ 0 ] . closest ( "tr" )
14
+ if ( ! firstUserRow ) {
15
+ throw new Error ( "Error on get the first user row" )
16
+ }
17
+
18
+ // Click on the "more" button to display the "Suspend" option
19
+ const moreButton = within ( firstUserRow ) . getByLabelText ( "more" )
20
+ fireEvent . click ( moreButton )
21
+ const menu = screen . getByRole ( "menu" )
22
+ const suspendButton = within ( menu ) . getByText ( UsersTableLanguage . suspendMenuItem )
23
+ fireEvent . click ( suspendButton )
24
+
25
+ // Check if the confirm message is displayed
26
+ const confirmDialog = screen . getByRole ( "dialog" )
27
+ expect ( confirmDialog ) . toHaveTextContent ( "Do you want to suspend the user TestUser?" )
28
+
29
+ // Setup spies to check the actions after
30
+ setupActionSpies ( )
31
+
32
+ // Click on the "Confirm" button
33
+ const confirmButton = within ( confirmDialog ) . getByText ( UsersPageLanguage . suspendDialogAction )
34
+ fireEvent . click ( confirmButton )
35
+ }
36
+
10
37
describe ( "Users Page" , ( ) => {
11
38
it ( "shows users" , async ( ) => {
12
39
render ( < UsersPage /> )
@@ -24,41 +51,42 @@ describe("Users Page", () => {
24
51
</ > ,
25
52
)
26
53
27
- // Get the first user in the table
28
- const users = await screen . findAllByText ( / .* @ c o d e r .c o m / )
29
- const firstUserRow = users [ 0 ] . closest ( "tr" )
30
- if ( ! firstUserRow ) {
31
- throw new Error ( "Error on get the first user row" )
32
- }
54
+ await suspendUser ( ( ) => {
55
+ jest . spyOn ( API , "suspendUser" ) . mockResolvedValueOnce ( MockUser )
56
+ jest . spyOn ( API , "getUsers" ) . mockImplementationOnce ( ( ) => Promise . resolve ( [ MockUser , MockUser2 ] ) )
57
+ } )
33
58
34
- // Click on the "more" button to display the "Suspend" option
35
- const moreButton = within ( firstUserRow ) . getByLabelText ( "more" )
36
- fireEvent . click ( moreButton )
37
- const menu = screen . getByRole ( "menu" )
38
- const suspendButton = within ( menu ) . getByText ( UsersTableLanguage . suspendMenuItem )
39
- fireEvent . click ( suspendButton )
59
+ // Check if the success message is displayed
60
+ await screen . findByText ( usersXServiceLanguage . suspendUserSuccess )
40
61
41
- // Check if the confirm message is displayed
42
- const confirmDialog = screen . getByRole ( "dialog" )
43
- expect ( confirmDialog ) . toHaveTextContent ( "Do you want to suspend the user TestUser?" )
62
+ // Check if the API was called correctly
63
+ expect ( API . suspendUser ) . toBeCalledTimes ( 1 )
64
+ expect ( API . suspendUser ) . toBeCalledWith ( MockUser . id )
65
+
66
+ // Check if the users list was reload
67
+ await waitFor ( ( ) => expect ( API . getUsers ) . toBeCalledTimes ( 1 ) )
68
+ } )
69
+ } )
44
70
45
- // Setup spies to check the actions after
46
- jest . spyOn ( API , "suspendUser" ) . mockResolvedValueOnce ( MockUser )
47
- jest . spyOn ( API , "getUsers" ) . mockImplementationOnce ( ( ) => Promise . resolve ( [ MockUser , MockUser2 ] ) )
71
+ describe ( "when it fails" , ( ) => {
72
+ it ( "shows an error message" , async ( ) => {
73
+ render (
74
+ < >
75
+ < UsersPage />
76
+ < GlobalSnackbar />
77
+ </ > ,
78
+ )
48
79
49
- // Click on the "Confirm" button
50
- const confirmButton = within ( confirmDialog ) . getByText ( UsersPageLanguage . suspendDialogAction )
51
- fireEvent . click ( confirmButton )
80
+ await suspendUser ( ( ) => {
81
+ jest . spyOn ( API , "suspendUser" ) . mockRejectedValueOnce ( { } )
82
+ } )
52
83
53
- // Check if the success message
54
- await screen . findByText ( usersXServiceLanguage . suspendUserSuccess )
84
+ // Check if the success message is displayed
85
+ await screen . findByText ( usersXServiceLanguage . suspendUserError )
55
86
56
87
// Check if the API was called correctly
57
88
expect ( API . suspendUser ) . toBeCalledTimes ( 1 )
58
89
expect ( API . suspendUser ) . toBeCalledWith ( MockUser . id )
59
-
60
- // Check if the users list was refetched
61
- await waitFor ( ( ) => expect ( API . getUsers ) . toBeCalledTimes ( 1 ) )
62
90
} )
63
91
} )
64
92
} )