|
1 | 1 | import BaseCrud from "../resources/BaseCrud"; |
2 | 2 | import Invoices from "../resources/Invoices"; |
3 | 3 | import Chance from "chance"; |
4 | | - |
5 | | -jest.mock("../resources/BaseCrud"); |
| 4 | +import { InvoicesStatic } from "../interfaces/InvoicesStatic"; |
6 | 5 |
|
7 | 6 | const seedgenerator = new Chance(); |
8 | 7 | const seed = seedgenerator.hash(); |
9 | 8 | console.log(`using chance seed ${seed}`); |
10 | 9 | const chance = new Chance(seed); |
11 | 10 |
|
12 | 11 | 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 | + |
13 | 24 | it("Should use init the base correctly", () => { |
14 | 25 | 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 | + }); |
17 | 149 | }); |
18 | 150 | }); |
0 commit comments