-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoperators.test.ts
137 lines (117 loc) · 3.93 KB
/
operators.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
optional,
nullable,
erratic,
and,
or,
compose,
customTest,
unsafeTest
} from "./operators/operators";
describe("[Operators] optional", () => {
it("runs the test function, if given a value", () => {
const test = optional((x: number) => x === 1);
expect(test(1)).toBe(true);
expect(test(2)).toBe(false);
});
it("returns true if the passed value is undefined, and false if the value is null", () => {
const test = optional((x: number) => x === 1);
expect(test(2)).toBe(false);
expect(test(null)).toBe(false);
expect(test(undefined)).toBe(true);
});
});
describe("[Operators] nullable", () => {
it("runs the test function, if given a value", () => {
const test = nullable((x: number) => x === 1);
expect(test(1)).toBe(true);
expect(test(2)).toBe(false);
});
it("returns true if the passed value is null, and false if the value is undefined", () => {
const test = nullable((x: number) => x === 1);
expect(test(2)).toBe(false);
expect(test(null)).toBe(true);
expect(test(undefined)).toBe(false);
});
});
describe("[Operators] erratic", () => {
it("runs the test function, if given a value", () => {
const test = erratic((x: number) => x === 1);
expect(test(1)).toBe(true);
expect(test(2)).toBe(false);
});
it("returns true if the passed value is null or undefined", () => {
const test = erratic((x: number) => x === 1);
expect(test(2)).toBe(false);
expect(test(null)).toBe(true);
expect(test(undefined)).toBe(true);
});
});
describe("[Operators] and", () => {
it("runs a series of tests on the same value, and passes only if the value exists, and every test passes (short-circuit on fail)", () => {
const test1 = (x: number) => x > 5;
const test2 = (x: number) => x < 10;
const test3 = (x: number) => !(x % 3);
const suite1 = and(test1, test2, test3);
const suite2 = and(test1, test2);
const input1 = 3;
const input2 = 6;
const input3 = 7;
const input4 = 20;
const input5 = null;
expect(suite1(input1)).toBe(false);
expect(suite1(input2)).toBe(true);
expect(suite1(input3)).toBe(false);
expect(suite1(input4)).toBe(false);
expect(suite1(input5)).toBe(false);
expect(suite2(input1)).toBe(false);
expect(suite2(input2)).toBe(true);
expect(suite2(input3)).toBe(true);
expect(suite2(input4)).toBe(false);
expect(suite2(input5)).toBe(false);
});
});
describe("[Operators] or", () => {
it("runs a series of tests on the same value, and passes if the value exists, and any of the tests pass (short-circuit on success)", () => {
const test1 = (x: number) => 5 < x && x < 10;
const test2 = (x: number) => !(x % 3);
const suite1 = or(test1, test2);
const input1 = 3;
const input2 = 6;
const input3 = 7;
const input4 = 20;
const input5 = null;
expect(suite1(input1)).toBe(true);
expect(suite1(input2)).toBe(true);
expect(suite1(input3)).toBe(true);
expect(suite1(input4)).toBe(false);
expect(suite1(input5)).toBe(false);
});
});
describe("[Operators] compose", () => {
it("is an alias for `Operators.and`", () => {
expect(compose).toBe(and);
});
});
describe("[Operators] customTest", () => {
it("will return truthy if the test returns a truthy value", () => {
expect(customTest(x => typeof x === "number")(5)).toBe(true);
});
it("will not throw on null or undefined", () => {
//@ts-ignore
const hasLength = customTest(x => !!x.length);
expect(hasLength("1")).toBe(true);
expect(hasLength(null)).toBe(false);
});
});
describe("[Operators] unsafeTest", () => {
it("will return truthy if the test returns a truthy value", () => {
const isNumber = unsafeTest(x => typeof x === "number");
expect(isNumber(5)).toBe(true);
});
it("will throw if null or undefined are unhandled", () => {
//@ts-ignore
const hasLength = unsafeTest(x => x.length);
expect(() => hasLength(null)).toThrow();
});
});