2022-03-23 19:22:49 +08:00
|
|
|
import { test, describe, assert, afterEach } from "vitest";
|
|
|
|
import { spy } from "tinyspy";
|
|
|
|
import { cleanup, fireEvent, render, get_text, wait } from "@gradio/tootils";
|
|
|
|
import event from "@testing-library/user-event";
|
|
|
|
|
2023-08-18 23:33:07 +08:00
|
|
|
import Textbox from "./interactive";
|
2023-08-16 02:21:41 +08:00
|
|
|
import type { LoadingStatus } from "@gradio/statustracker";
|
2022-07-22 02:12:46 +08:00
|
|
|
|
|
|
|
const loading_status = {
|
|
|
|
eta: 0,
|
|
|
|
queue_position: 1,
|
2022-08-18 02:17:56 +08:00
|
|
|
queue_size: 1,
|
2022-07-22 02:12:46 +08:00
|
|
|
status: "complete" as LoadingStatus["status"],
|
|
|
|
scroll_to_output: false,
|
|
|
|
visible: true,
|
|
|
|
fn_index: 0
|
|
|
|
};
|
2022-03-23 19:22:49 +08:00
|
|
|
|
|
|
|
describe("Textbox", () => {
|
|
|
|
afterEach(() => cleanup());
|
|
|
|
|
2023-06-29 02:37:21 +08:00
|
|
|
test("renders provided value", async () => {
|
|
|
|
const { getByDisplayValue } = await render(Textbox, {
|
2022-07-22 02:12:46 +08:00
|
|
|
show_label: true,
|
|
|
|
max_lines: 1,
|
|
|
|
loading_status,
|
2022-03-23 19:22:49 +08:00
|
|
|
lines: 1,
|
|
|
|
mode: "dynamic",
|
2022-04-06 18:47:19 +08:00
|
|
|
value: "hello world",
|
|
|
|
label: "Textbox"
|
2022-03-23 19:22:49 +08:00
|
|
|
});
|
|
|
|
|
2023-08-18 23:33:07 +08:00
|
|
|
const item: HTMLInputElement = getByDisplayValue(
|
|
|
|
"hello world"
|
|
|
|
) as HTMLInputElement;
|
2022-03-23 19:22:49 +08:00
|
|
|
assert.equal(item.value, "hello world");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("changing the text should update the value", async () => {
|
2023-06-29 02:37:21 +08:00
|
|
|
const { component, getByDisplayValue } = await render(Textbox, {
|
2022-07-22 02:12:46 +08:00
|
|
|
show_label: true,
|
|
|
|
max_lines: 10,
|
|
|
|
loading_status,
|
2022-03-23 19:22:49 +08:00
|
|
|
lines: 1,
|
|
|
|
mode: "dynamic",
|
2022-05-04 01:28:57 +08:00
|
|
|
value: "hi ",
|
2022-04-06 18:47:19 +08:00
|
|
|
label: "Textbox"
|
2022-03-23 19:22:49 +08:00
|
|
|
});
|
|
|
|
|
2023-08-18 23:33:07 +08:00
|
|
|
const item: HTMLInputElement = getByDisplayValue("hi") as HTMLInputElement;
|
2022-03-23 19:22:49 +08:00
|
|
|
|
|
|
|
const mock = spy();
|
|
|
|
component.$on("change", mock);
|
|
|
|
|
|
|
|
item.focus();
|
2023-06-22 17:26:43 +08:00
|
|
|
await event.keyboard("some text");
|
2022-03-23 19:22:49 +08:00
|
|
|
|
2022-05-04 01:28:57 +08:00
|
|
|
assert.equal(item.value, "hi some text");
|
|
|
|
assert.equal(component.value, "hi some text");
|
2023-06-22 17:26:43 +08:00
|
|
|
assert.equal(mock.callCount, 9);
|
|
|
|
assert.equal(mock.calls[8][0].detail, "hi some text");
|
2022-03-23 19:22:49 +08:00
|
|
|
});
|
|
|
|
});
|