-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseDraftLifecycle.test.ts
More file actions
119 lines (96 loc) · 3.98 KB
/
useDraftLifecycle.test.ts
File metadata and controls
119 lines (96 loc) · 3.98 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
// @vitest-environment jsdom
import { fireEvent, renderHook } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { useDraftLifecycle } from "./useDraftLifecycle";
type HookOptions = Parameters<typeof useDraftLifecycle>[0];
function makeOptions(overrides: Partial<HookOptions> = {}): HookOptions {
return {
initialDraft: null,
viewMode: "panels",
input: "",
savedDraftId: null,
refreshWorkspaceCatalog: vi.fn().mockResolvedValue(undefined),
findSimilar: vi.fn().mockResolvedValue(undefined),
loadAlternatives: vi.fn().mockResolvedValue(undefined),
loadTemplates: vi.fn().mockResolvedValue(undefined),
handleLoadDraft: vi.fn(),
onPanelDensityModeChange: vi.fn(),
setSuggestionsDismissed: vi.fn(),
...overrides,
};
}
describe("useDraftLifecycle", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("refreshes catalog and loads templates on mount", () => {
const options = makeOptions();
renderHook(() => useDraftLifecycle(options));
expect(options.refreshWorkspaceCatalog).toHaveBeenCalledTimes(1);
expect(options.loadTemplates).toHaveBeenCalledTimes(1);
});
it("finds similar saved responses once input has 10+ characters", () => {
const options = makeOptions({ input: "short" });
const { rerender } = renderHook(
(props: HookOptions) => useDraftLifecycle(props),
{ initialProps: options },
);
expect(options.findSimilar).not.toHaveBeenCalled();
const updated = makeOptions({
...options,
input: "longer than ten characters",
});
rerender(updated);
expect(updated.findSimilar).toHaveBeenCalledWith(
"longer than ten characters",
);
expect(updated.setSuggestionsDismissed).toHaveBeenCalledWith(false);
});
it("loads alternatives when a savedDraftId appears", () => {
const options = makeOptions();
const { rerender } = renderHook(
(props: HookOptions) => useDraftLifecycle(props),
{ initialProps: options },
);
expect(options.loadAlternatives).not.toHaveBeenCalled();
const updated = makeOptions({ ...options, savedDraftId: "draft-1" });
rerender(updated);
expect(updated.loadAlternatives).toHaveBeenCalledWith("draft-1");
});
it("fires initialDraft load exactly once when the prop is provided", () => {
const initialDraft = { id: "d-1" } as HookOptions["initialDraft"];
const options = makeOptions({ initialDraft });
renderHook(() => useDraftLifecycle(options));
expect(options.handleLoadDraft).toHaveBeenCalledWith(initialDraft);
});
it("maps Cmd-1/2/3 to the three panel density modes in panels view", () => {
const options = makeOptions({ viewMode: "panels" });
renderHook(() => useDraftLifecycle(options));
fireEvent.keyDown(window, { key: "1", metaKey: true });
expect(options.onPanelDensityModeChange).toHaveBeenCalledWith("balanced");
fireEvent.keyDown(window, { key: "2", metaKey: true });
expect(options.onPanelDensityModeChange).toHaveBeenCalledWith(
"focus-intake",
);
fireEvent.keyDown(window, { key: "3", metaKey: true });
expect(options.onPanelDensityModeChange).toHaveBeenCalledWith(
"focus-response",
);
});
it("ignores keyboard shortcuts when viewMode is conversation", () => {
const options = makeOptions({ viewMode: "conversation" });
renderHook(() => useDraftLifecycle(options));
fireEvent.keyDown(window, { key: "1", metaKey: true });
expect(options.onPanelDensityModeChange).not.toHaveBeenCalled();
});
it("ignores keyboard shortcuts when focus is in an editable target", () => {
const options = makeOptions({ viewMode: "panels" });
renderHook(() => useDraftLifecycle(options));
const input = document.createElement("input");
document.body.appendChild(input);
input.focus();
fireEvent.keyDown(input, { key: "1", metaKey: true, bubbles: true });
expect(options.onPanelDensityModeChange).not.toHaveBeenCalled();
document.body.removeChild(input);
});
});