-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromptForm.test.tsx
More file actions
169 lines (142 loc) · 7.76 KB
/
Copy pathPromptForm.test.tsx
File metadata and controls
169 lines (142 loc) · 7.76 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
/**
* Component tests for PromptForm.
*
* Tests cover: field rendering, unauthenticated redirect, optimistic update
* on submission, collection defaulting, and background save + refresh behavior.
*
* All external dependencies (router, auth, data layer) are mocked so these
* tests run entirely in-memory with no network calls.
*/
import React from "react";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { PromptForm } from "@/components/PromptForm";
// ── Mocks ────────────────────────────────────────────────────────────────────
// jest.mock() is hoisted before variable declarations, so factories must only
// use jest.fn() inline — no outer variables. We then import the mocked hooks
// and call mockReturnValue() to inject our named jest.fn() references.
jest.mock("next/navigation", () => ({ useRouter: jest.fn() }));
jest.mock("@/lib/hooks/usePrompts", () => ({ usePrompts: jest.fn() }));
jest.mock("@/lib/promptData", () => ({ savePrompt: jest.fn(() => Promise.resolve()) }));
jest.mock("@/components/AuthProvider", () => ({ useAuth: jest.fn() }));
import { useRouter } from "next/navigation";
import { usePrompts } from "@/lib/hooks/usePrompts";
import { savePrompt } from "@/lib/promptData";
import { useAuth } from "@/components/AuthProvider";
const mockPush = jest.fn();
const mockAddOptimistic = jest.fn();
const mockRefresh = jest.fn(() => Promise.resolve());
const mockSavePrompt = savePrompt as jest.Mock;
// ── Helpers ──────────────────────────────────────────────────────────────────
function renderWithAuth(user: object | null = { id: "user-1", email: "test@example.com" }) {
(useRouter as jest.Mock).mockReturnValue({ push: mockPush });
(usePrompts as jest.Mock).mockReturnValue({
addOptimistic: mockAddOptimistic,
refresh: mockRefresh,
prompts: [],
loading: false,
error: null,
updateOptimistic: jest.fn(),
removeOptimistic: jest.fn(),
});
(useAuth as jest.Mock).mockReturnValue({ user });
return render(<PromptForm />);
}
// ── Tests ─────────────────────────────────────────────────────────────────────
describe("PromptForm – rendering", () => {
it("renders all form fields", () => {
renderWithAuth();
expect(screen.getByPlaceholderText("Give your prompt a name")).toBeInTheDocument();
expect(screen.getByPlaceholderText("Paste or type your prompt here...")).toBeInTheDocument();
expect(screen.getByPlaceholderText("e.g. coding")).toBeInTheDocument();
expect(screen.getByRole("button", { name: /save prompt/i })).toBeInTheDocument();
});
it("renders the model select with default value gpt-4o", () => {
renderWithAuth();
const select = screen.getByRole("combobox") as HTMLSelectElement;
expect(select.value).toBe("gpt-4o");
});
});
describe("PromptForm – unauthenticated behavior", () => {
it("redirects to /login on submit when user is not authenticated", async () => {
renderWithAuth(null);
await userEvent.type(screen.getByPlaceholderText("Give your prompt a name"), "Test Prompt");
await userEvent.type(screen.getByPlaceholderText("Paste or type your prompt here..."), "Some content");
await userEvent.click(screen.getByRole("button", { name: /save prompt/i }));
expect(mockPush).toHaveBeenCalledWith("/login");
expect(mockSavePrompt).not.toHaveBeenCalled();
});
});
describe("PromptForm – authenticated submission", () => {
beforeEach(() => {
jest.clearAllMocks();
mockSavePrompt.mockResolvedValue(undefined);
mockRefresh.mockResolvedValue(undefined);
});
it("calls addOptimistic and navigates home on valid submit", async () => {
renderWithAuth();
await userEvent.type(screen.getByPlaceholderText("Give your prompt a name"), "My Prompt");
await userEvent.type(screen.getByPlaceholderText("Paste or type your prompt here..."), "Prompt content");
await userEvent.click(screen.getByRole("button", { name: /save prompt/i }));
expect(mockAddOptimistic).toHaveBeenCalledWith(
expect.objectContaining({ title: "My Prompt", content: "Prompt content" })
);
expect(mockPush).toHaveBeenCalledWith("/");
});
it("defaults collection to 'uncategorized' when left blank", async () => {
renderWithAuth();
await userEvent.type(screen.getByPlaceholderText("Give your prompt a name"), "Prompt");
await userEvent.type(screen.getByPlaceholderText("Paste or type your prompt here..."), "Content");
await userEvent.click(screen.getByRole("button", { name: /save prompt/i }));
expect(mockAddOptimistic).toHaveBeenCalledWith(
expect.objectContaining({ collection: "uncategorized" })
);
});
it("uses the entered collection name when provided", async () => {
renderWithAuth();
await userEvent.type(screen.getByPlaceholderText("Give your prompt a name"), "Prompt");
await userEvent.type(screen.getByPlaceholderText("Paste or type your prompt here..."), "Content");
await userEvent.type(screen.getByPlaceholderText("e.g. coding"), "engineering");
await userEvent.click(screen.getByRole("button", { name: /save prompt/i }));
expect(mockAddOptimistic).toHaveBeenCalledWith(
expect.objectContaining({ collection: "engineering" })
);
});
it("calls savePrompt asynchronously after optimistic update", async () => {
renderWithAuth();
await userEvent.type(screen.getByPlaceholderText("Give your prompt a name"), "Prompt");
await userEvent.type(screen.getByPlaceholderText("Paste or type your prompt here..."), "Content");
await userEvent.click(screen.getByRole("button", { name: /save prompt/i }));
await waitFor(() => {
expect(mockSavePrompt).toHaveBeenCalledWith(
expect.objectContaining({ title: "Prompt", content: "Content" })
);
});
});
it("calls refresh after savePrompt resolves", async () => {
renderWithAuth();
await userEvent.type(screen.getByPlaceholderText("Give your prompt a name"), "Prompt");
await userEvent.type(screen.getByPlaceholderText("Paste or type your prompt here..."), "Content");
await userEvent.click(screen.getByRole("button", { name: /save prompt/i }));
await waitFor(() => expect(mockRefresh).toHaveBeenCalled());
});
it("still calls refresh when savePrompt rejects, to roll back the optimistic entry", async () => {
mockSavePrompt.mockRejectedValueOnce(new Error("network error"));
// PromptForm logs the error — silence it so Jest output stays clean
jest.spyOn(console, "error").mockImplementation(() => {});
renderWithAuth();
await userEvent.type(screen.getByPlaceholderText("Give your prompt a name"), "Prompt");
await userEvent.type(screen.getByPlaceholderText("Paste or type your prompt here..."), "Content");
await userEvent.click(screen.getByRole("button", { name: /save prompt/i }));
await waitFor(() => expect(mockRefresh).toHaveBeenCalled());
});
it("generated prompt has a non-empty id and ISO timestamp", async () => {
renderWithAuth();
await userEvent.type(screen.getByPlaceholderText("Give your prompt a name"), "Prompt");
await userEvent.type(screen.getByPlaceholderText("Paste or type your prompt here..."), "Content");
await userEvent.click(screen.getByRole("button", { name: /save prompt/i }));
const optimisticArg = mockAddOptimistic.mock.calls[0][0];
expect(optimisticArg.id).toBeTruthy();
expect(new Date(optimisticArg.createdAt).toISOString()).toBe(optimisticArg.createdAt);
});
});