-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBooleanBuilder.test.ts
107 lines (84 loc) · 2.46 KB
/
BooleanBuilder.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
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
import BooleanBuilder, { bool } from '../src/BooleanBuilder';
describe('BooleanBuilder', () => {
let builder: BooleanBuilder<boolean>;
beforeEach(() => {
builder = bool();
});
describe('bool()', () => {
it('returns a builder', () => {
expect(bool(true)).toBeInstanceOf(BooleanBuilder);
});
it('sets type and default value', () => {
builder = bool(true);
expect(builder.type).toBe('boolean');
expect(builder.defaultValue).toBe(true);
});
it('errors if a non-boolean value is used', () => {
expect(() => {
bool().runChecks(
'key',
// @ts-ignore Test invalid type
123,
{},
);
}).toThrowErrorMatchingSnapshot();
});
it('returns the type alias', () => {
expect(bool().typeAlias()).toBe('boolean');
});
});
describe('onlyFalse()', () => {
it('adds a checker', () => {
builder.onlyFalse();
expect(builder.checks[1]).toEqual({
callback: builder.checkOnlyFalse,
args: [],
});
});
it('errors if value is `true`', () => {
builder.onlyFalse();
expect(() => {
builder.runChecks('key', true, { key: true });
}).toThrowErrorMatchingSnapshot();
});
it('passes if value is `false`', () => {
builder.onlyFalse();
expect(() => {
expect(builder.runChecks('key', false, { key: false })).toBe(false);
}).not.toThrow();
});
it('passes if value is undefined', () => {
builder.onlyFalse();
expect(() => {
expect(builder.runChecks('key', undefined, { key: undefined })).toBe(false);
}).not.toThrow();
});
});
describe('onlyTrue()', () => {
it('adds a checker', () => {
builder.onlyTrue();
expect(builder.checks[1]).toEqual({
callback: builder.checkOnlyTrue,
args: [],
});
});
it('errors if value is `false`', () => {
builder.onlyTrue();
expect(() => {
builder.runChecks('key', false, { key: false });
}).toThrowErrorMatchingSnapshot();
});
it('passes if value is `true`', () => {
builder.onlyTrue();
expect(() => {
expect(builder.runChecks('key', true, { key: true })).toBe(true);
}).not.toThrow();
});
it('passes if value is undefined', () => {
builder.onlyTrue();
expect(() => {
expect(builder.runChecks('key', undefined, { key: undefined })).toBe(true);
}).not.toThrow();
});
});
});