Skip to content

Commit

Permalink
withOverriddenFail must mark result as processed
Browse files Browse the repository at this point in the history
  • Loading branch information
vladbasin committed Jul 2, 2024
1 parent 5094aac commit 96dd34e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/Result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export class Result<T> {
}

/**
* Overrides current error
* Overrides current error. Marks it as processed, so that subsequent processing calls don't override it
* @param value New error
* @returns New Result which stores new error
*/
Expand All @@ -316,12 +316,12 @@ export class Result<T> {
}

/**
* Overrides current error
* Overrides current error. Marks it as processed, so that subsequent processing calls don't override it
* @param value New error
* @returns New Result which stores new error
*/
public withOverriddenFailError(newError: Error): Result<T> {
return this.onFailureCompensate(_ => Result.FailWithError(newError));
return this.onFailureCompensate(_ => Result.FailAsProcessedWithError(newError));
}

/**
Expand Down
60 changes: 56 additions & 4 deletions tests/Result.withOverriddenFail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,71 @@ import { Result } from '../src/Result';
import { executeResult } from './executeResult';

describe('.withOverriddenFail()', () => {
test('overrides value', done => {
const record = jest.fn();
test('overrides error and marks it as processed', done => {
const failCall = jest.fn();
const processFailCall = jest.fn();

executeResult(
done,
Result.Fail('error')
.withOverriddenFail('newError')
.onFailure(error => {
expect(error).toBe('newError');
record();
failCall();
})
.withProcessedFail(() => {
processFailCall();
return 'processed';
}),
() => {
expect(record).toBeCalledTimes(1);
expect(failCall).toBeCalledTimes(1);
expect(processFailCall).toBeCalledTimes(0);
}
);
});


test('overrides even processed error', done => {
const failCall = jest.fn();
const processFailCall = jest.fn();

executeResult(
done,
Result.Fail('error')
.withProcessedFail(() => {
processFailCall();
return 'processed1';
})
.withOverriddenFail('overridden')
.withProcessedFail(() => {
processFailCall();
return 'processed2';
})
.onFailure(error => {
expect(error).toBe('overridden');
failCall();
}),
() => {
expect(failCall).toBeCalledTimes(1);
expect(processFailCall).toBeCalledTimes(1);
}
);
});

test('overrides overridden error', done => {
const failCall = jest.fn();

executeResult(
done,
Result.Fail('error')
.withOverriddenFail('overridden1')
.withOverriddenFail('overridden2')
.onFailure(error => {
expect(error).toBe('overridden2');
failCall();
}),
() => {
expect(failCall).toBeCalledTimes(1);
}
);
});
Expand Down
59 changes: 55 additions & 4 deletions tests/Result.withOverriddenFailError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,70 @@ import { Result } from '../src/Result';
import { executeResult } from './executeResult';

describe('.withOverriddenFailError()', () => {
test('overrides value', done => {
const record = jest.fn();
test('overrides error and marks as processed', done => {
const failCall = jest.fn();
const processFailCall = jest.fn();

executeResult(
done,
Result.Fail('error')
.withOverriddenFailError(new Error('newError'))
.onFailure(error => {
expect(error).toBe('newError');
record();
failCall();
})
.withProcessedFail(() => {
processFailCall();
return 'processed';
}),
() => {
expect(record).toBeCalledTimes(1);
expect(failCall).toBeCalledTimes(1);
expect(processFailCall).toBeCalledTimes(0);
}
);
});

test('overrides even processed error', done => {
const failCall = jest.fn();
const processFailCall = jest.fn();

executeResult(
done,
Result.Fail('error')
.withProcessedFail(() => {
processFailCall();
return 'processed1';
})
.withOverriddenFailError(new Error('overridden'))
.withProcessedFail(() => {
processFailCall();
return 'processed2';
})
.onFailure(error => {
expect(error).toBe('overridden');
failCall();
}),
() => {
expect(failCall).toBeCalledTimes(1);
expect(processFailCall).toBeCalledTimes(1);
}
);
});

test('overrides overridden error', done => {
const failCall = jest.fn();

executeResult(
done,
Result.Fail('error')
.withOverriddenFailError(new Error('overridden1'))
.withOverriddenFailError(new Error('overridden2'))
.onFailure(error => {
expect(error).toBe('overridden2');
failCall();
}),
() => {
expect(failCall).toBeCalledTimes(1);
}
);
});
Expand Down

0 comments on commit 96dd34e

Please sign in to comment.