File tree Expand file tree Collapse file tree 3 files changed +30
-5
lines changed
Expand file tree Collapse file tree 3 files changed +30
-5
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,7 @@ const Select: React.FC<SelectProps> = (props: SelectProps) => {
5050 return (
5151 < div className = { classNames . join ( " " ) } >
5252 < select
53+ id = { props . id }
5354 onChange = { handleChange }
5455 value = { props . value }
5556 name = { props . name } >
Original file line number Diff line number Diff line change 11import React from "react" ;
2- import { render } from "@testing-library/react" ;
2+ import { render , fireEvent } from "@testing-library/react" ;
33import {
44 SelectFormField ,
55 InvalidSelectFormValueClass ,
@@ -142,4 +142,30 @@ describe("SelectFormField", () => {
142142 // Assert
143143 expect ( htmlLabelTag [ 0 ] . textContent ) . toContain ( requiredText ) ;
144144 } ) ;
145+
146+ test ( "when onChange set, calls handler upon change" , ( ) => {
147+ // Arrange
148+ const label = faker . random . word ( ) ;
149+ let isChecked = false ;
150+ const handleChange = ( ) => ( isChecked = true ) ;
151+ const selectLabel = faker . random . word ( ) ;
152+ const selectValue = faker . random . word ( ) ;
153+
154+ // Act
155+ const { getByLabelText, container } = render (
156+ < SelectFormField
157+ onChange = { handleChange }
158+ label = { label }
159+ id = { label }
160+ values = { [ { value : selectValue , label : selectLabel } ] }
161+ />
162+ ) ;
163+
164+ fireEvent . change ( getByLabelText ( label ) , {
165+ target : { value : faker . random . word ( ) } ,
166+ } ) ;
167+
168+ // Assert
169+ expect ( isChecked ) . toBeTrue ( ) ;
170+ } ) ;
145171} ) ;
Original file line number Diff line number Diff line change @@ -19,7 +19,6 @@ export const InvalidSelectFormValueClass = "-invalid";
1919export interface SelectFormFieldProps {
2020 errorMessage ?: string ;
2121 errorMessages ?: string [ ] ;
22- fieldId ?: string ;
2322 id : string ;
2423 isValid ?: boolean ;
2524 name ?: string ;
@@ -41,7 +40,6 @@ const SelectFormField: React.FC<SelectFormFieldProps> = (
4140 const {
4241 errorMessage,
4342 errorMessages,
44- id,
4543 isValid,
4644 name,
4745 label,
@@ -51,13 +49,13 @@ const SelectFormField: React.FC<SelectFormFieldProps> = (
5149 } = props ;
5250
5351 const cssIsValid = isValid ? "" : InvalidSelectFormValueClass ;
54- const fieldId = props . fieldId ?? uuid . v4 ( ) ;
52+ const id = props . id ?? uuid . v4 ( ) ;
5553 const hasErrorMessage = StringUtils . hasValue ( errorMessage ) ;
5654 const hasErrors = CollectionUtils . hasValues ( errorMessages ) ;
5755
5856 return (
5957 < div className = { `${ COMPONENT_CLASS } ${ cssIsValid } ` } >
60- < label htmlFor = { fieldId } >
58+ < label htmlFor = { id } >
6159 { label }
6260 { required ? "*" : "" }
6361 </ label >
You can’t perform that action at this time.
0 commit comments