Skip to content

Commit 562da60

Browse files
author
Said Shah
committed
Added tests for select form field
1 parent 6d56e16 commit 562da60

File tree

2 files changed

+144
-7
lines changed

2 files changed

+144
-7
lines changed
Lines changed: 142 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,145 @@
1-
// import React from "react";
2-
// import { render } from "@testing-library/react";
3-
// import { SelectFormField } from "./select-form-field";
4-
// import faker from "faker";
5-
// import uuid from "uuid";
1+
import React from "react";
2+
import { render } from "@testing-library/react";
3+
import {
4+
SelectFormField,
5+
InvalidSelectFormValueClass,
6+
} from "./select-form-field";
7+
import faker from "faker";
8+
import uuid from "uuid";
69

710
describe("SelectFormField", () => {
8-
test.skip("TODO - https://github.com/AndcultureCode/AndcultureCode.JavaScript.React.Components/issues/26", () => {});
11+
test("when default props, renders SelectFormField with label", () => {
12+
// Arrange
13+
const label = faker.random.words();
14+
const selectLabel = faker.random.word();
15+
const selectValue = faker.random.word();
16+
17+
// Act
18+
const { getByText } = render(
19+
<SelectFormField
20+
onChange={() => {}}
21+
label={label}
22+
id={uuid()}
23+
values={[{ value: selectValue, label: selectLabel }]}
24+
/>
25+
);
26+
27+
// Assert
28+
expect(getByText(label)).not.toBeNil();
29+
});
30+
31+
test("when has errorsMessages prop, renders select form field with error message", () => {
32+
// Arrange
33+
const expected = faker.random.words();
34+
const testErrorMessage = faker.random.words();
35+
const selectLabel = faker.random.word();
36+
const selectValue = faker.random.word();
37+
38+
// Act
39+
const { getByText } = render(
40+
<SelectFormField
41+
label={expected}
42+
onChange={() => {}}
43+
id={uuid()}
44+
errorMessages={[testErrorMessage]}
45+
values={[{ value: selectValue, label: selectLabel }]}
46+
/>
47+
);
48+
49+
// Assert
50+
expect(getByText(testErrorMessage)).not.toBeNil();
51+
});
52+
53+
test("when has errorsMessage prop, renders select form field with error message", () => {
54+
// Arrange
55+
const expected = faker.random.words();
56+
const testErrorMessage = faker.random.words();
57+
const selectLabel = faker.random.word();
58+
const selectValue = faker.random.word();
59+
60+
// Act
61+
const { getByText } = render(
62+
<SelectFormField
63+
label={expected}
64+
onChange={() => {}}
65+
id={uuid()}
66+
errorMessage={testErrorMessage}
67+
values={[{ value: selectValue, label: selectLabel }]}
68+
/>
69+
);
70+
71+
// Assert
72+
expect(getByText(testErrorMessage)).not.toBeNil();
73+
});
74+
75+
test(`when isValid prop is false, renders with ${InvalidSelectFormValueClass} class name`, () => {
76+
// Arrange
77+
const label = faker.random.words();
78+
const selectLabel = faker.random.word();
79+
const selectValue = faker.random.word();
80+
81+
// Act
82+
const { container } = render(
83+
<SelectFormField
84+
onChange={() => {}}
85+
label={label}
86+
isValid={false}
87+
id={uuid()}
88+
values={[{ value: selectValue, label: selectLabel }]}
89+
/>
90+
);
91+
const result = container.querySelector(
92+
"." + InvalidSelectFormValueClass
93+
);
94+
95+
// Assert
96+
expect(result).not.toBeNil();
97+
});
98+
99+
test(`when isValid prop is true, renders without ${InvalidSelectFormValueClass} class name`, () => {
100+
// Arrange
101+
const label = faker.random.words();
102+
const selectLabel = faker.random.word();
103+
const selectValue = faker.random.word();
104+
105+
// Act
106+
const { container } = render(
107+
<SelectFormField
108+
onChange={() => {}}
109+
label={label}
110+
isValid={true}
111+
id={uuid()}
112+
values={[{ value: selectValue, label: selectLabel }]}
113+
/>
114+
);
115+
const result = container.querySelector(
116+
"." + InvalidSelectFormValueClass
117+
);
118+
119+
// Assert
120+
expect(result).toBeNil();
121+
});
122+
123+
test("when required prop set, renders with required text", () => {
124+
// Arrange
125+
const label = faker.random.words();
126+
const requiredText = "*";
127+
const selectLabel = faker.random.word();
128+
const selectValue = faker.random.word();
129+
130+
// Act
131+
const { container } = render(
132+
<SelectFormField
133+
onChange={() => {}}
134+
label={label}
135+
required={true}
136+
id={uuid()}
137+
values={[{ value: selectValue, label: selectLabel }]}
138+
/>
139+
);
140+
const htmlLabelTag = container.getElementsByTagName("label");
141+
142+
// Assert
143+
expect(htmlLabelTag[0].textContent).toContain(requiredText);
144+
});
9145
});

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

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

1010
const COMPONENT_CLASS = "c-form-field";
11+
export const InvalidSelectFormValueClass = "-invalid";
1112

1213
// #endregion Component
1314

@@ -49,7 +50,7 @@ const SelectFormField: React.FC<SelectFormFieldProps> = (
4950
values,
5051
} = props;
5152

52-
const cssIsValid = isValid ? "" : "-invalid";
53+
const cssIsValid = isValid ? "" : InvalidSelectFormValueClass;
5354
const fieldId = props.fieldId ?? uuid.v4();
5455
const hasErrorMessage = StringUtils.hasValue(errorMessage);
5556
const hasErrors = CollectionUtils.hasValues(errorMessages);

0 commit comments

Comments
 (0)