Skip to content

Commit 9ea52f4

Browse files
committed
🚨 Add tests for form schema (#2446)
1 parent 0c50d52 commit 9ea52f4

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/test/lib/utils/authorship.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,85 @@ describe('createAuthFormWithFallback', () => {
163163
});
164164
});
165165
});
166+
167+
describe('Auth form for error cases', () => {
168+
test('expect to handle errors during strategy execution gracefully', async () => {
169+
// Mock console.warn to spy on error logging
170+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
171+
172+
// Mock first two strategies to fail, third should succeed
173+
vi.mocked(superValidate)
174+
.mockRejectedValueOnce(new Error('Failed to create basic strategy'))
175+
.mockRejectedValueOnce(new Error('Failed to create strategy with zod adapter'));
176+
vi.mocked(zod).mockReturnValue({} as any);
177+
178+
const result = await createAuthFormWithFallback();
179+
180+
// Should successfully fallback to manual creation
181+
expect(result.form).toMatchObject({
182+
valid: true,
183+
posted: false,
184+
data: { username: '', password: '' },
185+
errors: {},
186+
message: '',
187+
});
188+
expect(result.form.constraints).toBeDefined();
189+
expect(result.form.shape).toBeDefined();
190+
191+
// Console.warn should be called in DEV environment
192+
// (Note: This depends on import.meta.env.DEV being true in test environment)
193+
consoleWarnSpy.mockRestore();
194+
});
195+
196+
test('expect to handle zod adapter creation errors', async () => {
197+
// Mock zod to throw an error
198+
vi.mocked(zod)
199+
.mockImplementationOnce(() => {
200+
throw new Error('Failed to create Zod adapter');
201+
})
202+
.mockImplementationOnce(() => {
203+
throw new Error('Failed to create Zod adapter again');
204+
})
205+
.mockReturnValue({} as any); // Third call succeeds for manual creation
206+
207+
vi.mocked(superValidate).mockRejectedValue(
208+
new Error('Failed to create strategy with SuperValidate'),
209+
);
210+
211+
const result = await createAuthFormWithFallback();
212+
213+
// Expect to succeed with manual creation
214+
expect(result.form).toMatchObject({
215+
valid: true,
216+
posted: false,
217+
data: { username: '', password: '' },
218+
errors: {},
219+
message: '',
220+
});
221+
});
222+
223+
test('expect to handle unexpected error types gracefully', async () => {
224+
// Mock superValidate to throw non-Error objects
225+
vi.mocked(superValidate)
226+
.mockRejectedValueOnce('String error instead of Error object')
227+
.mockRejectedValueOnce(null)
228+
.mockRejectedValueOnce(undefined);
229+
vi.mocked(zod).mockReturnValue({} as any);
230+
231+
// Should still fallback to manual creation successfully
232+
const result = await createAuthFormWithFallback();
233+
234+
expect(result.form).toMatchObject({
235+
valid: true,
236+
posted: false,
237+
data: { username: '', password: '' },
238+
errors: {},
239+
message: '',
240+
});
241+
expect(result.form.constraints).toBeDefined();
242+
expect(result.form.shape).toBeDefined();
243+
});
244+
});
166245
});
167246

168247
describe('validateAuthFormWithFallback', () => {

0 commit comments

Comments
 (0)