11import React from "react" ;
2- import { render } from "@testing-library/react" ;
2+ import { render , fireEvent , wait , getByText } from "@testing-library/react" ;
33import { Form } from "./form" ;
44import faker from "faker" ;
55
@@ -16,4 +16,40 @@ describe("Form", () => {
1616 // Assert
1717 expect ( getByText ( expected ) ) . not . toBeNull ( ) ;
1818 } ) ;
19+
20+ test ( "when onSubmit set, calls handler upon submit" , async ( ) => {
21+ // Arrange
22+ let calledTimes = 0 ;
23+ const buttonText = faker . random . word ( ) ;
24+ const handleClick = ( ) => {
25+ calledTimes ++ ;
26+ } ;
27+
28+ // Act
29+ const { container } = render (
30+ < Form onSubmit = { handleClick } >
31+ < button > { buttonText } </ button >
32+ </ Form >
33+ ) ;
34+ fireEvent . submit ( getByText ( container , buttonText ) ) ;
35+
36+ // Assert
37+ await wait ( ( ) => {
38+ expect ( calledTimes ) . toEqual ( 1 ) ;
39+ } ) ;
40+ } ) ;
41+
42+ test ( "when given prop cssClassName, renders with class name" , ( ) => {
43+ // Arrange
44+ const testClassName = "testClassName" ;
45+
46+ // Act
47+ const { container } = render (
48+ < Form onSubmit = { ( ) => { } } cssClassName = { testClassName } />
49+ ) ;
50+ const result = container . querySelector ( "." + testClassName ) ;
51+
52+ // Assert
53+ expect ( result ) . not . toBeNil ( ) ;
54+ } ) ;
1955} ) ;
0 commit comments