Skip to content

Commit 02cc1a2

Browse files
Merge branch 'master' into feat/payments_bankaccounts
2 parents 2609f06 + 613f8aa commit 02cc1a2

File tree

2 files changed

+156
-4
lines changed

2 files changed

+156
-4
lines changed

src/resources/Invoices.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,24 @@ export default class Invoices extends BaseCrud<
9090
}/${invoiceId.toString()}/payment/${paymentId.toString()}`
9191
);
9292
}
93+
94+
/**
95+
* Delete a payment for an invoice
96+
*
97+
* @param {number} invoiceId
98+
* @param {number} paymentId
99+
* @return {*} {Promise<void>}
100+
* @memberof Invoices
101+
*/
102+
public async deletePayment(
103+
invoiceId: number,
104+
paymentId: number
105+
): Promise<void> {
106+
return this.request<void>(
107+
"DELETE",
108+
`${
109+
this.apiEndpoint
110+
}/${invoiceId.toString()}/payment/${paymentId.toString()}`
111+
);
112+
}
93113
}

src/tests/Invoices.test.ts

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,150 @@
11
import BaseCrud from "../resources/BaseCrud";
22
import Invoices from "../resources/Invoices";
33
import Chance from "chance";
4-
5-
jest.mock("../resources/BaseCrud");
4+
import { InvoicesStatic } from "../interfaces/InvoicesStatic";
65

76
const seedgenerator = new Chance();
87
const seed = seedgenerator.hash();
98
console.log(`using chance seed ${seed}`);
109
const chance = new Chance(seed);
1110

1211
describe("Invoices", () => {
12+
let requestSpy: jest.SpyInstance;
13+
14+
beforeEach(() => {
15+
requestSpy = jest
16+
.spyOn(BaseCrud.prototype as any, "request")
17+
.mockResolvedValue({});
18+
});
19+
20+
afterEach(() => {
21+
requestSpy.mockRestore();
22+
});
23+
1324
it("Should use init the base correctly", () => {
1425
const token = chance.string();
15-
new Invoices(token);
16-
expect(BaseCrud).toHaveBeenCalledWith(token, "/2.0/kb_invoice");
26+
const invoices = new Invoices(token);
27+
// @ts-ignore
28+
expect(invoices.apiToken).toBe(token);
29+
// @ts-ignore
30+
expect(invoices.apiEndpoint).toBe("/2.0/kb_invoice");
31+
});
32+
33+
describe("sent", () => {
34+
it("Should call request with POST and correct path", async () => {
35+
const invoices = new Invoices(chance.string());
36+
const id = chance.integer();
37+
const sentData: Partial<InvoicesStatic.InvoiceSent> = {
38+
recipient_email: chance.email(),
39+
subject: chance.string(),
40+
message: chance.string(),
41+
mark_as_open: true,
42+
};
43+
44+
await invoices.sent(id, sentData);
45+
46+
expect(requestSpy).toHaveBeenCalledWith(
47+
"POST",
48+
`/2.0/kb_invoice/${id}/send`,
49+
undefined,
50+
sentData
51+
);
52+
});
53+
});
54+
55+
describe("revertIssue", () => {
56+
it("Should call request with POST and correct path", async () => {
57+
const invoices = new Invoices(chance.string());
58+
const id = chance.integer();
59+
60+
await invoices.revertIssue(id);
61+
62+
expect(requestSpy).toHaveBeenCalledWith(
63+
"POST",
64+
`/2.0/kb_invoice/${id}/revert_issue`
65+
);
66+
});
67+
});
68+
69+
describe("createPayment", () => {
70+
it("Should call request with POST and correct path and formatted data", async () => {
71+
const invoices = new Invoices(chance.string());
72+
const invoiceId = chance.integer();
73+
const date = new Date(2023, 11, 15); // December 15, 2023
74+
const value = "100.50";
75+
const bankAccountId = chance.integer();
76+
const paymentServiceId = chance.integer();
77+
78+
await invoices.createPayment(
79+
invoiceId,
80+
date,
81+
value,
82+
bankAccountId,
83+
paymentServiceId
84+
);
85+
86+
expect(requestSpy).toHaveBeenCalledWith(
87+
"POST",
88+
`/2.0/kb_invoice/${invoiceId}/payment`,
89+
undefined,
90+
{
91+
date: "2023-12-15",
92+
value: "100.50",
93+
bank_account_id: bankAccountId,
94+
payment_service_id: paymentServiceId,
95+
}
96+
);
97+
});
98+
99+
it("Should call request without optional parameters", async () => {
100+
const invoices = new Invoices(chance.string());
101+
const invoiceId = chance.integer();
102+
const date = new Date(2023, 0, 5); // January 5, 2023
103+
const value = "50.25";
104+
105+
await invoices.createPayment(invoiceId, date, value);
106+
107+
expect(requestSpy).toHaveBeenCalledWith(
108+
"POST",
109+
`/2.0/kb_invoice/${invoiceId}/payment`,
110+
undefined,
111+
{
112+
date: "2023-01-05",
113+
value: "50.25",
114+
bank_account_id: undefined,
115+
payment_service_id: undefined,
116+
}
117+
);
118+
});
119+
});
120+
121+
describe("getPayment", () => {
122+
it("Should call request with GET and correct path", async () => {
123+
const invoices = new Invoices(chance.string());
124+
const invoiceId = chance.integer();
125+
const paymentId = chance.integer();
126+
127+
await invoices.getPayment(invoiceId, paymentId);
128+
129+
expect(requestSpy).toHaveBeenCalledWith(
130+
"GET",
131+
`/2.0/kb_invoice/${invoiceId}/payment/${paymentId}`
132+
);
133+
});
134+
});
135+
136+
describe("deletePayment", () => {
137+
it("Should call request with DELETE and correct path", async () => {
138+
const invoices = new Invoices(chance.string());
139+
const invoiceId = chance.integer();
140+
const paymentId = chance.integer();
141+
142+
await invoices.deletePayment(invoiceId, paymentId);
143+
144+
expect(requestSpy).toHaveBeenCalledWith(
145+
"DELETE",
146+
`/2.0/kb_invoice/${invoiceId}/payment/${paymentId}`
147+
);
148+
});
17149
});
18150
});

0 commit comments

Comments
 (0)