-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathlazyAsync.test.ts
More file actions
112 lines (95 loc) · 3.12 KB
/
Copy pathlazyAsync.test.ts
File metadata and controls
112 lines (95 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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,
'~standard': {
version: 1,
vendor: 'valibot',
validate: expect.any(Function),
},
'~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, [
// eslint-disable-next-line @typescript-eslint/no-empty-function
() => {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
function () {},
]);
});
test('for objects', async () => {
await expectSchemaIssueAsync(schema, baseIssue, [{}, { key: 'value' }]);
});
});
test('should call getter with input', () => {
const getter = vi.fn(() => string());
const dataset = { value: 'foo' };
lazyAsync(getter)['~run'](dataset, {});
expect(getter).toHaveBeenCalledWith(dataset.value, undefined);
});
test('should call getter with signal', () => {
const getter = vi.fn(() => string());
const dataset = { value: 'foo' };
const signal = AbortSignal.abort();
lazyAsync(getter)['~run'](dataset, { signal });
expect(getter).toHaveBeenCalledWith(dataset.value, signal);
});
});