-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
521ed2a
commit b10ffbd
Showing
5 changed files
with
2,138 additions
and
1,268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { test, expect, describe } from 'vitest' | ||
import { z } from 'zod' | ||
|
||
import { zodSp } from './main.js' | ||
|
||
const ISO_DATE_STUB = '2023-11-22T07:32:03.889Z' | ||
|
||
test.each([ | ||
{ input: '', expected: {} }, | ||
{ input: 'a=1', expected: { a: '1' } }, | ||
{ input: 'a=true', expected: { a: 'true' } }, | ||
{ input: 'a=asd', expected: { a: 'asd' } }, | ||
{ input: 'a=1&b=foo&b=bar', expected: { a: '1', b: 'bar' } }, | ||
{ input: 'a=1&b=foo,bar', expected: { a: '1', b: 'foo,bar' } }, | ||
{ input: 'a=1&b[]=foo,bar', expected: { a: '1', b: ['foo', 'bar'] } }, | ||
])('.parse($input) -> $expected', ({ input, expected }) => { | ||
expect(zodSp.parse(new URLSearchParams(input))).toEqual(expected) | ||
}) | ||
|
||
test.each([ | ||
{ input: {}, expected: '' }, | ||
{ input: { a: 1 }, expected: 'a=1' }, | ||
{ input: { a: true }, expected: 'a=true' }, | ||
{ input: { a: 'asd' }, expected: 'a=asd' }, | ||
{ input: { a: 1, b: ['foo', 'bar'] }, expected: 'a=1&b%5B%5D=foo%2Cbar' }, | ||
{ input: { a: 1, b: null, c: undefined }, expected: 'a=1' }, | ||
{ input: { a: 1, b: new Date(ISO_DATE_STUB), c: [new Date(ISO_DATE_STUB), new Date(ISO_DATE_STUB)] }, expected: `a=1&b=${encodeURIComponent(ISO_DATE_STUB)}&c%5B%5D=${encodeURIComponent(`${ISO_DATE_STUB},${ISO_DATE_STUB}`)}` }, | ||
])('.serialize($input) -> $expected', ({ input, expected }) => { | ||
expect(zodSp.serialize(input).toString()).toBe(expected) | ||
}) | ||
|
||
describe('.with(ZodSchema)', () => { | ||
const defaultTuple: [string, number] = ['default', 0] | ||
const ZodSchema = z.object({ | ||
a: z.string().optional(), | ||
b: z.coerce.number().optional(), | ||
c: z.coerce.boolean().default(false), | ||
d: z.array(z.coerce.date()).optional(), | ||
e: z.tuple([z.string(), z.coerce.number()]).catch(() => defaultTuple), | ||
f: z.coerce.date().optional(), | ||
}) | ||
const dummySp = zodSp.with(ZodSchema) | ||
|
||
test.each([ | ||
{ input: '', expected: { c: false, e: defaultTuple } }, | ||
{ input: 'c=true&a=123&e[]=foo,123', expected: { a: '123', c: true, e: ['foo', 123] } }, | ||
{ input: 'c=true&a=123&e[]=foo,123', expected: { a: '123', c: true, e: ['foo', 123] } }, | ||
{ input: zodSp.serialize({ d: [new Date(ISO_DATE_STUB), new Date(ISO_DATE_STUB)] }), expected: { a: '123', c: true, d: [new Date(ISO_DATE_STUB), new Date(ISO_DATE_STUB)], e: ['default', 0] } }, | ||
])('.parse($input) -> $expected', ({ input, expected }) => { | ||
expect(dummySp.parse(new URLSearchParams(input))).toEqual(expected) | ||
}) | ||
|
||
test.each([ | ||
{ input: 1, expected: 1 }, | ||
])('.serialize($input) -> $expected', ({ input, expected }) => { | ||
expect(input).toBe(expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
coverage: { | ||
all: true, | ||
functions: 90, | ||
branches: 90, | ||
lines: 90, | ||
statements: 90, | ||
include: ['src/**/*.ts'], | ||
skipFull: true, | ||
}, | ||
}, | ||
}) |
Oops, something went wrong.