1- import { render , screen , fireEvent , waitFor } from '@testing-library/vue' ;
1+ import { render , screen , waitFor } from '@testing-library/vue' ;
2+ import userEvent from '@testing-library/user-event' ;
23import VueRouter from 'vue-router' ;
34import ForgotPassword from '../resetPassword/ForgotPassword' ;
45import commonStrings from 'shared/translator' ;
6+ import { createTranslator } from 'shared/i18n' ;
7+
8+ const { fieldRequired$ } = commonStrings ;
9+ const {
10+ forgotPasswordTitle$,
11+ emailLabel$,
12+ submitButton$,
13+ emailValidationMessage$,
14+ forgotPasswordFailed$,
15+ } = createTranslator ( ForgotPassword . name , ForgotPassword . $trs ) ;
16+
17+ const INVALID_EMAIL = 'invalid-email' ;
18+ const VALID_EMAIL = 'test@test.com' ;
519
620const sendPasswordResetLinkMock = jest . fn ( ( ) => Promise . resolve ( ) ) ;
721
@@ -32,69 +46,66 @@ const renderComponent = () => {
3246} ;
3347
3448describe ( 'ForgotPassword' , ( ) => {
49+ let user ;
50+
51+ const emailInput = ( ) => screen . getByLabelText ( emailLabel$ ( ) ) ;
52+ const submitButton = ( ) => screen . getByRole ( 'button' , { name : submitButton$ ( ) } ) ;
53+
3554 beforeEach ( ( ) => {
3655 jest . clearAllMocks ( ) ;
56+ user = userEvent . setup ( ) ;
3757 } ) ;
3858
3959 it ( 'should render the forgot password form' , ( ) => {
4060 renderComponent ( ) ;
41- expect ( screen . getByText ( 'Reset your password' ) ) . toBeInTheDocument ( ) ;
42- expect ( screen . getByLabelText ( 'Email' ) ) . toBeInTheDocument ( ) ;
43- expect ( screen . getByRole ( 'button' , { name : 'Submit' } ) ) . toBeInTheDocument ( ) ;
61+ expect ( screen . getByText ( forgotPasswordTitle$ ( ) ) ) . toBeInTheDocument ( ) ;
62+ expect ( emailInput ( ) ) . toBeInTheDocument ( ) ;
63+ expect ( submitButton ( ) ) . toBeInTheDocument ( ) ;
4464 expect ( screen . queryByRole ( 'alert' ) ) . not . toBeInTheDocument ( ) ;
4565 } ) ;
4666
4767 it ( 'should not submit form with empty email' , async ( ) => {
4868 renderComponent ( ) ;
49- await fireEvent . click ( screen . getByRole ( 'button' , { name : 'Submit' } ) ) ;
69+ await user . click ( submitButton ( ) ) ;
5070 await waitFor ( ( ) => {
51- expect ( screen . getByTestId ( 'email-field' ) ) . toHaveTextContent (
52- commonStrings . $tr ( 'fieldRequired' ) ,
53- ) ;
71+ expect ( screen . getByTestId ( 'email-field' ) ) . toHaveTextContent ( fieldRequired$ ( ) ) ;
5472 } ) ;
5573 expect ( sendPasswordResetLinkMock ) . not . toHaveBeenCalled ( ) ;
5674 } ) ;
5775
5876 it ( 'should not submit form with invalid email' , async ( ) => {
5977 renderComponent ( ) ;
60- await fireEvent . update ( screen . getByLabelText ( / e m a i l / i ) , 'invalid-email' ) ;
61- await fireEvent . click ( screen . getByRole ( 'button' , { name : 'Submit' } ) ) ;
78+ await user . type ( emailInput ( ) , INVALID_EMAIL ) ;
79+ await user . click ( submitButton ( ) ) ;
6280 await waitFor ( ( ) => {
63- expect ( screen . getByTestId ( 'email-field' ) ) . toHaveTextContent (
64- ForgotPassword . $trs . emailValidationMessage ,
65- ) ;
81+ expect ( screen . getByTestId ( 'email-field' ) ) . toHaveTextContent ( emailValidationMessage$ ( ) ) ;
6682 } ) ;
6783 expect ( sendPasswordResetLinkMock ) . not . toHaveBeenCalled ( ) ;
6884 } ) ;
6985
7086 it ( 'should not show validation error while typing' , async ( ) => {
7187 renderComponent ( ) ;
72- await fireEvent . update ( screen . getByLabelText ( / e m a i l / i) , 'invalid-email' ) ;
73- expect ( screen . getByTestId ( 'email-field' ) ) . not . toHaveTextContent (
74- ForgotPassword . $trs . emailValidationMessage ,
75- ) ;
88+ await user . type ( emailInput ( ) , INVALID_EMAIL ) ;
89+ expect ( screen . getByTestId ( 'email-field' ) ) . not . toHaveTextContent ( emailValidationMessage$ ( ) ) ;
7690 } ) ;
7791
7892 it ( 'should show validation error on blur' , async ( ) => {
7993 renderComponent ( ) ;
80- const emailInput = screen . getByLabelText ( / e m a i l / i) ;
81- await fireEvent . update ( emailInput , 'invalid-email' ) ;
82- await fireEvent . blur ( emailInput ) ;
94+ await user . type ( emailInput ( ) , INVALID_EMAIL ) ;
95+ await user . tab ( ) ;
8396 await waitFor ( ( ) => {
84- expect ( screen . getByTestId ( 'email-field' ) ) . toHaveTextContent (
85- ForgotPassword . $trs . emailValidationMessage ,
86- ) ;
97+ expect ( screen . getByTestId ( 'email-field' ) ) . toHaveTextContent ( emailValidationMessage$ ( ) ) ;
8798 } ) ;
8899 } ) ;
89100
90101 it ( 'should submit form with valid email and navigate to success page' , async ( ) => {
91102 sendPasswordResetLinkMock . mockResolvedValue ( { } ) ;
92103 const { router } = renderComponent ( ) ;
93- await fireEvent . update ( screen . getByLabelText ( / e m a i l / i ) , 'test@test.com' ) ;
94- await fireEvent . click ( screen . getByRole ( 'button' , { name : 'Submit' } ) ) ;
104+ await user . type ( emailInput ( ) , VALID_EMAIL ) ;
105+ await user . click ( submitButton ( ) ) ;
95106 await waitFor ( ( ) => {
96107 expect ( sendPasswordResetLinkMock ) . toHaveBeenCalledTimes ( 1 ) ;
97- expect ( sendPasswordResetLinkMock ) . toHaveBeenCalledWith ( expect . anything ( ) , 'test@test.com' ) ;
108+ expect ( sendPasswordResetLinkMock ) . toHaveBeenCalledWith ( expect . anything ( ) , VALID_EMAIL ) ;
98109 } ) ;
99110 await waitFor ( ( ) => {
100111 expect ( router . currentRoute . name ) . toBe ( 'PasswordInstructionsSent' ) ;
@@ -104,10 +115,8 @@ describe('ForgotPassword', () => {
104115 it ( 'should show error banner when submission fails' , async ( ) => {
105116 sendPasswordResetLinkMock . mockRejectedValue ( new Error ( 'Failed' ) ) ;
106117 renderComponent ( ) ;
107- await fireEvent . update ( screen . getByLabelText ( / e m a i l / i) , 'test@test.com' ) ;
108- await fireEvent . click ( screen . getByRole ( 'button' , { name : 'Submit' } ) ) ;
109- expect ( await screen . findByRole ( 'alert' ) ) . toHaveTextContent (
110- ForgotPassword . $trs . forgotPasswordFailed ,
111- ) ;
118+ await user . type ( emailInput ( ) , VALID_EMAIL ) ;
119+ await user . click ( submitButton ( ) ) ;
120+ expect ( await screen . findByRole ( 'alert' ) ) . toHaveTextContent ( forgotPasswordFailed$ ( ) ) ;
112121 } ) ;
113122} ) ;
0 commit comments