2023-06-29 02:26:34 +08:00
|
|
|
import { test, describe, assert, afterEach } from "vitest";
|
|
|
|
|
|
|
|
import { cleanup, render } from "@gradio/tootils";
|
|
|
|
import event from "@testing-library/user-event";
|
|
|
|
|
2023-08-18 23:33:07 +08:00
|
|
|
import Radio from "./interactive";
|
2023-08-16 02:21:41 +08:00
|
|
|
import type { LoadingStatus } from "@gradio/statustracker";
|
2023-06-29 02:26:34 +08:00
|
|
|
|
|
|
|
const loading_status = {
|
|
|
|
eta: 0,
|
|
|
|
queue_position: 1,
|
|
|
|
queue_size: 1,
|
|
|
|
status: "complete" as LoadingStatus["status"],
|
|
|
|
scroll_to_output: false,
|
|
|
|
visible: true,
|
|
|
|
fn_index: 0,
|
|
|
|
show_progress: "full" as LoadingStatus["show_progress"]
|
|
|
|
};
|
|
|
|
|
|
|
|
describe("Radio", () => {
|
|
|
|
afterEach(() => cleanup());
|
2023-08-17 02:27:39 +08:00
|
|
|
const choices = [
|
|
|
|
["dog", "dog"],
|
|
|
|
["cat", "cat"],
|
|
|
|
["turtle", "turtle"]
|
|
|
|
];
|
2023-06-29 02:26:34 +08:00
|
|
|
|
2023-06-29 02:37:21 +08:00
|
|
|
test("renders provided value", async () => {
|
|
|
|
const { getAllByRole, getByTestId } = await render(Radio, {
|
2023-06-29 02:26:34 +08:00
|
|
|
show_label: true,
|
|
|
|
loading_status,
|
|
|
|
choices: choices,
|
|
|
|
value: "cat",
|
|
|
|
label: "Radio",
|
|
|
|
mode: "dynamic"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
getByTestId("cat-radio-label").className.includes("selected"),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2023-08-18 23:33:07 +08:00
|
|
|
const radioButtons: HTMLOptionElement[] = getAllByRole(
|
|
|
|
"radio"
|
|
|
|
) as HTMLOptionElement[];
|
2023-06-29 02:26:34 +08:00
|
|
|
assert.equal(radioButtons.length, 3);
|
|
|
|
|
|
|
|
radioButtons.forEach((radioButton: HTMLOptionElement, index) => {
|
2023-08-17 02:27:39 +08:00
|
|
|
assert.equal(radioButton.value === choices[index][1], true);
|
2023-06-29 02:26:34 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should update the value when a radio is clicked", async () => {
|
2023-06-29 02:37:21 +08:00
|
|
|
const { getByDisplayValue, getByTestId } = await render(Radio, {
|
2023-06-29 02:26:34 +08:00
|
|
|
show_label: true,
|
|
|
|
loading_status,
|
|
|
|
choices: choices,
|
|
|
|
value: "cat",
|
|
|
|
label: "Radio",
|
|
|
|
mode: "dynamic"
|
|
|
|
});
|
|
|
|
|
|
|
|
await event.click(getByDisplayValue("dog"));
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
getByTestId("dog-radio-label").className.includes("selected"),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
getByTestId("cat-radio-label").classList.contains("selected"),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
await event.click(getByDisplayValue("turtle"));
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
getByTestId("turtle-radio-label").classList.contains("selected"),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|