Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active December 14, 2025 17:10
Show Gist options
  • Select an option

  • Save trikitrok/37f919245981b4b3c72b33b6bd572443 to your computer and use it in GitHub Desktop.

Select an option

Save trikitrok/37f919245981b4b3c72b33b6bd572443 to your computer and use it in GitHub Desktop.
// some imports
describe('AcmeCompanyApi', () => {
// some declarations
beforeEach(() => {
forGettingCauses = {
getAllFor: jest.fn<Promise<Cause[]>, [Claim, AuthToken]>()
};
forOpeningClaim = {
open: jest.fn<Promise<string>, [Claim, Cause, AuthToken]>()
};
authTokenRetriever = {
retrieveToken: jest.fn<Promise<AuthToken>, []>()
};
authToken = {token: 'mi_token', type: 'Bearer'};
api = createApi(forGettingCauses, forOpeningClaim, authTokenRetriever);
claim = aClaim(causeCodeInClaim);
});
it('should fail when token retrieval throws error', async () => {
when(authTokenRetriever.retrieveToken)
.calledWith()
.mockRejectedValue(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(authTokenRetriever.retrieveToken)
.calledWith()
.mockResolvedValue(authToken);
when(forGettingCauses.getAllFor)
.calledWith(claim, authToken)
.mockRejectedValue(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 () => {
const cause = new Cause('code not used in claim');
when(authTokenRetriever.retrieveToken)
.calledWith()
.mockResolvedValue(authToken);
when(forGettingCauses.getAllFor)
.calledWith(claim, authToken)
.mockResolvedValue([cause]);
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 () => {
const matchingCause = new Cause(causeCodeInClaim);
when(authTokenRetriever.retrieveToken)
.calledWith()
.mockResolvedValue(authToken);
when(forGettingCauses.getAllFor)
.calledWith(claim, authToken)
.mockResolvedValue([matchingCause]);
when(forOpeningClaim.open)
.calledWith(claim, matchingCause, authToken)
.mockRejectedValue(new CannotOpenClaimError('Cannot open claim'));
const result = await api.open(claim);
expect(result).toEqual(new FailedOpening(
claim,
'Acme API: cannot open claim 123456789'
));
});
// some helper functions
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment