Skip to content

Commit 46d0a16

Browse files
author
Said Shah
committed
Added more tests for select form field
1 parent 9b4b6fc commit 46d0a16

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/atoms/forms/select.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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}>

src/molecules/form-fields/select-form-field.test.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { render } from "@testing-library/react";
2+
import { render, fireEvent } from "@testing-library/react";
33
import {
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
});

src/molecules/form-fields/select-form-field.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export const InvalidSelectFormValueClass = "-invalid";
1919
export 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>

0 commit comments

Comments
 (0)