Open History to see the last commit
1import { act, memo, useRef } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";
5
6import { THREAD_CONTEXT_DROP_EVENT, useThreadContextDrop } from "./threadContextDrag";
7
8const ENVIRONMENT_ID = "environment-local" as EnvironmentId;
9
10let root: Root;
11let container: HTMLDivElement;
12
13beforeEach(() => {
14 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
15 container = document.createElement("div");
16 document.body.append(container);
17 root = createRoot(container);
18});
19
20afterEach(async () => {
21 await act(() => root.unmount());
22 container.remove();
23 vi.unstubAllGlobals();
24});
25
26// ChatComposer is memoized and reused across thread navigation; each render's
27// drop callback must write into the draft of the thread it last rendered.
28const ComposerDropTarget = memo(function ComposerDropTarget({
29 draftKey,
30 drafts,
31}: {
32 draftKey: string;
33 drafts: Map<string, Array<ThreadId>>;
34}) {
35 const targetRef = useRef<HTMLDivElement | null>(null);
36 useThreadContextDrop(targetRef, ENVIRONMENT_ID, draftKey, (refs, isActive) => {
37 if (!isActive()) return;
38 drafts.get(draftKey)?.push(...refs.map((ref) => ref.threadId));
39 });
40 return <div data-drop-target ref={targetRef} />;
41});
42
43// Same target key, so the listener effect never re-runs: only the effect event
44// itself can hand the drop the latest props (React 19.2 kept the first render's).
45const StableKeyDropTarget = memo(function StableKeyDropTarget({
46 received,
47}: {
48 received: Array<ThreadId>;
49}) {
50 const targetRef = useRef<HTMLDivElement | null>(null);
51 useThreadContextDrop(targetRef, ENVIRONMENT_ID, "thread-a", (refs) => {
52 received.push(...refs.map((ref) => ref.threadId));
53 });
54 return <div data-drop-target ref={targetRef} />;
55});
56
57describe("useThreadContextDrop", () => {
58 it("drops into the last rendered thread's draft when the memoized target is reused", async () => {
59 const drafts = new Map<string, Array<ThreadId>>([
60 ["thread-a", []],
61 ["thread-b", []],
62 ]);
63
64 await act(() => root.render(<ComposerDropTarget draftKey="thread-a" drafts={drafts} />));
65 await act(() => root.render(<ComposerDropTarget draftKey="thread-b" drafts={drafts} />));
66
67 const dropped: ScopedThreadRef = {
68 environmentId: ENVIRONMENT_ID,
69 threadId: "thread-dropped" as ThreadId,
70 };
71 const target = container.querySelector("[data-drop-target]");
72 expect(target).not.toBeNull();
73 act(() => {
74 target!.dispatchEvent(new CustomEvent(THREAD_CONTEXT_DROP_EVENT, { detail: [dropped] }));
75 });
76
77 expect(drafts.get("thread-b")).toEqual(["thread-dropped"]);
78 expect(drafts.get("thread-a")).toEqual([]);
79 });
80
81 it("reads the latest props in a memoized target whose listener stays attached", async () => {
82 const first: Array<ThreadId> = [];
83 const latest: Array<ThreadId> = [];
84
85 await act(() => root.render(<StableKeyDropTarget received={first} />));
86 await act(() => root.render(<StableKeyDropTarget received={latest} />));
87
88 const target = container.querySelector("[data-drop-target]");
89 expect(target).not.toBeNull();
90 act(() => {
91 target!.dispatchEvent(
92 new CustomEvent(THREAD_CONTEXT_DROP_EVENT, {
93 detail: [{ environmentId: ENVIRONMENT_ID, threadId: "thread-dropped" as ThreadId }],
94 }),
95 );
96 });
97
98 expect(latest).toEqual(["thread-dropped"]);
99 expect(first).toEqual([]);
100 });
101});