Skip to content

Commit fe9ba63

Browse files
committed
Add test to confirm useCopilotKit can be called from a
ReactActivityMessageRenderer
1 parent 8309772 commit fe9ba63

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

packages/react/src/components/chat/__tests__/CopilotChatActivityRendering.e2e.test.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
testId,
1111
} from "@/__tests__/utils/test-helpers";
1212
import { ReactActivityMessageRenderer } from "@/types";
13+
import { useCopilotKit } from "@/providers";
1314

1415
describe("CopilotChat activity message rendering", () => {
1516
it("renders custom components for activity snapshots", async () => {
@@ -90,4 +91,56 @@ describe("CopilotChat activity message rendering", () => {
9091
expect(screen.queryByTestId("activity-card")).toBeNull();
9192
});
9293
});
94+
95+
it("useCopilotKit provides valid copilotkit instance inside activity message renderer", async () => {
96+
const agent = new MockStepwiseAgent();
97+
const agentId = "test-agent";
98+
agent.agentId = agentId;
99+
100+
let capturedCopilotkit: any = "not-called";
101+
102+
// Matches real-world pattern: inline arrow function with hooks
103+
const activityRenderer: ReactActivityMessageRenderer<{ message: string }> = {
104+
activityType: "test-activity",
105+
content: z.object({ message: z.string() }),
106+
render: ({ content }) => {
107+
const { copilotkit } = useCopilotKit();
108+
capturedCopilotkit = copilotkit;
109+
return <div data-testid="activity-render">{content.message}</div>;
110+
},
111+
};
112+
113+
renderWithCopilotKit({
114+
agents: { [agentId]: agent },
115+
agentId,
116+
renderActivityMessages: [activityRenderer],
117+
});
118+
119+
// Trigger user message and activity event
120+
const input = await screen.findByRole("textbox");
121+
fireEvent.change(input, { target: { value: "Test message" } });
122+
fireEvent.keyDown(input, { key: "Enter", code: "Enter" });
123+
124+
await waitFor(() => {
125+
expect(screen.getByText("Test message")).toBeDefined();
126+
});
127+
128+
agent.emit(runStartedEvent());
129+
agent.emit(
130+
activitySnapshotEvent({
131+
messageId: testId("activity"),
132+
activityType: "test-activity",
133+
content: { message: "Rendered content" },
134+
}),
135+
);
136+
agent.emit(runFinishedEvent());
137+
138+
await waitFor(() => {
139+
expect(screen.getByTestId("activity-render")).toBeDefined();
140+
});
141+
142+
// Verify context is properly propagated - copilotkit should NOT be null
143+
expect(capturedCopilotkit).not.toBeNull();
144+
expect(capturedCopilotkit).toBeDefined();
145+
});
93146
});

0 commit comments

Comments
 (0)