11import React from "react" ;
2- import { render } from "@testing-library/react" ;
3- import { PasswordFormField } from "./password-form-field" ;
2+ import { render , fireEvent } from "@testing-library/react" ;
3+ import {
4+ PasswordFormField ,
5+ PasswordFormFieldInvalidClassName ,
6+ } from "./password-form-field" ;
47import faker from "faker" ;
8+ import { InputTypes } from "../../atoms/constants/input-types" ;
59
610describe ( "PasswordFormField" , ( ) => {
711 test ( "when default props, renders input with label" , ( ) => {
@@ -16,4 +20,117 @@ describe("PasswordFormField", () => {
1620 // Assert
1721 expect ( getByLabelText ( expected ) ) . not . toBeNull ( ) ;
1822 } ) ;
23+
24+ test ( "when onChange prop set, calls handler upon change" , ( ) => {
25+ // Arrange
26+ let isChecked = false ;
27+ const handleChange = ( ) => ( isChecked = true ) ;
28+ const testLabel = "testLabel" ;
29+
30+ // Act
31+ const { getByLabelText } = render (
32+ < PasswordFormField onChange = { handleChange } label = { testLabel } />
33+ ) ;
34+ fireEvent . change ( getByLabelText ( testLabel ) , {
35+ target : { value : faker . random . word ( ) } ,
36+ } ) ;
37+
38+ // Assert
39+ expect ( isChecked ) . toBeTrue ( ) ;
40+ } ) ;
41+
42+ test ( "when has errorMessage prop, renders with error message" , ( ) => {
43+ // Arrange
44+ const testErrorMessage = faker . random . words ( ) ;
45+ const testLabel = faker . random . words ( ) ;
46+
47+ // Act
48+ const { getByText } = render (
49+ < PasswordFormField
50+ errorMessage = { testErrorMessage }
51+ label = { testLabel }
52+ onChange = { ( ) => { } }
53+ />
54+ ) ;
55+
56+ // Assert
57+ expect ( getByText ( testErrorMessage ) ) . not . toBeNil ( ) ;
58+ } ) ;
59+
60+ test ( "when required prop set, renders with required text" , ( ) => {
61+ // Arrange
62+ const requiredText = "*" ;
63+ const testLabel = faker . random . words ( ) ;
64+
65+ // Act
66+ const { container } = render (
67+ < PasswordFormField
68+ label = { testLabel }
69+ onChange = { ( ) => { } }
70+ required = { true }
71+ />
72+ ) ;
73+ const htmlLabelTag = container . getElementsByTagName ( "label" ) ;
74+
75+ // Assert
76+ expect ( htmlLabelTag [ 0 ] . textContent ) . toContain ( requiredText ) ;
77+ } ) ;
78+
79+ test ( "when show button clicked, renders hide button" , ( ) => {
80+ // Arrange
81+ const testLabel = faker . random . words ( ) ;
82+
83+ // Act
84+ const { getByText } = render (
85+ < PasswordFormField
86+ label = { testLabel }
87+ onChange = { ( ) => { } }
88+ value = { faker . random . word ( ) }
89+ />
90+ ) ;
91+ const button = getByText ( "Show" ) ;
92+ fireEvent . click ( button ) ;
93+
94+ // Assert
95+ expect ( button . textContent ) . toContain ( "Hide" ) ;
96+ } ) ;
97+
98+ test ( "when disabled prop set, renders with type password" , ( ) => {
99+ // Arrange
100+ const testLabel = faker . random . words ( ) ;
101+
102+ // Act
103+ const { container, getByText } = render (
104+ < PasswordFormField
105+ disabled = { true }
106+ label = { testLabel }
107+ onChange = { ( ) => { } }
108+ value = { faker . random . word ( ) }
109+ />
110+ ) ;
111+ const htmlInputElement = container . getElementsByTagName ( "input" ) ;
112+
113+ // Assert
114+ expect ( htmlInputElement [ 0 ] . type ) . toBe ( InputTypes . Password ) ;
115+ } ) ;
116+
117+ test ( `when isValid prop is false, renders with ${ PasswordFormFieldInvalidClassName } class name` , ( ) => {
118+ // Arrange
119+ const testLabel = faker . random . words ( ) ;
120+
121+ // Act
122+ const { container } = render (
123+ < PasswordFormField
124+ isValid = { false }
125+ label = { testLabel }
126+ onChange = { ( ) => { } }
127+ />
128+ ) ;
129+ const result = container . getElementsByClassName (
130+ PasswordFormFieldInvalidClassName
131+ ) ;
132+
133+ // Assert
134+ expect ( result ) . toHaveLength ( 1 ) ;
135+ } ) ;
19136} ) ;
0 commit comments