Skip to content

Commit ea8c8b2

Browse files
Issue #25 Backfill tests for Molecules/PasswordInputField
Add tests for molecules password form field
2 parents 0fc5615 + 4d3fddd commit ea8c8b2

File tree

2 files changed

+121
-3
lines changed

2 files changed

+121
-3
lines changed

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

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import 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";
47
import faker from "faker";
8+
import { InputTypes } from "../../atoms/constants/input-types";
59

610
describe("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
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { StringUtils } from "andculturecode-javascript-core";
99
// -----------------------------------------------------------------------------------------
1010

1111
const COMPONENT_CLASS = "c-form-field";
12+
export const PasswordFormFieldInvalidClassName = "-invalid";
1213

1314
// #endregion Constants
1415

@@ -50,7 +51,7 @@ const PasswordFormField: React.FC<PasswordFormFields> = (
5051
} = props;
5152

5253
const [isVisible, setIsVisible] = useState<boolean>(false);
53-
const cssIsValid = isValid ? "" : "-invalid";
54+
const cssIsValid = isValid ? "" : PasswordFormFieldInvalidClassName;
5455
const disableShowHide = StringUtils.isEmpty(value) || disabled;
5556
const fieldId = uuid.v4();
5657
const passwordShowHideLabel = isVisible ? "Hide" : "Show";

0 commit comments

Comments
 (0)