forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotModal.test.tsx
More file actions
257 lines (199 loc) · 7.55 KB
/
Copy pathCopilotModal.test.tsx
File metadata and controls
257 lines (199 loc) · 7.55 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import React, { createRef } from "react";
import { render, act } from "@testing-library/react";
import { describe, it, expect, beforeEach, vi } from "vitest";
// ---------------------------------------------------------------------------
// Mocks
// ---------------------------------------------------------------------------
const hoisted = vi.hoisted(() => {
const snapToIndex = vi.fn();
const close = vi.fn();
return {
snapToIndex,
close,
lastOnClose: null as (() => void) | null,
lastSnapPoints: null as (string | number)[] | null,
lastIndex: null as number | null,
};
});
// Mock react-native primitives used in CopilotModal
vi.mock("react-native", () => ({
StyleSheet: {
create: (s: Record<string, unknown>) => s,
},
View: "View",
}));
// Mock @gorhom/bottom-sheet
vi.mock("@gorhom/bottom-sheet", () => {
const React = require("react");
// BottomSheet stores its ref and captures props for assertions
const BottomSheet = React.forwardRef(function MockBottomSheet(
props: any,
ref: any,
) {
React.useImperativeHandle(ref, () => ({
snapToIndex: hoisted.snapToIndex,
close: hoisted.close,
}));
hoisted.lastOnClose = props.onClose ?? null;
hoisted.lastSnapPoints = props.snapPoints ?? null;
hoisted.lastIndex = props.index ?? null;
return React.createElement(
"mock-bottom-sheet",
{ "data-testid": "bottom-sheet" },
props.children,
);
});
const BottomSheetView = (props: any) =>
React.createElement(
"mock-bottom-sheet-view",
{ "data-testid": "bottom-sheet-view" },
props.children,
);
const BottomSheetBackdrop = (props: any) =>
React.createElement("mock-backdrop", props);
const BottomSheetFlatList = (props: any) =>
React.createElement(
"mock-bottom-sheet-flatlist",
{ "data-testid": "bottom-sheet-flatlist" },
props.children,
);
return {
__esModule: true,
default: BottomSheet,
BottomSheetView,
BottomSheetBackdrop,
BottomSheetFlatList,
};
});
// Mock CopilotChat (B4's component — may not exist yet in this worktree)
vi.mock("../CopilotChat", () => {
const React = require("react");
const CopilotChat = (props: any) =>
React.createElement("mock-copilot-chat", {
"data-testid": "copilot-chat",
...props,
});
return { CopilotChat };
});
// Import after mocks
import { CopilotModal } from "../CopilotModal";
import type { CopilotModalRef } from "../CopilotModal";
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("CopilotModal", () => {
beforeEach(() => {
hoisted.snapToIndex.mockClear();
hoisted.close.mockClear();
hoisted.lastOnClose = null;
hoisted.lastSnapPoints = null;
hoisted.lastIndex = null;
});
// ── Rendering ───────────────────────────────────────────────────────────
describe("rendering", () => {
it("renders the bottom sheet with default snap points", () => {
render(<CopilotModal />);
expect(hoisted.lastSnapPoints).toEqual(["50%", "90%"]);
});
it("starts closed (index -1)", () => {
render(<CopilotModal />);
expect(hoisted.lastIndex).toBe(-1);
});
it("embeds CopilotChat inside the sheet", () => {
const { getByTestId } = render(<CopilotModal />);
expect(getByTestId("copilot-chat")).toBeTruthy();
});
});
// ── Controlled visibility ─────────────────────────────────────────────
describe("controlled visibility", () => {
it("opens the sheet when visible becomes true", () => {
const { rerender } = render(<CopilotModal visible={false} />);
rerender(<CopilotModal visible={true} />);
expect(hoisted.snapToIndex).toHaveBeenCalledWith(0);
});
it("closes the sheet when visible becomes false", () => {
const { rerender } = render(<CopilotModal visible={true} />);
hoisted.snapToIndex.mockClear();
rerender(<CopilotModal visible={false} />);
expect(hoisted.close).toHaveBeenCalled();
});
});
// ── onDismiss ─────────────────────────────────────────────────────────
describe("onDismiss", () => {
it("calls onDismiss when the sheet closes", () => {
const onDismiss = vi.fn();
render(<CopilotModal onDismiss={onDismiss} />);
// Simulate bottom-sheet calling onClose
act(() => {
hoisted.lastOnClose?.();
});
expect(onDismiss).toHaveBeenCalledTimes(1);
});
it("does not call onDismiss when enableDismissOnClose is false", () => {
const onDismiss = vi.fn();
render(
<CopilotModal onDismiss={onDismiss} enableDismissOnClose={false} />,
);
act(() => {
hoisted.lastOnClose?.();
});
expect(onDismiss).not.toHaveBeenCalled();
});
});
// ── Custom snap points ────────────────────────────────────────────────
describe("custom snap points", () => {
it("uses provided snap points", () => {
render(<CopilotModal snapPoints={["25%", "75%", "100%"]} />);
expect(hoisted.lastSnapPoints).toEqual(["25%", "75%", "100%"]);
});
it("opens at the specified initialSnapIndex", () => {
const ref = createRef<CopilotModalRef>();
render(<CopilotModal ref={ref} initialSnapIndex={1} />);
act(() => {
ref.current?.open();
});
expect(hoisted.snapToIndex).toHaveBeenCalledWith(1);
});
});
// ── Imperative API ────────────────────────────────────────────────────
describe("imperative API", () => {
it("exposes open() via ref", () => {
const ref = createRef<CopilotModalRef>();
render(<CopilotModal ref={ref} />);
act(() => {
ref.current?.open();
});
expect(hoisted.snapToIndex).toHaveBeenCalledWith(0);
});
it("exposes close() via ref", () => {
const ref = createRef<CopilotModalRef>();
render(<CopilotModal ref={ref} />);
act(() => {
ref.current?.close();
});
expect(hoisted.close).toHaveBeenCalled();
});
});
// ── CopilotChat pass-through ──────────────────────────────────────────
describe("CopilotChat integration", () => {
it("passes agentName to CopilotChat", () => {
const { getByTestId } = render(<CopilotModal agentName="test-agent" />);
const chat = getByTestId("copilot-chat");
expect(chat.getAttribute("agentName")).toBe("test-agent");
});
it("passes placeholder to CopilotChat", () => {
const { getByTestId } = render(
<CopilotModal placeholder="Ask me anything..." />,
);
const chat = getByTestId("copilot-chat");
expect(chat.getAttribute("placeholder")).toBe("Ask me anything...");
});
it("passes headerTitle to CopilotChat", () => {
const { getByTestId } = render(
<CopilotModal headerTitle="AI Assistant" />,
);
const chat = getByTestId("copilot-chat");
expect(chat.getAttribute("headerTitle")).toBe("AI Assistant");
});
});
});