test: add vitest and svelte testing library (resolves #13)
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user