Skip to content

Commit

Permalink
feat: ensure autoFocus works on RadioButton
Browse files Browse the repository at this point in the history
  • Loading branch information
chensara committed Apr 10, 2024
1 parent 036a608 commit 890bcf7
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`RadioButton renders correctly when autoFocus 1`] = `
<label
className="radioButton"
data-testid="radio-button"
>
<span
className="inputContainer"
>
<input
autoFocus={true}
className="input"
defaultChecked={false}
disabled={false}
name=""
type="radio"
value=""
/>
<span
className="control labelAnimation"
data-testid="radio-button-control"
/>
</span>
</label>
`;

exports[`RadioButton renders correctly when checked 1`] = `
<label
className="radioButton"
Expand All @@ -9,6 +35,7 @@ exports[`RadioButton renders correctly when checked 1`] = `
className="inputContainer"
>
<input
autoFocus={false}
checked={true}
className="input"
disabled={false}
Expand All @@ -34,6 +61,7 @@ exports[`RadioButton renders correctly when default checked 1`] = `
className="inputContainer"
>
<input
autoFocus={false}
className="input"
defaultChecked={true}
disabled={false}
Expand All @@ -59,6 +87,7 @@ exports[`RadioButton renders correctly when disabled 1`] = `
className="inputContainer"
>
<input
autoFocus={false}
className="input"
defaultChecked={false}
disabled={true}
Expand All @@ -84,6 +113,7 @@ exports[`RadioButton renders correctly when unchecked 1`] = `
className="inputContainer"
>
<input
autoFocus={false}
checked={false}
className="input"
disabled={false}
Expand All @@ -109,6 +139,7 @@ exports[`RadioButton renders correctly with componentClassName 1`] = `
className="inputContainer"
>
<input
autoFocus={false}
className="input"
defaultChecked={false}
disabled={false}
Expand All @@ -134,6 +165,7 @@ exports[`RadioButton renders correctly with empty props 1`] = `
className="inputContainer"
>
<input
autoFocus={false}
className="input"
defaultChecked={false}
disabled={false}
Expand All @@ -159,6 +191,7 @@ exports[`RadioButton renders correctly with name 1`] = `
className="inputContainer"
>
<input
autoFocus={false}
className="input"
defaultChecked={false}
disabled={false}
Expand All @@ -184,6 +217,7 @@ exports[`RadioButton renders correctly with text 1`] = `
className="inputContainer"
>
<input
autoFocus={false}
className="input"
defaultChecked={false}
disabled={false}
Expand Down Expand Up @@ -214,6 +248,7 @@ exports[`RadioButton renders correctly with value 1`] = `
className="inputContainer"
>
<input
autoFocus={false}
className="input"
defaultChecked={false}
disabled={false}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from "react";
import renderer from "react-test-renderer";
import RadioButton from "../RadioButton";

Expand Down Expand Up @@ -33,6 +32,11 @@ describe("RadioButton renders correctly", () => {
expect(tree).toMatchSnapshot();
});

it("when autoFocus", () => {
const tree = renderer.create(<RadioButton autoFocus />).toJSON();
expect(tree).toMatchSnapshot();
});

