-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbindAsync.spec.ts
62 lines (50 loc) · 1.83 KB
/
bindAsync.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
import { Result } from '@/src/result';
import { ResultAsync } from '@/src/resultAsync';
describe('Result', () => {
describe('bindAsync', () => {
describe('promise', () => {
test('will call the projection function when the original Result succeeds', async () => {
const sut = Result.success(1);
const result = await sut
.bindAsync((number) => Promise.resolve(Result.success(1 + number)))
.toPromise();
expect(result).toSucceedWith(2);
});
test('will not call the projection function when the original Result fails', async () => {
const error = 'error';
let wasCalled = false;
const sut = Result.failure<number>(error);
const result = await sut
.bindAsync((number) => {
wasCalled = true;
return Promise.resolve(Result.success(1 + number));
})
.toPromise();
expect(result).toFailWith(error);
expect(wasCalled).toBe(false);
});
});
describe('ResultAsync', () => {
test('will call the projection function when the original Result succeeds', async () => {
const sut = Result.success(1);
const result = await sut
.bindAsync((number) => ResultAsync.success(1 + number))
.toPromise();
expect(result).toSucceedWith(2);
});
test('will not call the projection function when the original Result fails', async () => {
const error = 'error';
let wasCalled = false;
const sut = Result.failure<number>(error);
const result = await sut
.bindAsync((number) => {
wasCalled = true;
return ResultAsync.success(1 + number);
})
.toPromise();
expect(result).toFailWith(error);
expect(wasCalled).toBe(false);
});
});
});
});