import * as React from "react";
import { render, click, press } from "reakit-test-utils";
import { Radio, RadioGroup, useRadioState } from "..";
test("click on radio", () => {
const Test = () => {
const radio = useRadioState();
return (
);
};
const { getByLabelText } = render();
const radio = getByLabelText("radio") as HTMLInputElement;
expect(radio.checked).toBe(false);
click(radio);
expect(radio.checked).toBe(true);
});
test("click on non-native radio", () => {
const Test = () => {
const radio = useRadioState();
return ;
};
const { getByLabelText } = render();
const radio = getByLabelText("radio") as HTMLInputElement;
expect(radio.checked).toBe(false);
click(radio);
expect(radio.checked).toBe(true);
});
test("onChange", () => {
const Test = () => {
const { state, setState, ...radio } = useRadioState();
const [checked, setChecked] = React.useState(false);
const toggle = () => setChecked(!checked);
return (
);
};
const { getByLabelText } = render();
const radio = getByLabelText("radio") as HTMLInputElement;
expect(radio.checked).toBe(false);
click(radio);
expect(radio.checked).toBe(true);
});
test("Native radio onChange called only once when clicked", async () => {
const Test = () => {
const { state, setState, ...radio } = useRadioState();
const [checked, setChecked] = React.useState(false);
const [counter, setCounter] = React.useState(0);
const toggle = () => {
setCounter((s) => s + 1);
setChecked(!checked);
};
return (
<>
{counter}
>
);
};
const { getByLabelText, getByTestId } = render();
const radio = getByLabelText("radio") as HTMLInputElement;
click(radio);
const counter = getByTestId("counter") as HTMLElement;
expect(counter).toHaveTextContent("1");
});
test("onChange non-native radio", () => {
const Test = () => {
const { state, setState, ...radio } = useRadioState();
const [checked, setChecked] = React.useState(false);
const toggle = () => setChecked(!checked);
return (
);
};
const { getByLabelText } = render();
const radio = getByLabelText("radio") as HTMLInputElement;
expect(radio.checked).toBe(false);
click(radio);
expect(radio.checked).toBe(true);
});
[true, false].forEach((virtual) => {
describe(virtual ? "aria-activedescendant" : "roving-tabindex", () => {
test("arrow keys", () => {
const Test = () => {
const radio = useRadioState({ unstable_virtual: virtual });
return (
<>
>
);
};
const { getByLabelText } = render();
press.Tab();
expect(getByLabelText("a")).toHaveFocus();
expect(getByLabelText("a")).not.toBeChecked();
press.ArrowLeft();
expect(getByLabelText("c")).toHaveFocus();
expect(getByLabelText("c")).toBeChecked();
press.ArrowRight();
expect(getByLabelText("a")).toHaveFocus();
expect(getByLabelText("a")).toBeChecked();
press.ArrowDown();
expect(getByLabelText("b")).toHaveFocus();
expect(getByLabelText("b")).toBeChecked();
press.Tab();
press.ShiftTab();
expect(getByLabelText("b")).toHaveFocus();
expect(getByLabelText("b")).toBeChecked();
});
test("initial checked radio gets initial focus", () => {
const Test = () => {
const radio = useRadioState({ unstable_virtual: virtual, state: "b" });
return (
);
};
const { getByLabelText } = render();
press.Tab();
expect(getByLabelText("b")).toHaveFocus();
});
});
});
test("group", () => {
const Test = () => {
const radio = useRadioState({ baseId: "base" });
return (
);
};
const { container } = render();
expect(container).toMatchInlineSnapshot(`
`);
});
test("button group", () => {
const Test = () => {
const radio = useRadioState({ baseId: "base" });
return (
a
b
c
);
};
const { container } = render();
expect(container).toMatchInlineSnapshot(`
`);
});
test("empty strings can be checked", () => {
// See https://github.com/reakit/reakit/issues/607
const Test = () => {
const radio = useRadioState();
return (
);
};
const { getByLabelText } = render();
expect(getByLabelText("empty-string")).not.toBeChecked();
click(getByLabelText("empty-string"));
expect(getByLabelText("empty-string")).toBeChecked();
expect(getByLabelText("empty-string")).toHaveFocus();
});
test("falsy numbers can be checked", () => {
// See https://github.com/reakit/reakit/issues/607
const Test = () => {
const radio = useRadioState();
return (
);
};
const { getByLabelText } = render();
expect(getByLabelText("zero")).not.toBeChecked();
click(getByLabelText("zero"));
expect(getByLabelText("zero")).toBeChecked();
expect(getByLabelText("zero")).toHaveFocus();
});