forked from rileytomasek/zodix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemas.test.ts
73 lines (69 loc) · 2.3 KB
/
schemas.test.ts
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
import { zx } from './';
describe('BoolAsString', () => {
test('parses true as string', () => {
expect(zx.BoolAsString.parse('true')).toBe(true);
});
test('parses false as string', () => {
expect(zx.BoolAsString.parse('false')).toBe(false);
});
test('throws on non-boolean string', () => {
expect(() => zx.BoolAsString.parse('hello')).toThrowError();
});
});
describe('CheckboxAsString', () => {
test('parses "on" as boolean', () => {
expect(zx.CheckboxAsString.parse('on')).toBe(true);
});
test('parses undefined as boolean', () => {
expect(zx.CheckboxAsString.parse(undefined)).toBe(false);
});
test('throws on non-"on" string', () => {
expect(() => zx.CheckboxAsString.parse('hello')).toThrowError();
});
});
describe('IntAsString', () => {
test('parses int as string', () => {
expect(zx.IntAsString.parse('3')).toBe(3);
});
test('parses int as string with leading 0', () => {
expect(zx.IntAsString.parse('03')).toBe(3);
});
test('parses negative int as string', () => {
expect(zx.IntAsString.parse('-3')).toBe(-3);
});
test('throws on int as number', () => {
expect(() => zx.IntAsString.parse(3)).toThrowError();
});
test('throws on float', () => {
expect(() => zx.IntAsString.parse(3.14)).toThrowError();
});
test('throws on string float', () => {
expect(() => zx.IntAsString.parse('3.14')).toThrowError();
});
test('throws on non-int string', () => {
expect(() => zx.IntAsString.parse('a3')).toThrowError();
});
});
describe('NumAsString', () => {
test('parses number with decimal as string', () => {
expect(zx.NumAsString.parse('3.14')).toBe(3.14);
});
test('parses number with decimal as string with leading 0', () => {
expect(zx.NumAsString.parse('03.14')).toBe(3.14);
});
test('parses negative number with decimal as string', () => {
expect(zx.NumAsString.parse('-3.14')).toBe(-3.14);
});
test('parses int as string', () => {
expect(zx.NumAsString.parse('3')).toBe(3);
});
test('parses int as string with leading 0', () => {
expect(zx.NumAsString.parse('03')).toBe(3);
});
test('parses negative int as string', () => {
expect(zx.NumAsString.parse('-3')).toBe(-3);
});
test('throws on non-number string', () => {
expect(() => zx.NumAsString.parse('a3')).toThrowError();
});
});