Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save trikitrok/69ff5e9cd657941e441160c41b4cc588 to your computer and use it in GitHub Desktop.

Select an option

Save trikitrok/69ff5e9cd657941e441160c41b4cc588 to your computer and use it in GitHub Desktop.
describe('AcmeCompanyApi', () => {
// some declarations
beforeEach(() => {
decoratedApi = {
open: jest.fn<Promise<OpeningResult>, [Claim]>()
};
api = new WithErrorHandlingCompanyApi(decoratedApi);
claim = aClaim(causeCodeInClaim);
});
it('should fail when token retrieval throws error', async () => {
when(decoratedApi.open)
.calledWith(claim)
.mockImplementation((_) => {
throw new CannotRetrieveTokenError('Cannot retrieve token')
});
const result = await api.open(claim);
expect(result).toEqual(new FailedOpening(
claim,
'Acme API: failure retrieving token'
));
});
it('should fail when getting causes throws error', async () => {
when(decoratedApi.open)
.calledWith(claim)
.mockImplementation((_) => {
throw new CannotGetCausesError('Cannot retrieve causes')
});
const result = await api.open(claim);
expect(result).toEqual(new FailedOpening(
claim,
'Acme API: failure getting claim causes'
));
});
it('should fail when finding matching cause throws error', async () => {
when(decoratedApi.open)
.calledWith(claim)
.mockImplementation((_) => {
throw new CannotFindMatchingCauseError()
});
const result = await api.open(claim);
expect(result).toEqual(new FailedOpening(
claim,
'Acme API: cannot find cause for claim 123456789'
));
});
it('should fail when opening claim throws error', async () => {
when(decoratedApi.open)
.calledWith(claim)
.mockImplementation((_) => {
throw new CannotOpenClaimError('Cannot open claim')
});
const result = await api.open(claim);
expect(result).toEqual(new FailedOpening(
claim,
'Acme API: cannot open claim 123456789'
));
});
it('should fail when an unknown error occurs', async () => {
const unknownError = new Error('Unexpected error');
when(decoratedApi.open)
.calledWith(claim)
.mockImplementation((_) => {
throw unknownError;
});
const result = await api.open(claim);
expect(result).toEqual(new FailedOpening(
claim,
'Acme API: Unexpected error'
));
});
function aClaim(causeCode: string): Claim {
return new Claim(
'123456789',
'123456789',
new Date('2021-01-01'),
'Test claim',
new Customer('Pepe', '+34668522001'),
causeCode
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment