-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathonSuccessTryAsync.spec.ts
65 lines (51 loc) · 1.54 KB
/
onSuccessTryAsync.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
import { Result } from '@/src/result';
describe('Result', () => {
describe('onSuccessTryAsync', () => {
test('will perform no action for a failed Result', async () => {
const error = 'ouch';
const sut = Result.failure<number>(error);
let wasCalled = false;
const innerResult = await sut
.onSuccessTryAsync(
(_v) => {
wasCalled = true;
return Promise.resolve();
},
(_) => 'fail'
)
.toPromise();
expect(innerResult).toFailWith(error);
expect(wasCalled).toBe(false);
});
test('will execute the action for a successful Result', async () => {
let wasCalled = false;
const sut = Result.success(1);
const innerResult = await sut
.onSuccessTryAsync(
(_v) => {
wasCalled = true;
return Promise.resolve();
},
(_) => 'fail'
)
.toPromise();
expect(innerResult).toSucceedWith(1);
expect(wasCalled).toBe(true);
});
test('will execute the action and convert the thrown error for a successful Result', async () => {
let wasCalled = false;
const sut = Result.success(1);
const innerResult = await sut
.onSuccessTryAsync(
() => {
wasCalled = true;
throw new Error('boom');
},
(e) => (e instanceof Error ? e.message : 'fail')
)
.toPromise();
expect(innerResult).toFailWith('boom');
expect(wasCalled).toBe(true);
});
});
});