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