1- import { cleanup , screen , setup , waitFor } from '@/core/test-utils' ;
1+ import { cleanup , fireEvent , render , screen , waitFor } from '@/core/test-utils' ;
22
33import type { LoginFormProps } from './login-form' ;
44import { LoginForm } from './login-form' ;
@@ -10,55 +10,58 @@ const onSubmitMock: jest.Mock<LoginFormProps['onSubmit']> = jest.fn();
1010describe ( 'LoginForm Form ' , ( ) => {
1111 const LOGIN_BUTTON = 'login-button' ;
1212 it ( 'renders correctly' , async ( ) => {
13- setup ( < LoginForm /> ) ;
13+ render ( < LoginForm /> ) ;
1414 expect ( await screen . findByText ( / S i g n i n / i) ) . toBeOnTheScreen ( ) ;
1515 } ) ;
1616
1717 it ( 'should display required error when values are empty' , async ( ) => {
18- const { user } = setup ( < LoginForm /> ) ;
18+ render ( < LoginForm /> ) ;
1919
2020 const button = screen . getByTestId ( LOGIN_BUTTON ) ;
21- expect ( screen . queryByText ( / E m a i l i s r e q u i r e d / i) ) . not . toBeOnTheScreen ( ) ;
22- await user . press ( button ) ;
23- expect ( await screen . findByText ( / E m a i l i s r e q u i r e d / i) ) . toBeOnTheScreen ( ) ;
21+ expect ( screen . queryByText ( / U s e r n a m e i s r e q u i r e d / i) ) . not . toBeOnTheScreen ( ) ;
22+ fireEvent . press ( button ) ;
23+ expect ( await screen . findByText ( / U s e r n a m e i s r e q u i r e d / i) ) . toBeOnTheScreen ( ) ;
2424 expect ( screen . getByText ( / P a s s w o r d i s r e q u i r e d / i) ) . toBeOnTheScreen ( ) ;
2525 } ) ;
2626
2727 it ( 'should display matching error when email is invalid' , async ( ) => {
28- const { user } = setup ( < LoginForm /> ) ;
28+ render ( < LoginForm /> ) ;
2929
3030 const button = screen . getByTestId ( LOGIN_BUTTON ) ;
3131 const emailInput = screen . getByTestId ( 'email-input' ) ;
32+ const usernameInput = screen . getByTestId ( 'username-input' ) ;
3233 const passwordInput = screen . getByTestId ( 'password-input' ) ;
3334
34- await user . type ( emailInput , 'yyyyy' ) ;
35- await user . type ( passwordInput , 'test' ) ;
36- await user . press ( button ) ;
35+ fireEvent . changeText ( emailInput , 'yyyy' ) ;
36+ fireEvent . changeText ( usernameInput , ' ' ) ;
37+ fireEvent . changeText ( passwordInput , 'test' ) ;
38+ fireEvent . press ( button ) ;
3739
40+ expect ( screen . queryByText ( / U s e r n a m e i s r e q u i r e d / i) ) . not . toBeOnTheScreen ( ) ;
3841 expect ( await screen . findByText ( / I n v a l i d E m a i l F o r m a t / i) ) . toBeOnTheScreen ( ) ;
39- expect ( screen . queryByText ( / E m a i l i s r e q u i r e d / i) ) . not . toBeOnTheScreen ( ) ;
4042 } ) ;
4143
4244 it ( 'Should call LoginForm with correct values when values are valid' , async ( ) => {
43- const { user } = setup ( < LoginForm onSubmit = { onSubmitMock } /> ) ;
45+ render ( < LoginForm onSubmit = { onSubmitMock } /> ) ;
4446
4547 const button = screen . getByTestId ( LOGIN_BUTTON ) ;
46- const emailInput = screen . getByTestId ( 'email -input' ) ;
48+ const emailInput = screen . getByTestId ( 'username -input' ) ;
4749 const passwordInput = screen . getByTestId ( 'password-input' ) ;
4850
49- await user . type ( emailInput , 'youssef@gmail.com ' ) ;
50- await user . type ( passwordInput , 'password' ) ;
51- await user . press ( button ) ;
51+ fireEvent . changeText ( emailInput , 'youssef' ) ;
52+ fireEvent . changeText ( passwordInput , 'password' ) ;
53+ fireEvent . press ( button ) ;
5254 await waitFor ( ( ) => {
5355 expect ( onSubmitMock ) . toHaveBeenCalledTimes ( 1 ) ;
5456 } ) ;
55- // expect.objectContaining({}) because we don't want to test the target event we are receiving from the onSubmit function
57+ // undefined because we don't use second argument of the SubmitHandler
5658 expect ( onSubmitMock ) . toHaveBeenCalledWith (
5759 {
58- 60+ email : undefined ,
61+ username : 'youssef' ,
5962 password : 'password' ,
6063 } ,
61- expect . objectContaining ( { } ) ,
64+ undefined ,
6265 ) ;
6366 } ) ;
6467} ) ;
0 commit comments