47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { extractFormData } from './extractFormData';
|
|
import * as v from 'valibot';
|
|
|
|
describe('extractFormData', () => {
|
|
it('should successfully extract and validate correct form data', async () => {
|
|
const formData = new FormData();
|
|
formData.append('name', 'John Doe');
|
|
formData.append('age', '30');
|
|
|
|
const request = new Request('http://localhost', {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
|
|
const schema = v.object({
|
|
name: v.string(),
|
|
age: v.string()
|
|
});
|
|
|
|
const result = await extractFormData(request, schema);
|
|
|
|
expect(result.error).toBeNull();
|
|
expect(result.data).toEqual({ name: 'John Doe', age: '30' });
|
|
});
|
|
|
|
it('should fail validation with missing required fields', async () => {
|
|
const formData = new FormData();
|
|
formData.append('age', '30');
|
|
|
|
const request = new Request('http://localhost', {
|
|
method: 'POST',
|
|
body: formData
|
|
});
|
|
|
|
const schema = v.object({
|
|
name: v.string(),
|
|
age: v.string()
|
|
});
|
|
|
|
const result = await extractFormData(request, schema);
|
|
|
|
expect(result.data).toBeUndefined();
|
|
expect(result.error).toBeTypeOf('string');
|
|
});
|
|
});
|