|
| 1 | +import { Result } from '@/src/result'; |
| 2 | +import { ResultAsync } from '../../src'; |
| 3 | + |
| 4 | +describe('Result', () => { |
| 5 | + describe('bindFailureAsync', () => { |
| 6 | + describe('Promise', () => { |
| 7 | + test('takes the result from the second result, when previous result fails', async () => { |
| 8 | + const sut = Result.failure('💥'); |
| 9 | + |
| 10 | + const innerResult = await sut |
| 11 | + .bindFailureAsync(() => Promise.resolve(Result.success('✅'))) |
| 12 | + .toPromise(); |
| 13 | + |
| 14 | + return expect(innerResult).toSucceedWith('✅'); |
| 15 | + }); |
| 16 | + |
| 17 | + test('takes the result from the first result, when it succeeds', async () => { |
| 18 | + const sut = Result.success('✅'); |
| 19 | + |
| 20 | + const innerResult = await sut |
| 21 | + .bindFailureAsync(() => Promise.resolve(Result.failure('💥'))) |
| 22 | + .toPromise(); |
| 23 | + |
| 24 | + return expect(innerResult).toSucceedWith('✅'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('takes the failure from the second result, when both fail', async () => { |
| 28 | + const sut = Result.failure('💥'); |
| 29 | + |
| 30 | + const innerResult = await sut |
| 31 | + .bindFailureAsync(() => Promise.resolve(Result.failure('💥💥'))) |
| 32 | + .toPromise(); |
| 33 | + |
| 34 | + return expect(innerResult).toFailWith('💥💥'); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('ResultAsync', () => { |
| 39 | + test('takes the result from the second result, when previous result fails', async () => { |
| 40 | + const sut = Result.failure('💥'); |
| 41 | + |
| 42 | + const innerResult = await sut |
| 43 | + .bindFailureAsync(() => ResultAsync.success('✅')) |
| 44 | + .toPromise(); |
| 45 | + |
| 46 | + return expect(innerResult).toSucceedWith('✅'); |
| 47 | + }); |
| 48 | + |
| 49 | + test('takes the result from the first result, when it succeeds', async () => { |
| 50 | + const sut = Result.success('✅'); |
| 51 | + |
| 52 | + const innerResult = await sut |
| 53 | + .bindFailureAsync(() => ResultAsync.failure('💥')) |
| 54 | + .toPromise(); |
| 55 | + |
| 56 | + return expect(innerResult).toSucceedWith('✅'); |
| 57 | + }); |
| 58 | + |
| 59 | + test('takes the failure from the second result, when both fail', async () => { |
| 60 | + const sut = Result.failure('💥'); |
| 61 | + |
| 62 | + const innerResult = await sut |
| 63 | + .bindFailureAsync(() => ResultAsync.failure('💥💥')) |
| 64 | + .toPromise(); |
| 65 | + |
| 66 | + return expect(innerResult).toFailWith('💥💥'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + }); |
| 70 | +}); |
0 commit comments