Skip to content

Commit 2f47b5c

Browse files
committed
🚨 Improve tests (#2446)
1 parent 894039b commit 2f47b5c

File tree

1 file changed

+69
-11
lines changed

1 file changed

+69
-11
lines changed

src/test/lib/utils/auth_forms.test.ts

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,33 @@ describe('auth_forms', () => {
9292
randomUUID: vi.fn(() => 'test-uuid-12345'),
9393
});
9494

95-
// Setup mocks using mockImplementation for better type compatibility
96-
vi.mocked(superValidate).mockImplementation(() =>
97-
Promise.resolve({
95+
// Improved request-aware superValidate mock
96+
vi.mocked(superValidate).mockImplementation(async (arg: unknown) => {
97+
let data = { username: '', password: '' };
98+
let posted = false;
99+
100+
// If arg is a Request, parse its form data
101+
if (arg instanceof Request) {
102+
posted = arg.method?.toUpperCase() === 'POST';
103+
104+
try {
105+
const formData = await arg.clone().formData();
106+
107+
data = {
108+
username: formData.get('username')?.toString() || '',
109+
password: formData.get('password')?.toString() || '',
110+
};
111+
} catch {
112+
// If parsing fails, use default data
113+
data = { username: '', password: '' };
114+
}
115+
}
116+
117+
return {
98118
id: 'test-form-id',
99119
valid: true,
100-
posted: false,
101-
data: { username: '', password: '' },
120+
posted,
121+
data,
102122
errors: {},
103123
constraints: {
104124
username: { minlength: 3, maxlength: 24, required: true, pattern: '[\\w]*' },
@@ -114,8 +134,8 @@ describe('auth_forms', () => {
114134
password: { type: 'string' },
115135
} as unknown,
116136
message: '',
117-
} as unknown as SuperValidated<Record<string, string>, string>),
118-
);
137+
} as unknown as SuperValidated<Record<string, string>, string>;
138+
});
119139

120140
vi.mocked(zod).mockImplementation((schema: unknown) => schema as any);
121141
});
@@ -239,7 +259,11 @@ describe('auth_forms', () => {
239259
const result = await validateAuthFormWithFallback(mockRequest);
240260

241261
expect(result).toBeDefined();
242-
expect(result.data).toBeDefined();
262+
expect(result.data).toEqual({
263+
username: 'testuser',
264+
password: 'TestPass123',
265+
});
266+
expect(result.posted).toBe(true);
243267
expect(result.message).toBe('');
244268
});
245269

@@ -249,7 +273,33 @@ describe('auth_forms', () => {
249273
const result = await validateAuthFormWithFallback(mockRequest);
250274

251275
expect(result).toBeDefined();
252-
expect(result.data).toBeDefined();
276+
expect(result.data).toEqual({
277+
username: '',
278+
password: '',
279+
});
280+
expect(result.posted).toBe(true);
281+
});
282+
283+
test('expect to distinguish between POST and GET requests', async () => {
284+
// Test POST request
285+
const postRequest = createMockRequest('testuser', 'testpass');
286+
const postResult = await validateAuthFormWithFallback(postRequest);
287+
288+
expect(postResult.posted).toBe(true);
289+
expect(postResult.data).toEqual({
290+
username: 'testuser',
291+
password: 'testpass',
292+
});
293+
294+
// Test GET request (no form data)
295+
const getRequest = new Request('http://localhost:3000', { method: 'GET' });
296+
const getResult = await validateAuthFormWithFallback(getRequest);
297+
298+
expect(getResult.posted).toBe(false);
299+
expect(getResult.data).toEqual({
300+
username: '',
301+
password: '',
302+
});
253303
});
254304

255305
test('expect to use fallback strategy when primary validation fails', async () => {
@@ -277,7 +327,11 @@ describe('auth_forms', () => {
277327
const result = await validateAuthFormWithFallback(mockRequest);
278328

279329
expect(result).toBeDefined();
280-
expect(result.data).toBeDefined();
330+
expect(result.data).toEqual({
331+
username: 'complexuser123',
332+
password: 'ComplexPass123!',
333+
});
334+
expect(result.posted).toBe(true);
281335
});
282336
});
283337

@@ -288,7 +342,11 @@ describe('auth_forms', () => {
288342

289343
await createAuthFormWithFallback();
290344

291-
expect(mockConsoleWarn).toHaveBeenCalled();
345+
if (import.meta.env.DEV) {
346+
expect(mockConsoleWarn).toHaveBeenCalled();
347+
} else {
348+
expect(mockConsoleWarn).not.toHaveBeenCalled();
349+
}
292350
});
293351

294352
test('expect to handle Error objects correctly in strategy failure logging', async () => {

0 commit comments

Comments
 (0)