Skip to content

Commit

Permalink
chore: add tests for error classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ernest-okot committed Feb 20, 2019
1 parent 91b86d7 commit 8ff9f65
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function handleError(error: AxiosError): Error {
return getError(code, message);
}

export function getError(code: FailureReason, message?: string) {
export function getError(code?: FailureReason, message?: string) {
if (code === FailureReason.APPROVAL_REJECTED) {
return new ApprovalRejectedError(message);
}
Expand Down
113 changes: 113 additions & 0 deletions test/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { FailureReason } from "../src/common";
import {
ApprovalRejectedError,
ExpiredError,
getError,
InternalProcessingError,
InvalidCallbackUrlHostError,
InvalidCurrencyError,
NotAllowedError,
NotAllowedTargetEnvironmentError,
NotEnoughFundsError,
PayeeNotAllowedToReceiveError,
PayeeNotFoundError,
PayerLimitReachedError,
PayerNotFoundError,
PaymentNotApprovedError,
ResourceAlreadyExistError,
ResourceNotFoundError,
ServiceUnavailableError,
TransactionCancelledError,
UnspecifiedError
} from "../src/errors";
import { expect } from "./chai";

describe("Errors", function() {
describe("getError", function() {
context("when there is no error code", function() {
it("returns unspecified error", function() {
expect(getError()).is.instanceOf(UnspecifiedError);
});
});

context("when there is an error code", function() {
it("returns the correct error", function() {
expect(getError(FailureReason.APPROVAL_REJECTED, "test message"))
.is.instanceOf(ApprovalRejectedError)
.and.has.property("message", "test message");

expect(getError(FailureReason.EXPIRED, "test message"))
.is.instanceOf(ExpiredError)
.and.has.property("message", "test message");

expect(
getError(FailureReason.INTERNAL_PROCESSING_ERROR, "test message")
)
.is.instanceOf(InternalProcessingError)
.and.has.property("message", "test message");

expect(
getError(FailureReason.INVALID_CALLBACK_URL_HOST, "test message")
)
.is.instanceOf(InvalidCallbackUrlHostError)
.and.has.property("message", "test message");

expect(getError(FailureReason.INVALID_CURRENCY, "test message"))
.is.instanceOf(InvalidCurrencyError)
.and.has.property("message", "test message");

expect(getError(FailureReason.NOT_ALLOWED, "test message"))
.is.instanceOf(NotAllowedError)
.and.has.property("message", "test message");

expect(
getError(FailureReason.NOT_ALLOWED_TARGET_ENVIRONMENT, "test message")
)
.is.instanceOf(NotAllowedTargetEnvironmentError)
.and.has.property("message", "test message");

expect(getError(FailureReason.NOT_ENOUGH_FUNDS, "test message"))
.is.instanceOf(NotEnoughFundsError)
.and.has.property("message", "test message");

expect(
getError(FailureReason.PAYEE_NOT_ALLOWED_TO_RECEIVE, "test message")
)
.is.instanceOf(PayeeNotAllowedToReceiveError)
.and.has.property("message", "test message");

expect(getError(FailureReason.PAYEE_NOT_FOUND, "test message"))
.is.instanceOf(PayeeNotFoundError)
.and.has.property("message", "test message");

expect(getError(FailureReason.PAYER_LIMIT_REACHED, "test message"))
.is.instanceOf(PayerLimitReachedError)
.and.has.property("message", "test message");

expect(getError(FailureReason.PAYER_NOT_FOUND, "test message"))
.is.instanceOf(PayerNotFoundError)
.and.has.property("message", "test message");

expect(getError(FailureReason.PAYMENT_NOT_APPROVED, "test message"))
.is.instanceOf(PaymentNotApprovedError)
.and.has.property("message", "test message");

expect(getError(FailureReason.RESOURCE_ALREADY_EXIST, "test message"))
.is.instanceOf(ResourceAlreadyExistError)
.and.has.property("message", "test message");

expect(getError(FailureReason.RESOURCE_NOT_FOUND, "test message"))
.is.instanceOf(ResourceNotFoundError)
.and.has.property("message", "test message");

expect(getError(FailureReason.SERVICE_UNAVAILABLE, "test message"))
.is.instanceOf(ServiceUnavailableError)
.and.has.property("message", "test message");

expect(getError(FailureReason.TRANSACTION_CANCELED, "test message"))
.is.instanceOf(TransactionCancelledError)
.and.has.property("message", "test message");
});
});
});
});

0 comments on commit 8ff9f65

Please sign in to comment.