Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useLocalDispatchState.browser.test.tsx

41 lines1.6 KB

1import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import { expect, it } from "vite-plus/test";
4import { page } from "vite-plus/test/browser";
5​
6import { render } from "../../../test/render";
7import { useLocalDispatchState } from "./useLocalDispatchState";
8​
9const threadId = ThreadId.make("thread:shared");
10const local = EnvironmentId.make("environment:local");
11const remote = EnvironmentId.make("environment:remote");
12​
13function Harness(props: { readonly environmentId: EnvironmentId }) {
14 const dispatch = useLocalDispatchState({
15 environmentId: props.environmentId,
16 threadKey: scopedThreadKey(scopeThreadRef(props.environmentId, threadId)),
17 activeThread: undefined,
18 activeLatestRun: null,
19 latestUserMessageId: null,
20 phase: "ready",
21 activePendingApproval: null,
22 activePendingUserInput: null,
23 threadError: null,
24 });
25 return (
26 <button type="button" onClick={() => dispatch.beginLocalDispatch()}>
27 {dispatch.isSendBusy ? "Sending" : "Idle"}
28 </button>
29 );
31​
32it.each([
33 { to: local, busy: "Sending" },
34 { to: remote, busy: "Idle" },
35])("keeps a send only on its own environment's thread ($to)", async ({ to, busy }) => {
36 await using screen = await render(<Harness environmentId={local} />);
37 await page.getByRole("button", { name: "Idle" }).click();
38 await expect.element(page.getByRole("button", { name: "Sending" })).toBeVisible();
39 await screen.rerender(<Harness environmentId={to} />);
40 await expect.element(page.getByRole("button", { name: busy })).toBeVisible();
41});