Skip to content

Commit 8ff9f65

Browse files
committed
chore: add tests for error classes
1 parent 91b86d7 commit 8ff9f65

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export function handleError(error: AxiosError): Error {
9999
return getError(code, message);
100100
}
101101

102-
export function getError(code: FailureReason, message?: string) {
102+
export function getError(code?: FailureReason, message?: string) {
103103
if (code === FailureReason.APPROVAL_REJECTED) {
104104
return new ApprovalRejectedError(message);
105105
}

test/errors.test.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { FailureReason } from "../src/common";
2+
import {
3+
ApprovalRejectedError,
4+
ExpiredError,
5+
getError,
6+
InternalProcessingError,
7+
InvalidCallbackUrlHostError,
8+
InvalidCurrencyError,
9+
NotAllowedError,
10+
NotAllowedTargetEnvironmentError,
11+
NotEnoughFundsError,
12+
PayeeNotAllowedToReceiveError,
13+
PayeeNotFoundError,
14+
PayerLimitReachedError,
15+
PayerNotFoundError,
16+
PaymentNotApprovedError,
17+
ResourceAlreadyExistError,
18+
ResourceNotFoundError,
19+
ServiceUnavailableError,
20+
TransactionCancelledError,
21+
UnspecifiedError
22+
} from "../src/errors";
23+
import { expect } from "./chai";
24+
25+
describe("Errors", function() {
26+
describe("getError", function() {
27+
context("when there is no error code", function() {
28+
it("returns unspecified error", function() {
29+
expect(getError()).is.instanceOf(UnspecifiedError);
30+
});
31+
});
32+
33+
context("when there is an error code", function() {
34+
it("returns the correct error", function() {
35+
expect(getError(FailureReason.APPROVAL_REJECTED, "test message"))
36+
.is.instanceOf(ApprovalRejectedError)
37+
.and.has.property("message", "test message");
38+
39+
expect(getError(FailureReason.EXPIRED, "test message"))
40+
.is.instanceOf(ExpiredError)
41+
.and.has.property("message", "test message");
42+
43+
expect(
44+
getError(FailureReason.INTERNAL_PROCESSING_ERROR, "test message")
45+
)
46+
.is.instanceOf(InternalProcessingError)
47+
.and.has.property("message", "test message");
48+
49+
expect(
50+
getError(FailureReason.INVALID_CALLBACK_URL_HOST, "test message")
51+
)
52+
.is.instanceOf(InvalidCallbackUrlHostError)
53+
.and.has.property("message", "test message");
54+
55+
expect(getError(FailureReason.INVALID_CURRENCY, "test message"))
56+
.is.instanceOf(InvalidCurrencyError)
57+
.and.has.property("message", "test message");
58+
59+
expect(getError(FailureReason.NOT_ALLOWED, "test message"))
60+
.is.instanceOf(NotAllowedError)
61+
.and.has.property("message", "test message");
62+
63+
expect(
64+
getError(FailureReason.NOT_ALLOWED_TARGET_ENVIRONMENT, "test message")
65+
)
66+
.is.instanceOf(NotAllowedTargetEnvironmentError)
67+
.and.has.property("message", "test message");
68+
69+
expect(getError(FailureReason.NOT_ENOUGH_FUNDS, "test message"))
70+
.is.instanceOf(NotEnoughFundsError)
71+
.and.has.property("message", "test message");
72+
73+
expect(
74+
getError(FailureReason.PAYEE_NOT_ALLOWED_TO_RECEIVE, "test message")
75+
)
76+
.is.instanceOf(PayeeNotAllowedToReceiveError)
77+
.and.has.property("message", "test message");
78+
79+
expect(getError(FailureReason.PAYEE_NOT_FOUND, "test message"))
80+
.is.instanceOf(PayeeNotFoundError)
81+
.and.has.property("message", "test message");
82+
83+
expect(getError(FailureReason.PAYER_LIMIT_REACHED, "test message"))
84+
.is.instanceOf(PayerLimitReachedError)
85+
.and.has.property("message", "test message");
86+
87+
expect(getError(FailureReason.PAYER_NOT_FOUND, "test message"))
88+
.is.instanceOf(PayerNotFoundError)
89+
.and.has.property("message", "test message");
90+
91+
expect(getError(FailureReason.PAYMENT_NOT_APPROVED, "test message"))
92+
.is.instanceOf(PaymentNotApprovedError)
93+
.and.has.property("message", "test message");
94+
95+
expect(getError(FailureReason.RESOURCE_ALREADY_EXIST, "test message"))
96+
.is.instanceOf(ResourceAlreadyExistError)
97+
.and.has.property("message", "test message");
98+
99+
expect(getError(FailureReason.RESOURCE_NOT_FOUND, "test message"))
100+
.is.instanceOf(ResourceNotFoundError)
101+
.and.has.property("message", "test message");
102+
103+
expect(getError(FailureReason.SERVICE_UNAVAILABLE, "test message"))
104+
.is.instanceOf(ServiceUnavailableError)
105+
.and.has.property("message", "test message");
106+
107+
expect(getError(FailureReason.TRANSACTION_CANCELED, "test message"))
108+
.is.instanceOf(TransactionCancelledError)
109+
.and.has.property("message", "test message");
110+
});
111+
});
112+
});
113+
});

0 commit comments

Comments
 (0)