11import React from "react" ;
2- import { render } from "@testing-library/react" ;
3- import { RadioButton } from "./radio-button-input" ;
2+ import { render , fireEvent , wait } from "@testing-library/react" ;
3+ import {
4+ RadioButton ,
5+ RadioButtonSelectedClassName ,
6+ } from "./radio-button-input" ;
47import faker from "faker" ;
58import uuid from "uuid" ;
69
@@ -22,4 +25,50 @@ describe("RadioButton", () => {
2225 // Assert
2326 expect ( getByLabelText ( expected ) ) . not . toBeNull ( ) ;
2427 } ) ;
28+
29+ test ( `when checked prop is true, renders check ${ RadioButtonSelectedClassName } class name` , ( ) => {
30+ // Arrange
31+ const expected = faker . random . word ( ) ;
32+
33+ // Act
34+ const { container } = render (
35+ < RadioButton
36+ checked = { true }
37+ id = { uuid ( ) }
38+ label = { expected }
39+ name = { faker . random . word ( ) }
40+ />
41+ ) ;
42+ const result = container . querySelector (
43+ "." + RadioButtonSelectedClassName
44+ ) ;
45+
46+ // Assert
47+ expect ( result ) . not . toBeNull ( ) ;
48+ } ) ;
49+
50+ test ( "when onClick set, calls handler upon click" , async ( ) => {
51+ // Arrange
52+ let calledTimes = 0 ;
53+ const handleClick = ( ) => calledTimes ++ ;
54+ const buttonText = "test button" ;
55+
56+ // Act
57+ const { getByText } = render (
58+ < RadioButton
59+ onClick = { handleClick }
60+ checked = { false }
61+ id = { uuid ( ) }
62+ name = { faker . random . word ( ) }
63+ label = { buttonText }
64+ />
65+ ) ;
66+
67+ fireEvent . click ( getByText ( buttonText ) ) ;
68+
69+ // Assert
70+ await wait ( ( ) => {
71+ expect ( calledTimes ) . toEqual ( 1 ) ;
72+ } ) ;
73+ } ) ;
2574} ) ;
0 commit comments