Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removing material from checkbox #1255

Merged
merged 27 commits into from Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
166521a
removing material from checkbox
Jialecl Sep 1, 2022
2a51d4a
fixing token variable name
Jialecl Sep 1, 2022
41f0791
Merge branch 'master' of https://github.com/dxc-technology/halstack-r…
Jialecl Sep 7, 2022
207a719
Merge branch 'master' of https://github.com/dxc-technology/halstack-r…
Jialecl Sep 9, 2022
77b96b7
Merge branch 'master' of https://github.com/dxc-technology/halstack-r…
Jialecl Sep 12, 2022
8fb1b1e
Adding changes based on review and new interaction tests
Jialecl Sep 12, 2022
2abba41
removing aria-labelledby
Jialecl Sep 12, 2022
bb02654
Fixing click in empty space
Jialecl Sep 12, 2022
e577b32
Adding review changes in Checkbox.tsx
Jialecl Sep 12, 2022
554515f
Checkbox redesign for simpler HTML structure
GomezIvann Sep 16, 2022
f695a3f
Merge branch 'master' of https://github.com/dxc-technology/halstack-r…
Jialecl Sep 20, 2022
6ecf6cc
removed uuid and isLabelHovered state + style fixes
Jialecl Sep 20, 2022
500b820
Adding double click prevent default to labelContainer
Jialecl Sep 20, 2022
3a6277c
Merge branch 'master' of https://github.com/dxc-technology/halstack-r…
Jialecl Sep 21, 2022
c1facef
removing dblClick and improving user interaction
Jialecl Sep 21, 2022
403730e
adding space to optional when labelPosition is after
Jialecl Sep 21, 2022
3e2e9d6
Merge branch 'master' into jialecl-checkbox-withoutMaterial
Jialecl Sep 22, 2022
8d7c688
Merge branch 'master' of https://github.com/dxc-technology/halstack-r…
Jialecl Sep 22, 2022
3c506b0
Merge branch 'jialecl-checkbox-withoutMaterial' of https://github.com…
Jialecl Sep 22, 2022
08e32a3
adding aditional accessibility tests and exception check
Jialecl Sep 22, 2022
bca8c19
reverting useId to uuidv4
Jialecl Sep 22, 2022
85fca3f
removing useId import
Jialecl Sep 22, 2022
537ff2f
Merge branch 'master' into jialecl-checkbox-withoutMaterial
GomezIvann Sep 22, 2022
751b381
Changing the focus to the MainContainer
Jialecl Sep 22, 2022
0f997fa
Merge branch 'jialecl-checkbox-withoutMaterial' of https://github.com…
Jialecl Sep 22, 2022
31bcb52
removing getByTestId import
Jialecl Sep 22, 2022
24edc7d
Checkbox focus behaviour update
GomezIvann Sep 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 56 additions & 15 deletions lib/src/checkbox/Checkbox.test.js
@@ -1,13 +1,18 @@
import React from "react";
import { render, fireEvent } from "@testing-library/react";

import userEvent from "@testing-library/user-event";
import DxcCheckbox from "./Checkbox";

describe("Checkbox component tests", () => {
test("Checkbox renders with correct text", () => {
const { getByText } = render(<DxcCheckbox label="Checkbox" />);

expect(getByText("Checkbox")).toBeTruthy();
test("Checkbox renders with correct aria-labelledby and aria-required", () => {
const { getByText, getByRole } = render(<DxcCheckbox label="Checkbox" />);
const labelId = getByText("Checkbox").getAttribute("id");
expect(getByRole("checkbox").getAttribute("aria-labelledby")).toBe(labelId);
expect(getByRole("checkbox").getAttribute("aria-required")).toBe("true");
});
test("Optional checkbox renders with correct aria-required", () => {
const { getByRole } = render(<DxcCheckbox label="Checkbox" optional />);
expect(getByRole("checkbox").getAttribute("aria-required")).toBe("false");
});
test("Calls correct function on click", () => {
const onChange = jest.fn();
Expand All @@ -18,34 +23,70 @@ describe("Checkbox component tests", () => {
});
test("Uncontrolled checkbox", () => {
const onChange = jest.fn();
const component = render(<DxcCheckbox label="Checkbox" onChange={onChange} />);
const component = render(<DxcCheckbox label="Checkbox" onChange={onChange} name="test" />);
const visibleCheckbox = component.getByText("Checkbox");
const input = component.getByRole("checkbox");
expect(input.checked).toBe(false);
const submitInput = component.container.querySelector(`input[name="test"]`);
expect(input.getAttribute("aria-checked")).toBe("false");
expect(submitInput.checked).toBe(false);
fireEvent.click(visibleCheckbox);
expect(onChange).toHaveBeenCalled();
expect(onChange).toHaveBeenCalledWith(true);
expect(input.checked).toBe(true);
expect(input.getAttribute("aria-checked")).toBe("true");
expect(submitInput.checked).toBe(true);
});
test("Controlled checkbox", () => {
const onChange = jest.fn();
const component = render(<DxcCheckbox label="Checkbox" checked={false} onChange={onChange} />);
const component = render(<DxcCheckbox label="Checkbox" checked={false} onChange={onChange} name="test" />);

const input = component.getByRole("checkbox");
const visibleCheckbox = component.getByText("Checkbox");
const submitInput = component.container.querySelector(`input[name="test"]`);

fireEvent.click(visibleCheckbox);
expect(onChange).toHaveBeenCalled();
expect(onChange).toHaveBeenCalledWith(true);
expect(input.checked).toBe(false);
expect(input.getAttribute("aria-checked")).toBe("false");
expect(submitInput.checked).toBe(false);
});
test("Renders with correct initial value and initial state when it is uncontrolled", () => {
const { getByRole } = render(
<DxcCheckbox label="Default label" defaultChecked value="test-defaultChecked" />
const { getByRole, container } = render(
<DxcCheckbox label="Default label" defaultChecked value="test-defaultChecked" name="test" />
);
const checkbox = getByRole("checkbox");
const submitInput = container.querySelector(`input[name="test"]`);
expect(submitInput.value).toBe("test-defaultChecked");
expect(checkbox.getAttribute("aria-checked")).toBe("true");
expect(submitInput.checked).toBe(true);
});
test("Test disable keyboard and mouse interactions", () => {
const onChange = jest.fn();
const { getByRole, getByText, container } = render(
<DxcCheckbox label="Checkbox" onChange={onChange} disabled name="test" />
);

const input = getByRole("checkbox");
expect(input.checked).toBe(true);
expect(input.value).toBe("test-defaultChecked");
expect(input.getAttribute("aria-checked")).toBe("true");
const visibleCheckbox = getByText("Checkbox");
const submitInput = container.querySelector(`input[name="test"]`);

fireEvent.click(visibleCheckbox);
expect(onChange).toHaveBeenCalledTimes(0);
expect(input.getAttribute("aria-checked")).toBe("false");
expect(input.getAttribute("aria-disabled")).toBe("true");
expect(submitInput.checked).toBe(false);
userEvent.tab();
expect(document.activeElement === input).toBeFalsy();
});

test("Test keyboard interactions", () => {
const onChange = jest.fn();
const { getByRole } = render(<DxcCheckbox label="Checkbox" name="test" onChange={onChange} />);

const checkbox = getByRole("checkbox");

userEvent.tab();
expect(document.activeElement === checkbox).toBeTruthy();
fireEvent.keyDown(checkbox, { key: " ", code: "Space", keyCode: 32, charCode: 32 });
expect(onChange).toHaveBeenCalledWith(true);
});
});