-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathcompletable.test.ts
More file actions
55 lines (43 loc) · 2.01 KB
/
Copy pathcompletable.test.ts
File metadata and controls
55 lines (43 loc) · 2.01 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
import { completable, getCompleter } from './completable.js';
import { zodTestMatrix, type ZodMatrixEntry } from '../__fixtures__/zodTestMatrix.js';
describe.each(zodTestMatrix)('completable with $zodVersionLabel', (entry: ZodMatrixEntry) => {
const { z } = entry;
it('preserves types and values of underlying schema', () => {
const baseSchema = z.string();
const schema = completable(baseSchema, () => []);
expect(schema.parse('test')).toBe('test');
expect(() => schema.parse(123)).toThrow();
});
it('provides access to completion function', async () => {
const completions = ['foo', 'bar', 'baz'];
const schema = completable(z.string(), () => completions);
const completer = getCompleter(schema);
expect(completer).toBeDefined();
expect(await completer!('')).toEqual(completions);
});
it('allows async completion functions', async () => {
const completions = ['foo', 'bar', 'baz'];
const schema = completable(z.string(), async () => completions);
const completer = getCompleter(schema);
expect(completer).toBeDefined();
expect(await completer!('')).toEqual(completions);
});
it('passes current value to completion function', async () => {
const schema = completable(z.string(), value => [value + '!']);
const completer = getCompleter(schema);
expect(completer).toBeDefined();
expect(await completer!('test')).toEqual(['test!']);
});
it('works with number schemas', async () => {
const schema = completable(z.number(), () => [1, 2, 3]);
expect(schema.parse(1)).toBe(1);
const completer = getCompleter(schema);
expect(completer).toBeDefined();
expect(await completer!(0)).toEqual([1, 2, 3]);
});
it('preserves schema description', () => {
const desc = 'test description';
const schema = completable(z.string().describe(desc), () => []);
expect(schema.description).toBe(desc);
});
});