Skip to content

Commit d7d6081

Browse files
author
Said Shah
committed
Added tests for checkbox input
1 parent 6d56e16 commit d7d6081

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed
Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
1+
import { CheckboxInput, CheckboxDisabledClass } from "./checkbox-input";
2+
import faker from "faker";
13
import React from "react";
4+
import { render, fireEvent, wait } from "@testing-library/react";
25

36
describe("CheckboxInput", () => {
4-
test.skip("TODO - https://github.com/AndcultureCode/AndcultureCode.JavaScript.React.Components/issues/6", () => {});
7+
test("when default props, renders checkbox", () => {
8+
// Arrange
9+
const expected = faker.random.words();
10+
11+
// Act
12+
const { getByText } = render(
13+
<CheckboxInput
14+
onChange={() => {}}
15+
label={expected}
16+
checked={false}
17+
/>
18+
);
19+
20+
// Assert
21+
expect(getByText(expected)).not.toBeNil();
22+
});
23+
24+
test(`when disabled prop set to true, renders with ${CheckboxDisabledClass} class`, () => {
25+
// Arrange
26+
const expected = faker.random.words();
27+
28+
// Act
29+
const { container } = render(
30+
<CheckboxInput
31+
onChange={() => {}}
32+
label={expected}
33+
checked={false}
34+
disabled={true}
35+
/>
36+
);
37+
const result = container.querySelector("." + CheckboxDisabledClass);
38+
39+
// Assert
40+
expect(result).not.toBeNil();
41+
});
42+
43+
test("when onChange set, calls handler upon change", async () => {
44+
// Arrange
45+
let isChecked = false;
46+
const handleChange = () => (isChecked = true);
47+
const label = faker.random.word();
48+
49+
// Act
50+
const { getByLabelText } = render(
51+
<CheckboxInput
52+
onChange={handleChange}
53+
label={label}
54+
checked={isChecked}
55+
/>
56+
);
57+
58+
fireEvent.click(getByLabelText(label));
59+
60+
// Assert
61+
expect(isChecked).toBeTrue();
62+
});
563
});

src/atoms/forms/checkbox-input.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import React from "react";
66
// -----------------------------------------------------------------------------------------
77

88
const ELEMENT_CLASS = "e-checkbox";
9+
export const CheckboxDisabledClass = "-disabled";
910

1011
// #endregion Constants
1112

@@ -32,12 +33,10 @@ const CheckboxInput: React.FC<CheckboxInputProperties> = (
3233
const { checked, disabled, label, onChange } = props;
3334

3435
let className = ELEMENT_CLASS;
35-
if (disabled) {
36-
className += " -disabled";
37-
}
36+
let isDisabled = disabled ? CheckboxDisabledClass : "";
3837

3938
return (
40-
<label className={className}>
39+
<label className={`${className} ${isDisabled}`}>
4140
{label}
4241
<input
4342
checked={checked}

0 commit comments

Comments
 (0)