-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.ts
119 lines (112 loc) · 3.66 KB
/
index.spec.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
import {record, str, num, bool, nil, exactly, arrayOf, RecordSpec} from './index';
import {getOrElse} from 'fp-ts/lib/Option';
describe('str', () => {
const failString = 'fail';
it('passes for normal string', () => {
expect(getOrElse(() => failString)(str('a'))).toBe('a');
});
it('passes for empty string', () => {
expect(getOrElse(() => failString)(str(''))).toBe('');
});
it('fails for non-string', () => {
expect(getOrElse(() => failString)(str(1))).toBe(failString);
});
});
describe('num', () => {
const failNum = -1;
it('passes for normal number', () => {
expect(getOrElse(() => failNum)(num(1))).toBe(1);
});
it('passes for 0', () => {
expect(getOrElse(() => failNum)(num(0))).toBe(0);
});
it('passes for NaN', () => {
expect(getOrElse(() => failNum)(num(NaN))).toBe(NaN);
});
it('fails for non-string', () => {
expect(getOrElse(() => failNum)(num('a'))).toBe(failNum);
});
});
describe('bool', () => {
it('passes for true', () => {
expect(getOrElse(() => false)(bool(true))).toBe(true);
});
it('passes for false', () => {
expect(getOrElse(() => true)(bool(false))).toBe(false);
});
it('fails for string', () => {
expect(getOrElse(() => false)(bool('hi'))).toBe(false);
});
it('fails for null', () => {
expect(getOrElse(() => false)(bool(null))).toBe(false);
});
it('fails for undefined', () => {
expect(getOrElse(() => false)(bool(undefined))).toBe(false);
});
it('fails for 0', () => {
expect(getOrElse(() => false)(bool(0))).toBe(false);
});
it('fails for empty-string', () => {
expect(getOrElse(() => false)(bool(''))).toBe(false);
});
});
describe('nil', () => {
it('passes for null', () => {
expect(getOrElse((): null | true => true)(nil(null))).toBe(null);
});
it('fails otherwise', () => {
expect(getOrElse((): null | true => true)(nil(undefined))).toEqual(true);
expect(getOrElse((): null | true => true)(nil(1))).toBe(true);
});
});
describe('exactly', () => {
it('passes for same value', () => {
expect(getOrElse(() => 'fail')(exactly('pass')('pass'))).toBe('pass');
});
it('fails otherwise', () => {
expect(getOrElse(() => 'fail')(exactly('pass')('Pass'))).toBe('fail');
});
});
describe('arrayOf', () => {
const failArr = [0];
it('fails for non-array', () => {
expect(getOrElse(() => failArr)(arrayOf(num)(1))).toEqual(failArr);
});
it('fails for internal type mismatch', () => {
expect(getOrElse(() => failArr)(arrayOf(num)(['a', 1]))).toEqual(failArr);
});
it('pass for empty', () => {
expect(getOrElse(() => failArr)(arrayOf(num)([]))).toEqual([]);
});
it('pass for match', () => {
expect(getOrElse(() => failArr)(arrayOf(num)([1, 2]))).toEqual([1, 2]);
});
});
describe('record', () => {
interface Rec {
num: number;
str: string;
arr: number[];
bool: boolean;
}
const rec: RecordSpec<Rec> = {
num: num,
str: str,
arr: arrayOf(num),
bool: bool,
};
const checkRec = getOrElse((): Rec | null => null);
it('fails for not object', () => {
expect(checkRec(record(rec)(null))).toBe(null);
});
it('fails for empty', () => {
expect(checkRec(record(rec)({}))).toBe(null);
});
it('fails for partial', () => {
expect(checkRec(record(rec)({num: 1}))).toBe(null);
});
it('pass for full', () => {
const v = {num: 1, str: 'hi', arr: [1], bool: false};
expect(checkRec(record(rec)(v))).toEqual(v);
});
});