File size: 1,979 Bytes
c8cad77 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // @vitest-environment jsdom
import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { expect, test } from "vitest";
import EpisodeTaskDialog from "./EpisodeTaskDialog";
test("saves the task for the browsed episode or for every episode", async () => {
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
const host = document.createElement("div");
document.body.append(host);
const root = createRoot(host);
const saved: [string, number | null][] = [];
try {
await act(async () => {
root.render(
<EpisodeTaskDialog
repoId="local/demo"
episodeIndex={3}
currentTask="pick the cube"
totalEpisodes={5}
open
onOpenChange={() => {}}
onSave={(task, episodeIndex) => {
saved.push([task, episodeIndex]);
}}
/>,
);
});
const input = document.getElementById("episode-task") as HTMLInputElement;
const saveButton = () =>
[...document.body.querySelectorAll("button")].find(
(b) => b.textContent === "Save task",
) as HTMLButtonElement;
const type = async (value: string) => {
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
await act(async () => {
setter.call(input, value);
input.dispatchEvent(new Event("input", { bubbles: true }));
});
};
expect(input.value).toBe("pick the cube");
await type(" ");
expect(saveButton().disabled).toBe(true);
await type(" pick the bowl ");
await act(async () => saveButton().click());
expect(saved).toEqual([["pick the bowl", 3]]);
await act(async () => (document.getElementById("episode-task-all") as HTMLButtonElement).click());
await act(async () => saveButton().click());
expect(saved.at(-1)).toEqual(["pick the bowl", null]);
} finally {
await act(async () => root.unmount());
host.remove();
}
});
|