-
Notifications
You must be signed in to change notification settings - Fork 4
/
try.spec.ts
62 lines (48 loc) · 1.67 KB
/
try.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 { ResultAsync } from '@/src/resultAsync';
describe('Result', () => {
describe('try', () => {
describe('action', () => {
test('will return a failed Result from a thrown Error', async () => {
const errorMessage = 'ouch';
const action = () => {
throw new Error(errorMessage);
};
const sut = await ResultAsync.try(action, (e) =>
e instanceof Error ? e.message : `${e}`
).toPromise();
expect(sut).toFailWith(errorMessage);
});
test('will return a successful Result from an action', async () => {
let wasCalled = false;
const action = () => {
wasCalled = true;
return Promise.resolve();
};
const sut = await ResultAsync.try(action, (e) => 'error').toPromise();
expect(sut).toSucceed();
expect(wasCalled).toBe(true);
});
});
describe('factory', () => {
test('will return a failed Result from a thrown Error', async () => {
const errorMessage = 'ouch';
const throwError = () => {
throw new Error(errorMessage);
};
const factory = () => {
throwError();
return Promise.resolve(1);
};
const sut = await ResultAsync.try(factory, (e) =>
e instanceof Error ? e.message : `${e}`
).toPromise();
expect(sut).toFailWith(errorMessage);
});
test('will create a successful Result with the given value', async () => {
const factory = () => Promise.resolve(1);
const sut = await ResultAsync.try(factory, (e) => `${e}`).toPromise();
expect(sut).toSucceedWith(1);
});
});
});
});