-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpage.test.tsx
More file actions
230 lines (187 loc) · 6.95 KB
/
Copy pathpage.test.tsx
File metadata and controls
230 lines (187 loc) · 6.95 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
import { describe, it, expect, vi, beforeEach } from "vitest";
import "@testing-library/jest-dom/vitest";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
// Mock next/navigation
const mockPush = vi.fn();
let mockSearchParams = new URLSearchParams();
vi.mock("next/navigation", () => ({
useRouter: () => ({ push: mockPush }),
useSearchParams: () => mockSearchParams,
}));
// Mock next/link as a simple anchor
vi.mock("next/link", () => ({
default: ({
href,
children,
...props
}: {
href: string;
children: React.ReactNode;
}) => (
<a href={href} {...props}>
{children}
</a>
),
}));
// Mock OAuthButtons to avoid tooltip provider dependency
vi.mock("@/components/auth/oauth-buttons", () => ({
OAuthButtons: () => <div data-testid="oauth-buttons" />,
}));
// Supabase mock state
const mockSignInWithPassword = vi.fn();
const mockGetUser = vi.fn();
const mockFrom = vi.fn();
vi.mock("@/lib/supabase/client", () => ({
createClient: () => ({
auth: {
signInWithPassword: mockSignInWithPassword,
getUser: mockGetUser,
},
from: mockFrom,
}),
}));
import SignInPage from "./page";
beforeEach(() => {
vi.clearAllMocks();
mockSearchParams = new URLSearchParams();
});
describe("SignInPage", () => {
it("renders email and password fields", () => {
render(<SignInPage />);
expect(screen.getByLabelText("Email")).toBeInTheDocument();
expect(screen.getByLabelText("Password")).toBeInTheDocument();
expect(
screen.getByRole("button", { name: /sign in/i }),
).toBeInTheDocument();
});
it("email field has type=email and is required", () => {
render(<SignInPage />);
const input = screen.getByLabelText("Email");
expect(input).toBeRequired();
expect(input).toHaveAttribute("type", "email");
});
it("password field has minLength=6 and is required", () => {
render(<SignInPage />);
const input = screen.getByLabelText("Password");
expect(input).toBeRequired();
expect(input).toHaveAttribute("type", "password");
expect(input).toHaveAttribute("minLength", "6");
});
it("shows error message when sign-in fails", async () => {
mockSignInWithPassword.mockResolvedValue({
error: { message: "Invalid login credentials" },
});
const user = userEvent.setup();
render(<SignInPage />);
await user.type(screen.getByLabelText("Email"), "jane@example.com");
await user.type(screen.getByLabelText("Password"), "wrongpass");
const form = screen.getByRole("button", { name: /sign in/i })
.closest("form")!;
form.requestSubmit();
await waitFor(() => {
expect(
screen.getByText("Invalid login credentials"),
).toBeInTheDocument();
});
});
it("calls supabase.auth.signInWithPassword with correct parameters", async () => {
mockSignInWithPassword.mockResolvedValue({ error: null });
mockGetUser.mockResolvedValue({
data: { user: { id: "user-1" } },
});
const mockMaybeSingle = vi.fn().mockResolvedValue({
data: { workspace_id: "ws-1", workspaces: { slug: "jane" } },
});
const mockLimit = vi.fn().mockReturnValue({ maybeSingle: mockMaybeSingle });
const mockEq = vi.fn().mockReturnValue({ limit: mockLimit });
const mockSelect = vi.fn().mockReturnValue({ eq: mockEq });
mockFrom.mockReturnValue({ select: mockSelect });
const user = userEvent.setup();
render(<SignInPage />);
await user.type(screen.getByLabelText("Email"), "jane@example.com");
await user.type(screen.getByLabelText("Password"), "password123");
const form = screen.getByRole("button", { name: /sign in/i })
.closest("form")!;
form.requestSubmit();
await waitFor(() => {
expect(mockSignInWithPassword).toHaveBeenCalledWith({
email: "jane@example.com",
password: "password123",
});
});
});
it("redirects to workspace after successful sign-in", async () => {
mockSignInWithPassword.mockResolvedValue({ error: null });
mockGetUser.mockResolvedValue({
data: { user: { id: "user-1" } },
});
const mockMaybeSingle = vi.fn().mockResolvedValue({
data: { workspace_id: "ws-1", workspaces: { slug: "jane" } },
});
const mockLimit = vi.fn().mockReturnValue({ maybeSingle: mockMaybeSingle });
const mockEq = vi.fn().mockReturnValue({ limit: mockLimit });
const mockSelect = vi.fn().mockReturnValue({ eq: mockEq });
mockFrom.mockReturnValue({ select: mockSelect });
const user = userEvent.setup();
render(<SignInPage />);
await user.type(screen.getByLabelText("Email"), "jane@example.com");
await user.type(screen.getByLabelText("Password"), "password123");
const form = screen.getByRole("button", { name: /sign in/i })
.closest("form")!;
form.requestSubmit();
await waitFor(() => {
expect(mockPush).toHaveBeenCalledWith("/jane");
});
});
it("redirects to root when no workspace found after sign-in", async () => {
mockSignInWithPassword.mockResolvedValue({ error: null });
mockGetUser.mockResolvedValue({
data: { user: { id: "user-1" } },
});
const mockMaybeSingle = vi.fn().mockResolvedValue({ data: null });
const mockLimit = vi.fn().mockReturnValue({ maybeSingle: mockMaybeSingle });
const mockEq = vi.fn().mockReturnValue({ limit: mockLimit });
const mockSelect = vi.fn().mockReturnValue({ eq: mockEq });
mockFrom.mockReturnValue({ select: mockSelect });
const user = userEvent.setup();
render(<SignInPage />);
await user.type(screen.getByLabelText("Email"), "jane@example.com");
await user.type(screen.getByLabelText("Password"), "password123");
const form = screen.getByRole("button", { name: /sign in/i })
.closest("form")!;
form.requestSubmit();
await waitFor(() => {
expect(mockPush).toHaveBeenCalledWith("/");
});
});
it("disables submit button while loading", async () => {
// Never resolve to keep loading state
mockSignInWithPassword.mockReturnValue(new Promise(() => {}));
const user = userEvent.setup();
render(<SignInPage />);
await user.type(screen.getByLabelText("Email"), "jane@example.com");
await user.type(screen.getByLabelText("Password"), "password123");
const submitButton = screen.getByRole("button", { name: /sign in/i });
const form = submitButton.closest("form")!;
form.requestSubmit();
await waitFor(() => {
expect(
screen.getByRole("button", { name: /signing in/i }),
).toBeDisabled();
});
});
it("shows confirmation success message when confirmed=true query param is present", () => {
mockSearchParams = new URLSearchParams("confirmed=true");
render(<SignInPage />);
expect(
screen.getByText(/email confirmed/i),
).toBeInTheDocument();
});
it("does not show confirmation message without query param", () => {
render(<SignInPage />);
expect(
screen.queryByText(/email confirmed/i),
).not.toBeInTheDocument();
});
});