-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.js
390 lines (334 loc) · 11.3 KB
/
index.test.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import R from 'ramda';
import {union, matchWith, Maybe, Result, Task} from './index';
const unionEquals = val1 => val2 => val1.equals(val2);
describe('union', () => {
const Perhaps = union({
Something: 1,
None: 0,
});
describe('create', () => {
test('a Something has the correct tag.', () => {
expect(Perhaps.Something(42)).toEqual(expect.objectContaining({
_ctor: 'Something',
}));
});
test('matches a Something with a value.', () => {
const perhaps = Perhaps.Something(42);
const matched = matchWith(perhaps, {
Something: val => val,
None: Function.prototype,
});
expect(matched).toEqual(42);
});
test('a None has the correct tag.', () => {
expect(Perhaps.None()).toEqual(expect.objectContaining({
_ctor: 'None',
}));
});
test('matches a None without any values.', () => {
const perhaps = Perhaps.None();
const matched = matchWith(perhaps, {
Something: Function.prototype,
None: () => 'I am none',
});
expect(matched).toEqual('I am none');
});
test('throws when matching a non-union type.', () => {
const badFn = () => matchWith('not a union', {});
expect(badFn).toThrow();
});
test('creates curried constructors', () => {
const Point = union({Point: 2});
const Color = union({RGB: 3});
const Bool = union({True: 0, False: 0});
expect(Color.RGB(255, 0, 0)).toSatisfy(
unionEquals(Color.RGB(255)(0)(0))
);
expect(Point.Point(1, 0)).toSatisfy(unionEquals(Point.Point(1)(0)));
expect(Bool.True()).toSatisfy(unionEquals(Bool.True()));
});
});
describe('prototype', () => {
test('Something and None have the same constructor.', () => {
const none = Perhaps.None();
const something = Perhaps.Something(42);
expect(none.constructor).toEqual(something.constructor);
});
test('Maybe and Perhaps have different prototypes.', () => {
expect(Perhaps.prototype).not.toEqual(Maybe.prototype);
});
describe('equals', () => {
const something = Perhaps.Something(42);
test('should equal when the value is the same', () => {
expect(something).toSatisfy(unionEquals(Perhaps.Something(42)));
});
test('should not be equal when the value is the different', () => {
expect(something).not.toSatisfy(unionEquals(Perhaps.Something(3)));
});
test('should not be equal when the tag is different', () => {
expect(something).not.toSatisfy(unionEquals(Perhaps.None()));
});
test('also works on nested arrays', () => {
const someArray = Perhaps.Something([1, 2, [3, 4]]);
expect(someArray).toSatisfy(unionEquals(Perhaps.Something([1, 2, [3, 4]])));
});
test('also works on nested objects', () => {
const someObject = Perhaps.Something({a: {b: 3}});
expect(someObject).toSatisfy(unionEquals(Perhaps.Something({a: {b: 3}})));
});
});
});
describe('matchWith', () => {
const cases = {
Just: val => val,
Nothing: () => 'default',
};
test('works on a Just value', () => {
expect(matchWith(Maybe.Just('Hello'), cases)).toEqual('Hello');
});
test('works on a Nothing value', () => {
expect(matchWith(Maybe.Nothing(), cases)).toEqual('default');
});
});
});
describe('Maybe', () => {
test('is Just in the minimum default context', () => {
expect(Maybe.of(3)).toSatisfy(unionEquals(Maybe.Just(3)));
});
describe('Just', () => {
test('creates a Just value', () => {
const maybe = Maybe.Just(42);
expect(maybe).toEqual(expect.objectContaining({
_ctor: 'Just',
_args: [42],
}));
});
});
describe('Nothing', () => {
test('creates a Nothing value', () => {
const maybe = Maybe.Nothing();
expect(maybe).toEqual(expect.objectContaining({
_ctor: 'Nothing',
_args: [],
}));
});
});
describe('map', () => {
const double = R.multiply(2);
test("maps when it's just", () => {
const maybe = Maybe.Just(10);
expect(maybe.map(double)).toSatisfy(unionEquals(Maybe.Just(20)));
});
test("skips when it's nothing", () => {
const maybe = Maybe.Nothing();
expect(maybe.map(double)).toSatisfy(unionEquals(maybe));
});
});
describe('chain', () => {
const listHead = list =>
list.length === 0 ? Maybe.Nothing() : Maybe.Just(list[0]);
test('maps and flattens', () => {
const maybe = Maybe.Just([1, 2]);
expect(maybe.chain(listHead)).toSatisfy(unionEquals(Maybe.Just(1)));
});
test("skips when it's nothing", () => {
const maybe = Maybe.Nothing();
expect(maybe.chain(listHead)).toSatisfy(unionEquals(Maybe.Nothing()));
});
});
describe('ap', () => {
test('applies the argument to a function', () => {
const add = a => b => a + b;
const maybeFn = Maybe.Just(add);
expect(maybeFn.ap(Maybe.Just(3)).ap(Maybe.Just(2))).toSatisfy(
unionEquals(Maybe.Just(5))
);
});
test('does nothing if function is Nothing', () => {
const add = a => b => a + b;
const maybeFn = Maybe.Nothing();
expect(maybeFn.ap(Maybe.Just(3)).ap(Maybe.Just(2))).toSatisfy(
unionEquals(Maybe.Nothing())
);
});
test('does nothing if value is Nothing', () => {
const add = a => b => a + b;
const maybeFn = Maybe.Just(add);
expect(maybeFn.ap(Maybe.Nothing())).toSatisfy(
unionEquals(Maybe.Nothing())
);
});
});
});
describe('Result', () => {
const cases = {
Err: err => err,
Ok: val => val,
};
test('matches Err', () => {
const result = Result.Err('I am err');
expect(matchWith(result, cases)).toEqual('I am err');
});
test('matches Ok', () => {
const result = Result.Ok('I am ok');
expect(matchWith(result, cases)).toEqual('I am ok');
});
test('is Ok in the minimum default context', () => {
expect(Result.of(3)).toSatisfy(unionEquals(Result.Ok(3)));
});
describe('.map', () => {
const double = val => 2 * val;
test("maps when it's Ok", () => {
expect(Result.Ok(3).map(double)).toSatisfy(unionEquals(Result.Ok(6)));
});
test("skips when it's Err", () => {
expect(Result.Err('Oh no').map(double)).toSatisfy(
unionEquals(Result.Err('Oh no'))
);
});
});
describe('.mapError', () => {
const toUpper = str => str.toUpperCase();
test("skips when it's Ok", () => {
expect(Result.Ok(3).mapError(toUpper)).toSatisfy(
unionEquals(Result.Ok(3))
);
});
test("maps the error when it's Err", () => {
expect(Result.Err('Oh no').mapError(toUpper)).toSatisfy(
unionEquals(Result.Err('OH NO'))
);
});
});
describe('.chain', () => {
const safeJsonParse = val => {
try {
return Result.Ok(JSON.parse(val));
} catch (e) {
return Result.Err(e);
}
};
test("maps and flattens when it's Ok", () => {
expect(Result.Ok('{ "a": 3 }').chain(safeJsonParse)).toSatisfy(
unionEquals(Result.Ok({a: 3}))
);
});
test("skips when it's Err", () => {
expect(Result.Err('Oh no').chain(safeJsonParse)).toSatisfy(
unionEquals(Result.Err('Oh no'))
);
});
});
describe('.ap', () => {
test('applies the argument to a function', () => {
const add = a => b => a + b;
const resultFn = Result.Ok(add);
expect(resultFn.ap(Result.Ok(3)).ap(Result.Ok(2))).toSatisfy(
unionEquals(Result.Ok(5))
);
});
test('does nothing if function is Err', () => {
const add = a => b => a + b;
const resultFn = Result.Err('Oh no');
expect(resultFn.ap(Result.Ok(3)).ap(Result.Ok(2))).toSatisfy(
unionEquals(Result.Err('Oh no'))
);
});
test('does nothing if value is Err', () => {
const add = a => b => a + b;
const resultFn = Result.Ok(add);
expect(resultFn.ap(Result.Err('Oh no'))).toSatisfy(
unionEquals(Result.Err('Oh no'))
);
});
});
});
describe('Task', () => {
describe('.succeed', () => {
test('creates a successful task', () => {
const task = Task.succeed(3);
return expect(task.run()).resolves.toBe(3);
});
});
describe('.failed', () => {
test('creates a failed task', () => {
const task = Task.fail('OMG');
return expect(task.run()).rejects.toBe('OMG');
});
});
describe('.all', () => {
test('batches tasks', () => {
const task = Task.all([Task.succeed(3), Task.succeed(5)]);
return expect(task.run()).resolves.toEqual([3, 5]);
});
test('fails if one of them fails', () => {
const task = Task.all([Task.succeed(3), Task.fail('Oh no')]);
return expect(task.run()).rejects.toBe('Oh no');
});
test('can be mapped like any task', () => {
const sum = ([a, b]) => a + b;
const task = Task.all([Task.succeed(3), Task.succeed(5)]).map(sum);
return expect(task.run()).resolves.toBe(8);
});
});
describe('.sequence', () => {
test('executes tasks in sequence', () => {
const task = Task.sequence([Task.succeed(3), Task.succeed(5)]);
return expect(task.run()).resolves.toEqual([3, 5]);
});
test('executes tasks in order from left to right', () => {
const delayTask = Task.of(
() => new Promise(resolve => setTimeout(resolve, 500))
).map(_ => 3);
const task = Task.sequence([delayTask, Task.succeed(5)]);
return expect(task.run()).resolves.toEqual([3, 5]);
});
test('fails if one of them fails', () => {
const task = Task.sequence([Task.succeed(3), Task.fail('Oh no')]);
return expect(task.run()).rejects.toBe('Oh no');
});
});
describe('.prototype', () => {
describe('.map', () => {
test('maps when successful', () => {
const double = a => a * 2;
const task = Task.succeed(42).map(double);
return expect(task.run()).resolves.toBe(84);
});
});
describe('.map2', () => {
test('maps when successful', () => {
const add = (a, b) => a + b;
const task = Task.succeed(42).map2(add, Task.succeed(3));
return expect(task.run()).resolves.toBe(45);
});
test('fails when one of them fails', () => {
const add = (a, b) => a + b;
const task = Task.succeed(42).map2(add, Task.fail('Oh no'));
return expect(task.run()).rejects.toBe('Oh no');
});
});
describe('.mapError', () => {
test('maps the error when failed', () => {
const toLower = str => str.toLowerCase();
const task = Task.fail('OH NO').mapError(toLower);
return expect(task.run()).rejects.toBe('oh no');
});
});
describe('.chain', () => {
test('maps and flattens when successful', () => {
const task = Task.succeed('Hello');
const toConcatTask = val => Task.of(() => val.concat('!!!'));
const chainedTask = task.chain(toConcatTask);
return expect(chainedTask.run()).resolves.toBe('Hello!!!');
});
});
describe('.onError', () => {
test('catches and flattens when failed', () => {
const toConcatTask = val => Task.of(() => val.concat('!!!'));
const task = Task.fail('Oh no').onError(toConcatTask);
return expect(task.run()).resolves.toBe('Oh no!!!');
});
});
});
});