|
| 1 | +import { ResultAsync } from '@/src/resultAsync'; |
| 2 | + |
| 3 | +describe('ResultAsync', () => { |
| 4 | + describe('tapEither', () => { |
| 5 | + test('executes the action with a failed ResultAsync', async () => { |
| 6 | + let wasCalled = false; |
| 7 | + const sut = ResultAsync.failure<number>('error'); |
| 8 | + |
| 9 | + const result = await sut |
| 10 | + .tapEither(() => { |
| 11 | + wasCalled = true; |
| 12 | + }) |
| 13 | + .toPromise(); |
| 14 | + |
| 15 | + expect(result).toFailWith('error'); |
| 16 | + expect(wasCalled).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + test('executes the action with a successful ResultAsync', async () => { |
| 20 | + const value = 1; |
| 21 | + let wasCalled = false; |
| 22 | + |
| 23 | + const sut = ResultAsync.success(value); |
| 24 | + |
| 25 | + const result = await sut |
| 26 | + .tapEither(() => { |
| 27 | + wasCalled = true; |
| 28 | + }) |
| 29 | + .toPromise(); |
| 30 | + |
| 31 | + expect(result).toSucceedWith(1); |
| 32 | + expect(wasCalled).toBe(true); |
| 33 | + }); |
| 34 | + |
| 35 | + test('executes the async action with a successful ResultAsync', async () => { |
| 36 | + const value = 1; |
| 37 | + let wasCalled = false; |
| 38 | + |
| 39 | + const sut = ResultAsync.success(value); |
| 40 | + |
| 41 | + const result = await sut |
| 42 | + .tapEither(() => { |
| 43 | + wasCalled = true; |
| 44 | + |
| 45 | + return Promise.resolve(); |
| 46 | + }) |
| 47 | + .toPromise(); |
| 48 | + |
| 49 | + expect(result).toSucceedWith(1); |
| 50 | + expect(wasCalled).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + test('executes the async action with a failed ResultAsync', async () => { |
| 54 | + const error = 'error'; |
| 55 | + let wasCalled = false; |
| 56 | + |
| 57 | + const sut = ResultAsync.failure(error); |
| 58 | + |
| 59 | + const result = await sut |
| 60 | + .tapEither(() => { |
| 61 | + wasCalled = true; |
| 62 | + |
| 63 | + return Promise.resolve(); |
| 64 | + }) |
| 65 | + .toPromise(); |
| 66 | + |
| 67 | + expect(result).toFailWith(error); |
| 68 | + expect(wasCalled).toBe(true); |
| 69 | + }); |
| 70 | + }); |
| 71 | +}); |
0 commit comments