it("when unchecked", () => {
const tree = renderer.create(<RadioButton checked={false} />).toJSON();
expect(tree).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,99 @@
import React from "react";
import "@testing-library/jest-dom";
import { fireEvent, render, cleanup, screen } from "@testing-library/react";
import RadioButton from "../RadioButton";

describe("RadioButton tests", () => {
const formName = "myForm";
const radiosName = "radios";

const option1Value = "1";
let onChangeMock1: jest.Mock;
let onChangeMock2: jest.Mock;
let onChangeMock3: jest.Mock;

const option1Text = "Option 1";
const option2Value = "2";
const option2Text = "Option 2";
const option3Value = "3";
const option3Text = "Option 3";

let onChangeMock1: jest.Mock;
let onChangeMock2: jest.Mock;
let onChangeMock3: jest.Mock;
describe("With one of the radio buttons is checked by default", () => {
beforeEach(() => {
const radiosName = "radios";

beforeEach(() => {
onChangeMock1 = jest.fn();
onChangeMock2 = jest.fn();
onChangeMock3 = jest.fn();
const option1Value = "1";
const option2Value = "2";
const option3Value = "3";

render(
<form name={formName}>
<RadioButton
name={radiosName}
value={option1Value}
text={option1Text}
onSelect={onChangeMock1}
defaultChecked={true}
/>
<RadioButton name={radiosName} value={option2Value} text={option2Text} onSelect={onChangeMock2} />
<RadioButton name={radiosName} value={option3Value} text={option3Text} onSelect={onChangeMock3} />
</form>
);
});
onChangeMock1 = jest.fn();
onChangeMock2 = jest.fn();
onChangeMock3 = jest.fn();

afterEach(() => {
cleanup();
});
render(
<form name={formName}>
<RadioButton
name={radiosName}
value={option1Value}
text={option1Text}
onSelect={onChangeMock1}
defaultChecked
/>
<RadioButton name={radiosName} value={option2Value} text={option2Text} onSelect={onChangeMock2} />
<RadioButton name={radiosName} value={option3Value} text={option3Text} onSelect={onChangeMock3} />
</form>
);
});

it("should default select 1st option", () => {
const option1: HTMLInputElement = screen.getByLabelText(option1Text);
const option2: HTMLInputElement = screen.getByLabelText(option2Text);
const option3: HTMLInputElement = screen.getByLabelText(option3Text);
expect(option1.checked).toBeTruthy();
expect(option2.checked).toBeFalsy();
expect(option3.checked).toBeFalsy();
});
afterEach(() => {
cleanup();
});

it("should select 2nd option", () => {
const option1: HTMLInputElement = screen.getByLabelText(option1Text);
const option2: HTMLInputElement = screen.getByLabelText(option2Text);
const option3: HTMLInputElement = screen.getByLabelText(option3Text);
fireEvent.click(option2);
expect(option1.checked).toBeFalsy();
expect(option2.checked).toBeTruthy();
expect(option3.checked).toBeFalsy();
});
it("should default select 1st option", () => {
const option1: HTMLInputElement = screen.getByLabelText(option1Text);
const option2: HTMLInputElement = screen.getByLabelText(option2Text);
const option3: HTMLInputElement = screen.getByLabelText(option3Text);
expect(option1.checked).toBeTruthy();
expect(option2.checked).toBeFalsy();
expect(option3.checked).toBeFalsy();
});

it("should call the onChange callback when clicked", () => {
const option2 = screen.getByLabelText(option2Text);
fireEvent.click(option2);
expect(onChangeMock2.mock.calls.length).toBe(1);
expect(onChangeMock2.mock.calls[0]).toBeTruthy();
it("should select 2nd option", () => {
const option1: HTMLInputElement = screen.getByLabelText(option1Text);
const option2: HTMLInputElement = screen.getByLabelText(option2Text);
const option3: HTMLInputElement = screen.getByLabelText(option3Text);
fireEvent.click(option2);
expect(option1.checked).toBeFalsy();
expect(option2.checked).toBeTruthy();
expect(option3.checked).toBeFalsy();
});

it("should call the onChange callback when clicked", () => {
const option2 = screen.getByLabelText(option2Text);
fireEvent.click(option2);
expect(onChangeMock2.mock.calls.length).toBe(1);
expect(onChangeMock2.mock.calls[0]).toBeTruthy();
});

it("should be the same text", () => {
const text = "test text";
const { getByText } = render(<RadioButton text={text} />);
const radioButtonComponentText = getByText(text);
expect(radioButtonComponentText).toBeTruthy();
});
});

it("should be the same text", () => {
const text = "test text";
const { getByText } = render(<RadioButton text={text} />);
const radioButtonComponentText = getByText(text);
expect(radioButtonComponentText).toBeTruthy();
describe("When all radio buttons are unchecked", () => {
it("should auto focus 2nd option", () => {
render(
<form name={formName}>
<RadioButton text={option1Text} />
<RadioButton text={option2Text} autoFocus />
<RadioButton text={option3Text} />
</form>
);

const option1: HTMLInputElement = screen.getByLabelText(option1Text);
const option2: HTMLInputElement = screen.getByLabelText(option2Text);
const option3: HTMLInputElement = screen.getByLabelText(option3Text);
expect(option1).not.toHaveFocus();
expect(option2).toHaveFocus();
expect(option3).not.toHaveFocus();
});
});
});

0 comments on commit 890bcf7

Please sign in to comment.