-
Notifications
You must be signed in to change notification settings - Fork 4
/
ensure.spec.ts
58 lines (41 loc) · 1.54 KB
/
ensure.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
import { ResultAsync } from '@/src/resultAsync';
describe('ResultAsync', () => {
describe('ensure', () => {
test('calls the predicate and returns a successful Result with a successful ResultAsync', async () => {
const value = 1;
const sut = ResultAsync.success(value);
const result = await sut.ensure((num) => num >= 1, 'error').toPromise();
expect(result).toSucceedWith(value);
});
test('does not execute the predicate for a failed ResultAsync', async () => {
const error = 'error';
let wasCalled = false;
const sut = ResultAsync.failure(error);
const result = await sut
.ensure((_val) => {
wasCalled = true;
return true;
}, 'handled')
.toPromise();
expect(result).toFailWith(error);
expect(wasCalled).toBe(false);
});
test('returns a ResultAsync with a successful Result and predicate that returns false with an error value', async () => {
const value = 1;
const sut = ResultAsync.success(value);
const result = await sut.ensure((num) => num >= 2, 'error').toPromise();
expect(result).toFailWith('error');
});
test('returns a ResultAsync with a successful Result and predicate that returns false with an error creator', async () => {
const value = 1;
const sut = ResultAsync.success(value);
const result = await sut
.ensure(
(num) => num >= 2,
(v) => `error ${v}`
)
.toPromise();
expect(result).toFailWith('error 1');
});
});
});