-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit and type tests for lazyAsync schema
- Loading branch information
1 parent
6241e0b
commit 3573e82
Showing
9 changed files
with
158 additions
and
27 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { describe, expectTypeOf, test } from 'vitest'; | ||
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts'; | ||
import { | ||
string, | ||
type StringIssue, | ||
type StringSchema, | ||
} from '../string/index.ts'; | ||
import { lazyAsync, type LazySchemaAsync } from './lazyAsync.ts'; | ||
|
||
describe('lazyAsync', () => { | ||
test('should return schema object', () => { | ||
expectTypeOf(lazyAsync(async () => string())).toEqualTypeOf< | ||
LazySchemaAsync<StringSchema<undefined>> | ||
>(); | ||
}); | ||
|
||
describe('should infer correct types', () => { | ||
type Schema = LazySchemaAsync<StringSchema<undefined>>; | ||
|
||
test('of input', () => { | ||
expectTypeOf<InferInput<Schema>>().toEqualTypeOf<string>(); | ||
}); | ||
|
||
test('of output', () => { | ||
expectTypeOf<InferOutput<Schema>>().toEqualTypeOf<string>(); | ||
}); | ||
|
||
test('of issue', () => { | ||
expectTypeOf<InferIssue<Schema>>().toEqualTypeOf<StringIssue>(); | ||
}); | ||
}); | ||
}); |
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,97 @@ | ||
import { describe, expect, test, vi } from 'vitest'; | ||
import { | ||
expectNoSchemaIssueAsync, | ||
expectSchemaIssueAsync, | ||
} from '../../vitest/index.ts'; | ||
import { | ||
string, | ||
type StringIssue, | ||
type StringSchema, | ||
} from '../string/index.ts'; | ||
import { lazyAsync, type LazySchemaAsync } from './lazyAsync.ts'; | ||
|
||
describe('lazyAsync', () => { | ||
test('should return schema object', () => { | ||
const getter = async () => string(); | ||
expect(lazyAsync(getter)).toStrictEqual({ | ||
kind: 'schema', | ||
type: 'lazy', | ||
reference: lazyAsync, | ||
expects: 'unknown', | ||
getter, | ||
async: true, | ||
_run: expect.any(Function), | ||
} satisfies LazySchemaAsync<StringSchema<undefined>>); | ||
}); | ||
|
||
describe('should return dataset without issues', () => { | ||
const schema = lazyAsync(() => string()); | ||
|
||
test('for strings', async () => { | ||
await expectNoSchemaIssueAsync(schema, ['', 'foo', '123']); | ||
}); | ||
}); | ||
|
||
describe('should return dataset with issues', () => { | ||
const schema = lazyAsync(() => string('message')); | ||
const baseIssue: Omit<StringIssue, 'input' | 'received'> = { | ||
kind: 'schema', | ||
type: 'string', | ||
expected: 'string', | ||
message: 'message', | ||
}; | ||
|
||
// Primitive types | ||
|
||
test('for bigints', async () => { | ||
await expectSchemaIssueAsync(schema, baseIssue, [-1n, 0n, 123n]); | ||
}); | ||
|
||
test('for booleans', async () => { | ||
await expectSchemaIssueAsync(schema, baseIssue, [true, false]); | ||
}); | ||
|
||
test('for null', async () => { | ||
await expectSchemaIssueAsync(schema, baseIssue, [null]); | ||
}); | ||
|
||
test('for numbers', async () => { | ||
await expectSchemaIssueAsync(schema, baseIssue, [-1, 0, 123, 45.67]); | ||
}); | ||
|
||
test('for undefined', async () => { | ||
await expectSchemaIssueAsync(schema, baseIssue, [undefined]); | ||
}); | ||
|
||
test('for symbols', async () => { | ||
await expectSchemaIssueAsync(schema, baseIssue, [ | ||
Symbol(), | ||
Symbol('foo'), | ||
]); | ||
}); | ||
|
||
// Complex types | ||
|
||
test('for arrays', async () => { | ||
await expectSchemaIssueAsync(schema, baseIssue, [[], ['value']]); | ||
}); | ||
|
||
test('for functions', async () => { | ||
await expectSchemaIssueAsync(schema, baseIssue, [ | ||
() => {}, | ||
function () {}, | ||
]); | ||
}); | ||
|
||
test('for objects', async () => { | ||
await expectSchemaIssueAsync(schema, baseIssue, [{}, { key: 'value' }]); | ||
}); | ||
}); | ||
|
||
test('should call getter with input', () => { | ||
const getter = vi.fn(() => string()); | ||
const dataset = { typed: false, value: 'foo' }; | ||
lazyAsync(getter)._run(dataset, {}); | ||
expect(getter).toHaveBeenCalledWith(dataset.value); | ||
}); | ||
}); |
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
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
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
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