-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathonSuccessTry.spec.ts
More file actions
98 lines (80 loc) · 2.79 KB
/
onSuccessTry.spec.ts
File metadata and controls
98 lines (80 loc) · 2.79 KB
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
import { ResultAsync } from '@/src/resultAsync';
import { ActionOfT } from '@/src/utilities';
describe('ResultAsync', () => {
describe('onSuccessTry', () => {
test('does not call the action with a failed ResultAsync', async () => {
let wasCalled = false;
const sut = ResultAsync.failure('error');
const result = await sut
.onSuccessTry(
() => (wasCalled = true),
(e) => `handled: ${e}`
)
.toPromise();
expect(result).toFailWith('error');
expect(wasCalled).toBe(false);
});
describe('action', () => {
test('calls the action and returns a successful ResultAsync when the action does not throw', async () => {
let wasCalled = false;
const sut = ResultAsync.success(1);
const result = await sut
.onSuccessTry(
() => (wasCalled = true),
(e) => `handled: ${e}`
)
.toPromise();
expect(result).toSucceedWith(1);
expect(wasCalled).toBe(true);
});
test('calls the action and returns a failed ResultAsync when the action throws', async () => {
let wasCalled = false;
const sut = ResultAsync.success(1);
// the thrown error confuses the type system, so we use an explicitly typed function to select the correct overload
const action: ActionOfT<number> = () => {
wasCalled = true;
throw new Error('error');
};
const result = await sut
.onSuccessTry(action, (e) =>
e instanceof Error ? `handled: ${e.message}` : 'handled'
)
.toPromise();
expect(result).toFailWith('handled: error');
expect(wasCalled).toBe(true);
});
});
describe('async action', () => {
test('calls the async action and returns a successful ResultAsync when the action does not throw', async () => {
let wasCalled = false;
const sut = ResultAsync.success(1);
const result = await sut
.onSuccessTry(
() => {
wasCalled = true;
return Promise.resolve();
},
(e) => `handled: ${e}`
)
.toPromise();
expect(result).toSucceedWith(1);
expect(wasCalled).toBe(true);
});
test('calls the async action and returns a failed ResultAsync when the Promise rejects', async () => {
let wasCalled = false;
const sut = ResultAsync.success(1);
const result = await sut
.onSuccessTry(
() => {
wasCalled = true;
return Promise.reject('error');
},
(e) => `handled: ${e}`
)
.toPromise();
expect(result).toFailWith('handled: error');
expect(wasCalled).toBe(true);
});
});
});
});