@@ -2,6 +2,7 @@ import { fireEvent, screen, waitFor, within } from "@testing-library/react"
2
2
import React from "react"
3
3
import * as API from "../../api"
4
4
import { GlobalSnackbar } from "../../components/GlobalSnackbar/GlobalSnackbar"
5
+ import { Language as ResetPasswordDialogLanguage } from "../../components/ResetPasswordDialog/ResetPasswordDialog"
5
6
import { Language as UsersTableLanguage } from "../../components/UsersTable/UsersTable"
6
7
import { MockUser , MockUser2 , render } from "../../testHelpers"
7
8
import { Language as usersXServiceLanguage } from "../../xServices/users/usersXService"
@@ -34,6 +35,33 @@ const suspendUser = async (setupActionSpies: () => void) => {
34
35
fireEvent . click ( confirmButton )
35
36
}
36
37
38
+ const resetUserPassword = async ( setupActionSpies : ( ) => void ) => {
39
+ // Get the first user in the table
40
+ const users = await screen . findAllByText ( / .* @ c o d e r .c o m / )
41
+ const firstUserRow = users [ 0 ] . closest ( "tr" )
42
+ if ( ! firstUserRow ) {
43
+ throw new Error ( "Error on get the first user row" )
44
+ }
45
+
46
+ // Click on the "more" button to display the "Suspend" option
47
+ const moreButton = within ( firstUserRow ) . getByLabelText ( "more" )
48
+ fireEvent . click ( moreButton )
49
+ const menu = screen . getByRole ( "menu" )
50
+ const resetPasswordButton = within ( menu ) . getByText ( UsersTableLanguage . resetPasswordMenuItem )
51
+ fireEvent . click ( resetPasswordButton )
52
+
53
+ // Check if the confirm message is displayed
54
+ const confirmDialog = screen . getByRole ( "dialog" )
55
+ expect ( confirmDialog ) . toHaveTextContent ( `You will need to send ${ MockUser . username } the following password:` )
56
+
57
+ // Setup spies to check the actions after
58
+ setupActionSpies ( )
59
+
60
+ // Click on the "Confirm" button
61
+ const confirmButton = within ( confirmDialog ) . getByRole ( "button" , { name : ResetPasswordDialogLanguage . confirmText } )
62
+ fireEvent . click ( confirmButton )
63
+ }
64
+
37
65
describe ( "Users Page" , ( ) => {
38
66
it ( "shows users" , async ( ) => {
39
67
render ( < UsersPage /> )
@@ -81,7 +109,7 @@ describe("Users Page", () => {
81
109
jest . spyOn ( API , "suspendUser" ) . mockRejectedValueOnce ( { } )
82
110
} )
83
111
84
- // Check if the success message is displayed
112
+ // Check if the error message is displayed
85
113
await screen . findByText ( usersXServiceLanguage . suspendUserError )
86
114
87
115
// Check if the API was called correctly
@@ -90,4 +118,50 @@ describe("Users Page", () => {
90
118
} )
91
119
} )
92
120
} )
121
+
122
+ describe ( "reset user password" , ( ) => {
123
+ describe ( "when it is success" , ( ) => {
124
+ it ( "shows a success message" , async ( ) => {
125
+ render (
126
+ < >
127
+ < UsersPage />
128
+ < GlobalSnackbar />
129
+ </ > ,
130
+ )
131
+
132
+ await resetUserPassword ( ( ) => {
133
+ jest . spyOn ( API , "updateUserPassword" ) . mockResolvedValueOnce ( undefined )
134
+ } )
135
+
136
+ // Check if the success message is displayed
137
+ await screen . findByText ( usersXServiceLanguage . resetUserPasswordSuccess )
138
+
139
+ // Check if the API was called correctly
140
+ expect ( API . updateUserPassword ) . toBeCalledTimes ( 1 )
141
+ expect ( API . updateUserPassword ) . toBeCalledWith ( expect . any ( String ) , MockUser . id )
142
+ } )
143
+ } )
144
+
145
+ describe ( "when it fails" , ( ) => {
146
+ it ( "shows an error message" , async ( ) => {
147
+ render (
148
+ < >
149
+ < UsersPage />
150
+ < GlobalSnackbar />
151
+ </ > ,
152
+ )
153
+
154
+ await resetUserPassword ( ( ) => {
155
+ jest . spyOn ( API , "updateUserPassword" ) . mockRejectedValueOnce ( { } )
156
+ } )
157
+
158
+ // Check if the error message is displayed
159
+ await screen . findByText ( usersXServiceLanguage . resetUserPasswordError )
160
+
161
+ // Check if the API was called correctly
162
+ expect ( API . updateUserPassword ) . toBeCalledTimes ( 1 )
163
+ expect ( API . updateUserPassword ) . toBeCalledWith ( expect . any ( String ) , MockUser . id )
164
+ } )
165
+ } )
166
+ } )
93
167
} )
0 commit comments