-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
3 changed files
with
38 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
import type { SchemaInput, SchemaOutput } from './types' | ||
import { type } from './schema-utils' | ||
|
||
it('type', () => { | ||
const schema = type<string>() | ||
describe('type', () => { | ||
it('without map', () => { | ||
const schema = type<string>() | ||
|
||
expectTypeOf<SchemaInput<typeof schema>>().toEqualTypeOf<string>() | ||
expectTypeOf<SchemaOutput<typeof schema>>().toEqualTypeOf<string>() | ||
expectTypeOf<SchemaInput<typeof schema>>().toEqualTypeOf<string>() | ||
expectTypeOf<SchemaOutput<typeof schema>>().toEqualTypeOf<string>() | ||
}) | ||
|
||
const schema2 = type<string, number>() | ||
expectTypeOf<SchemaInput<typeof schema2>>().toEqualTypeOf<string>() | ||
expectTypeOf<SchemaOutput<typeof schema2>>().toEqualTypeOf<number>() | ||
it('with map', () => { | ||
const schema2 = type<string, number>((val) => { | ||
expectTypeOf(val).toEqualTypeOf<string>() | ||
|
||
return Number(val) | ||
}) | ||
|
||
expectTypeOf<SchemaInput<typeof schema2>>().toEqualTypeOf<string>() | ||
expectTypeOf<SchemaOutput<typeof schema2>>().toEqualTypeOf<number>() | ||
|
||
// @ts-expect-error - map is required when TInput !== TOutput | ||
type<string, number>() | ||
|
||
// @ts-expect-error - output not match number | ||
type<string, number>(() => '123') | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import { type } from './schema-utils' | ||
|
||
describe('type', async () => { | ||
it('without check', async () => { | ||
it('without map', async () => { | ||
const schema = type() | ||
const val = {} | ||
expect((await schema['~standard'].validate(val) as any).value).toBe(val) | ||
}) | ||
|
||
it('with check', async () => { | ||
it('with map', async () => { | ||
const val = {} | ||
const check = vi.fn().mockReturnValueOnce({ value: val }) | ||
const check = vi.fn().mockReturnValueOnce('__mapped__') | ||
const schema = type(check) | ||
expect((await schema['~standard'].validate(val) as any).value).toBe(val) | ||
expect((await schema['~standard'].validate(val) as any).value).toBe('__mapped__') | ||
expect(check).toHaveBeenCalledWith(val) | ||
}) | ||
}) |
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