Skip to content

Commit 4e27cc9

Browse files
author
Said Shah
committed
Added tests for input form field
1 parent 61ffc08 commit 4e27cc9

File tree

2 files changed

+129
-4
lines changed

2 files changed

+129
-4
lines changed

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

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import React from "react";
22
import { render } from "@testing-library/react";
3-
import { InputFormField } from "./input-form-field";
3+
import {
4+
InputFormField,
5+
InvalidInputFormValueClass,
6+
ShowLabelForScreenReadersOnlyClass,
7+
} from "./input-form-field";
48
import faker from "faker";
59

610
describe("InputFormField", () => {
@@ -16,4 +20,121 @@ describe("InputFormField", () => {
1620
// Assert
1721
expect(getByLabelText(expected)).not.toBeNull();
1822
});
23+
24+
test("when errorsMessages prop set, renders with error messages", () => {
25+
// Arrange
26+
const expected = faker.random.words();
27+
const firstErrorMessage = faker.random.words();
28+
const secondErrorMessage = faker.random.words();
29+
30+
// Act
31+
const { getByText } = render(
32+
<InputFormField
33+
label={expected}
34+
onChange={() => {}}
35+
errorMessages={[firstErrorMessage, secondErrorMessage]}
36+
/>
37+
);
38+
39+
// Assert
40+
expect(getByText(firstErrorMessage)).not.toBeNil();
41+
expect(getByText(secondErrorMessage)).not.toBeNil();
42+
});
43+
44+
test("when errorsMessage prop set, renders with error message", () => {
45+
// Arrange
46+
const expected = faker.random.words();
47+
const testErrorMessage = faker.random.words();
48+
49+
// Act
50+
const { getByText } = render(
51+
<InputFormField
52+
label={expected}
53+
onChange={() => {}}
54+
errorMessage={testErrorMessage}
55+
/>
56+
);
57+
58+
// Assert
59+
expect(getByText(testErrorMessage)).not.toBeNil();
60+
});
61+
62+
test(`when isValid prop set to false, renders with ${InvalidInputFormValueClass} class name`, () => {
63+
// Arrange
64+
const expected = faker.random.words();
65+
66+
// Act
67+
const { container } = render(
68+
<InputFormField
69+
label={expected}
70+
onChange={() => {}}
71+
isValid={false}
72+
/>
73+
);
74+
const result = container.getElementsByClassName(
75+
InvalidInputFormValueClass
76+
);
77+
78+
// Assert
79+
expect(result).toHaveLength(1);
80+
});
81+
82+
test(`when isValid prop set to true, renders without ${InvalidInputFormValueClass} class name`, () => {
83+
// Arrange
84+
const expected = faker.random.words();
85+
86+
// Act
87+
const { container } = render(
88+
<InputFormField
89+
label={expected}
90+
onChange={() => {}}
91+
isValid={true}
92+
/>
93+
);
94+
const result = container.getElementsByClassName(
95+
InvalidInputFormValueClass
96+
);
97+
98+
// Assert
99+
expect(result).toHaveLength(0);
100+
});
101+
102+
test("when required prop set, renders with required text", () => {
103+
// Arrange
104+
const expected = faker.random.words();
105+
const requiredText = "*";
106+
107+
// Act
108+
const { container } = render(
109+
<InputFormField
110+
label={expected}
111+
onChange={() => {}}
112+
required={true}
113+
/>
114+
);
115+
const htmlLabelTag = container.getElementsByTagName("label");
116+
117+
// Assert
118+
expect(htmlLabelTag[0].textContent).toContain(requiredText);
119+
});
120+
121+
test(`when showLabelForScreenReadersOnly prop set, renders with ${ShowLabelForScreenReadersOnlyClass} class name`, () => {
122+
// Arrange
123+
const expected = faker.random.words();
124+
125+
// Act
126+
const { container } = render(
127+
<InputFormField
128+
label={expected}
129+
onChange={() => {}}
130+
showLabelForScreenReadersOnly={true}
131+
/>
132+
);
133+
const htmlLabelTag = container.getElementsByTagName("span");
134+
135+
// Assert
136+
expect(htmlLabelTag[0].classList).toContain(
137+
ShowLabelForScreenReadersOnlyClass
138+
);
139+
});
19140
});

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { InputProperties } from "../../atoms/interfaces/input-properties";
1010
// -----------------------------------------------------------------------------------------
1111

1212
const COMPONENT_CLASS = "c-form-field";
13+
export const InvalidInputFormValueClass = "-invalid";
14+
export const ShowLabelForScreenReadersOnlyClass = "sr-only";
1315

1416
// #endregion Constants
1517

@@ -64,14 +66,16 @@ const InputFormField: React.RefForwardingComponent<
6466
value,
6567
} = props;
6668

67-
const cssIsValid = isValid ? "" : "-invalid";
69+
const cssIsValid = isValid ? "" : InvalidInputFormValueClass;
6870
const fieldId = props.fieldId ?? uuid.v4();
6971

7072
return (
7173
<div className={`${COMPONENT_CLASS} ${cssIsValid}`}>
7274
<label htmlFor={fieldId}>
7375
{showLabelForScreenReadersOnly ? (
74-
<span className="sr-only">{label}</span>
76+
<span className={ShowLabelForScreenReadersOnlyClass}>
77+
{label}
78+
</span>
7579
) : (
7680
<React.Fragment>{label}</React.Fragment>
7781
)}
@@ -82,7 +86,7 @@ const InputFormField: React.RefForwardingComponent<
8286
)}
8387
</label>
8488
<input
85-
data-test-id={inputTestId}
89+
data-testid={inputTestId}
8690
disabled={disabled}
8791
id={fieldId}
8892
placeholder={placeholder}

0 commit comments

Comments
 (0)