-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
154 lines (131 loc) · 3.88 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { expect } from 'chai';
import { defaultTo } from 'lodash/fp';
import {
assert,
env,
requires,
requiresIf,
returnsIf,
returnsIfFalsy,
returnsIfTruthy,
} from '../src';
describe('env', () => {
beforeEach(() => {
process.env = {};
});
it('should return env var when no functions is passed', () => {
process.env = { TEST1: 'this is test' };
expect(
env()('TEST1'),
).to.equal('this is test');
});
it('should detect an undefined variable with `requires`', () => {
expect(
() => env(requires)('UNDEFINED_VAR'),
).to.throw('Parsing env var UNDEFINED_VAR failed: Argument undefined');
});
it('should process function chain', () => {
expect(
env(() => 2, (a) => a * 3, (a) => a.toString())('UNDEFINED_VAR'),
).to.equal('6');
});
it('should compatible with other functional utilities', () => {
expect(
env(defaultTo('2'), parseFloat)('UNDEFINED_VAR'),
).to.equal(2);
});
});
describe('requires', () => {
it('should throw error when parameter is undefined', () => {
expect(
() => requires(undefined),
).to.throw('Argument undefined');
});
it('should return given parameter', () => {
expect(
requires('123'),
).to.equal('123');
});
});
describe('requiresIf', () => {
it('should throw error on undefined on test success', () => {
expect(
() => requiresIf(true)(undefined),
).to.throw('Argument undefined');
expect(
() => requiresIf(() => true)(undefined),
).to.throw('Argument undefined');
});
it('should not throw error on undefined on test failure', () => {
expect(
requiresIf(false)(undefined),
).to.equal(undefined);
expect(
requiresIf(() => false)(undefined),
).to.equal(undefined);
});
});
describe('returnsIf', () => {
it('should break function chaining loop and return previous value on test success', () => {
expect(
env(defaultTo('3'), returnsIf(true), parseFloat)('UNDEFINED_VAR'),
).to.equal('3');
expect(
env(defaultTo('3'), returnsIf(() => true), parseFloat)('UNDEFINED_VAR'),
).to.equal('3');
});
it('should continue function chaining loop on test failure', () => {
expect(
env(defaultTo('3'), returnsIf(false), parseFloat)('UNDEFINED_VAR'),
).to.equal(3);
expect(
env(defaultTo('3'), returnsIf(() => false), parseFloat)('UNDEFINED_VAR'),
).to.equal(3);
});
});
describe('returnsIfFalsy', () => {
it('should break function chaining loop and return previous value on falsy value', () => {
expect(
env(defaultTo(''), returnsIfFalsy, () => 2)('UNDEFINED_VAR'),
).to.equal('');
});
it('should continue function chaining loop on truthy value', () => {
expect(
env(defaultTo('3'), returnsIfFalsy, () => 2)('UNDEFINED_VAR'),
).to.equal(2);
});
});
describe('returnsIfTruthy', () => {
it('should break function chaining loop and return previous value on truthy value', () => {
expect(
env(defaultTo('3'), returnsIfTruthy, () => 2)('UNDEFINED_VAR'),
).to.equal('3');
});
it('should continue function chaining loop on falsy value', () => {
expect(
env(defaultTo(''), returnsIfTruthy, () => 2)('UNDEFINED_VAR'),
).to.equal(2);
});
});
describe('assert', () => {
it('should return the first parameter on truthy value', () => {
expect(
env(() => 1, assert(true))('UNDEFINED_VAR'),
).to.equal(1);
});
it('should return the first parameter on succeeded test', () => {
expect(
env(() => 1, assert(() => true))('UNDEFINED_VAR'),
).to.equal(1);
});
it('should throw error on falsy value', () => {
expect(
() => env(() => 1, assert(false))('UNDEFINED_VAR'),
).to.throw('Assertion failed');
});
it('should throw error on failed test', () => {
expect(
() => env(() => 1, assert(() => false))('UNDEFINED_VAR'),
).to.throw('Assertion failed');
});
});