From 8ff9f65b6ec2d78ae8e363653dc33f94f33499b2 Mon Sep 17 00:00:00 2001 From: Ernest Okot Date: Wed, 20 Feb 2019 03:59:17 +0300 Subject: [PATCH] chore: add tests for error classes --- src/errors.ts | 2 +- test/errors.test.ts | 113 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 test/errors.test.ts diff --git a/src/errors.ts b/src/errors.ts index 742fb15..abf47fb 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -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); } diff --git a/test/errors.test.ts b/test/errors.test.ts new file mode 100644 index 0000000..c3ccf14 --- /dev/null +++ b/test/errors.test.ts @@ -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"); + }); + }); + }); +});