11import React from 'react' ;
22
3- import { cleanup , fireEvent , render , screen , waitFor } from '@/core/test-utils' ;
3+ import { cleanup , screen , setup , waitFor } from '@/core/test-utils' ;
44
55import type { LoginFormProps } from './login-form' ;
66import { LoginForm } from './login-form' ;
@@ -11,55 +11,55 @@ const onSubmitMock: jest.Mock<LoginFormProps['onSubmit']> = jest.fn();
1111
1212describe ( 'LoginForm Form ' , ( ) => {
1313 it ( 'renders correctly' , async ( ) => {
14- render ( < LoginForm /> ) ;
14+ setup ( < LoginForm /> ) ;
1515 expect ( await screen . findByText ( / S i g n i n / i) ) . toBeOnTheScreen ( ) ;
1616 } ) ;
1717
1818 it ( 'should display required error when values are empty' , async ( ) => {
19- render ( < LoginForm /> ) ;
19+ const { user } = setup ( < LoginForm /> ) ;
2020
2121 const button = screen . getByTestId ( 'login-button' ) ;
2222 expect ( screen . queryByText ( / E m a i l i s r e q u i r e d / i) ) . not . toBeOnTheScreen ( ) ;
23- fireEvent . press ( button ) ;
23+ await user . press ( button ) ;
2424 expect ( await screen . findByText ( / E m a i l i s r e q u i r e d / i) ) . toBeOnTheScreen ( ) ;
2525 expect ( screen . getByText ( / P a s s w o r d i s r e q u i r e d / i) ) . toBeOnTheScreen ( ) ;
2626 } ) ;
2727
2828 it ( 'should display matching error when email is invalid' , async ( ) => {
29- render ( < LoginForm /> ) ;
29+ const { user } = setup ( < LoginForm /> ) ;
3030
3131 const button = screen . getByTestId ( 'login-button' ) ;
3232 const emailInput = screen . getByTestId ( 'email-input' ) ;
3333 const passwordInput = screen . getByTestId ( 'password-input' ) ;
3434
35- fireEvent . changeText ( emailInput , 'yyyyy' ) ;
36- fireEvent . changeText ( passwordInput , 'test' ) ;
37- fireEvent . press ( button ) ;
35+ await user . type ( emailInput , 'yyyyy' ) ;
36+ await user . type ( passwordInput , 'test' ) ;
37+ await user . press ( button ) ;
3838
39- expect ( screen . queryByText ( / E m a i l i s r e q u i r e d / i) ) . not . toBeOnTheScreen ( ) ;
4039 expect ( await screen . findByText ( / I n v a l i d E m a i l F o r m a t / i) ) . toBeOnTheScreen ( ) ;
40+ expect ( screen . queryByText ( / E m a i l i s r e q u i r e d / i) ) . not . toBeOnTheScreen ( ) ;
4141 } ) ;
4242
4343 it ( 'Should call LoginForm with correct values when values are valid' , async ( ) => {
44- render ( < LoginForm onSubmit = { onSubmitMock } /> ) ;
44+ const { user } = setup ( < LoginForm onSubmit = { onSubmitMock } /> ) ;
4545
4646 const button = screen . getByTestId ( 'login-button' ) ;
4747 const emailInput = screen . getByTestId ( 'email-input' ) ;
4848 const passwordInput = screen . getByTestId ( 'password-input' ) ;
4949
50- fireEvent . changeText ( emailInput , '[email protected] ' ) ; 51- fireEvent . changeText ( passwordInput , 'password' ) ;
52- fireEvent . press ( button ) ;
50+ await user . type ( emailInput , '[email protected] ' ) ; 51+ await user . type ( passwordInput , 'password' ) ;
52+ await user . press ( button ) ;
5353 await waitFor ( ( ) => {
5454 expect ( onSubmitMock ) . toHaveBeenCalledTimes ( 1 ) ;
5555 } ) ;
56- // undefined because we don't use second argument of the SubmitHandler
56+ // expect.objectContaining({}) because we don't want to test the target event we are receiving from the onSubmit function
5757 expect ( onSubmitMock ) . toHaveBeenCalledWith (
5858 {
59596060 password : 'password' ,
6161 } ,
62- undefined
62+ expect . objectContaining ( { } ) ,
6363 ) ;
6464 } ) ;
6565} ) ;
0 commit comments