Skip to content

Commit 48ff005

Browse files
Merge pull request #74 from mathewmeconry/fix/bills_v4
fix(bills): add BillStatusFilter enum and update list method documentation
2 parents 6c77d98 + 80305ab commit 48ff005

File tree

3 files changed

+112
-28
lines changed

3 files changed

+112
-28
lines changed

src/interfaces/BillsV4Static.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,21 @@ export namespace BillsV4Static {
155155
DUPLICATE = "DUPLICATE",
156156
}
157157

158+
export enum BillStatusFilter {
159+
DRAFTS = "DRAFTS",
160+
TODO = "TODO",
161+
PAID = "PAID",
162+
OVERDUE = "OVERDUE",
163+
}
164+
158165
export interface ListOptions {
159166
limit?: number;
160167
page?: number;
161168
order?: "asc" | "desc";
162169
sort?: string;
163170
search_term?: string;
164171
"fields[]"?: string[];
165-
status?: "DRAFTS" | "TODO" | "PAID" | "OVERDUE";
172+
status?: BillStatusFilter;
166173
bill_date_start?: string;
167174
bill_date_end?: string;
168175
due_date_start?: string;

src/resources/BillsV4.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,27 @@ export default class BillsV4 extends BaseCrud<
1414
super(apiToken, "/4.0/purchase/bills");
1515
}
1616

17+
1718
/**
18-
* Lists the bills
19+
* Lists the ressource
1920
*
2021
* @param {BillsV4Static.ListOptions} [options]
21-
* @returns {Promise<BillsV4Static.Bill[]>}
22+
* @returns {Promise<Array<BillsV4Static.Bill>>}
2223
* @memberof BillsV4
2324
*/
2425
public async list(
2526
options?: BillsV4Static.ListOptions
26-
): Promise<BillsV4Static.Bill[]> {
27-
const response = await this.request<{ data: BillsV4Static.Bill[] }>(
28-
"GET",
29-
"/4.0/purchase/bills",
30-
options
31-
);
32-
return response.data;
27+
): Promise<Array<BillsV4Static.Bill>> {
28+
const resp = await this.request<{
29+
data: Array<BillsV4Static.Bill>;
30+
paging: {
31+
page: number;
32+
page_size: number;
33+
page_count: number;
34+
item_count: number;
35+
};
36+
}>("GET", this.apiEndpoint, options);
37+
return resp.data;
3338
}
3439

3540
/**
@@ -103,4 +108,4 @@ export default class BillsV4 extends BaseCrud<
103108
{ document_no: documentNo }
104109
);
105110
}
106-
}
111+
}

src/tests/BillsV4.test.ts

Lines changed: 89 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,41 @@ import BaseCrud from "../resources/BaseCrud";
22
import BillsV4 from "../resources/BillsV4";
33
import Chance from "chance";
44
import { BillsV4Static } from "../interfaces/BillsV4Static";
5-
6-
jest.mock("../resources/BaseCrud");
5+
import { BaseStatic } from "../interfaces/BaseStatic";
76

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

1312
describe("BillsV4", () => {
13+
let requestSpy: jest.SpyInstance;
14+
15+
beforeEach(() => {
16+
requestSpy = jest
17+
.spyOn(BaseCrud.prototype as any, "request")
18+
.mockResolvedValue({});
19+
});
20+
1421
afterEach(() => {
15-
jest.resetAllMocks();
22+
requestSpy.mockRestore();
1623
});
1724

1825
it("Should use init the base correctly", () => {
1926
const token = chance.string();
20-
new BillsV4(token);
21-
expect(BaseCrud).toHaveBeenCalledWith(token, "/4.0/purchase/bills");
27+
const billsV4 = new BillsV4(token);
28+
// @ts-ignore
29+
expect(billsV4.apiToken).toBe(token);
30+
// @ts-ignore
31+
expect(billsV4.apiEndpoint).toBe("/4.0/purchase/bills");
2232
});
2333

2434
describe("list", () => {
2535
it("Should call request with GET", async () => {
2636
const billsV4 = new BillsV4(chance.string());
27-
// @ts-ignore
28-
BaseCrud.prototype.request.mockResolvedValue({ data: [] });
37+
requestSpy.mockResolvedValue({ data: [] });
2938
await billsV4.list();
30-
// @ts-ignore
31-
expect(BaseCrud.prototype.request).toHaveBeenCalledWith(
39+
expect(requestSpy).toHaveBeenCalledWith(
3240
"GET",
3341
"/4.0/purchase/bills",
3442
undefined
@@ -37,23 +45,87 @@ describe("BillsV4", () => {
3745

3846
it("Should pass options to request", async () => {
3947
const billsV4 = new BillsV4(chance.string());
40-
const options: BillsV4Static.ListOptions = { limit: chance.integer({ min: 0 }) };
41-
// @ts-ignore
42-
BaseCrud.prototype.request.mockResolvedValue({ data: [] });
48+
const options: BaseStatic.BaseOptions = { limit: chance.integer() };
49+
requestSpy.mockResolvedValue({ data: [] });
4350
await billsV4.list(options);
44-
// @ts-ignore
45-
expect(BaseCrud.prototype.request).toHaveBeenCalledWith(
51+
expect(requestSpy).toHaveBeenCalledWith(
52+
"GET",
53+
"/4.0/purchase/bills",
54+
options
55+
);
56+
});
57+
58+
it("should list bills with options", async () => {
59+
const billsV4 = new BillsV4(chance.string());
60+
const bills = [
61+
{ id: chance.guid(), document_no: "B-1", lastname_company: "Test" },
62+
];
63+
requestSpy.mockResolvedValue({ data: bills });
64+
65+
const options: BillsV4Static.ListOptions = {
66+
limit: 10,
67+
page: 1,
68+
"fields[]": ["document_no", "title"],
69+
};
70+
const result = await billsV4.list(options);
71+
72+
expect(requestSpy).toHaveBeenCalledWith(
4673
"GET",
4774
"/4.0/purchase/bills",
4875
options
4976
);
77+
expect(result).toEqual(bills);
78+
});
79+
});
80+
81+
describe("updateStatus", () => {
82+
it("Should call request with PUT and correct path", async () => {
83+
const billsV4 = new BillsV4(chance.string());
84+
const id = chance.guid();
85+
const status = BillsV4Static.BillStatusUpdate.BOOKED;
86+
await billsV4.updateStatus(id, status);
87+
expect(requestSpy).toHaveBeenCalledWith(
88+
"PUT",
89+
`/4.0/purchase/bills/${id}/bookings/${status}`
90+
);
91+
});
92+
});
93+
94+
describe("executeAction", () => {
95+
it("Should call request with POST and correct path and data", async () => {
96+
const billsV4 = new BillsV4(chance.string());
97+
const id = chance.guid();
98+
const action = BillsV4Static.BillAction.DUPLICATE;
99+
await billsV4.executeAction(id, action);
100+
expect(requestSpy).toHaveBeenCalledWith(
101+
"POST",
102+
`/4.0/purchase/bills/${id}/actions`,
103+
undefined,
104+
{ action }
105+
);
106+
});
107+
});
108+
109+
describe("validateDocumentNumber", () => {
110+
it("Should call request with GET and correct path and data", async () => {
111+
const billsV4 = new BillsV4(chance.string());
112+
const documentNo = chance.string();
113+
requestSpy.mockResolvedValue({ valid: true });
114+
await billsV4.validateDocumentNumber(documentNo);
115+
expect(requestSpy).toHaveBeenCalledWith(
116+
"GET",
117+
"/4.0/purchase/documentnumbers/bills",
118+
{ document_no: documentNo }
119+
);
50120
});
51121
});
52122

53123
describe("search", () => {
54124
it("Should throw not implemented error", async () => {
55125
const billsV4 = new BillsV4(chance.string());
56-
await expect(billsV4.search([])).rejects.toThrow("not implemented by Bexio yet");
126+
await expect(billsV4.search([])).rejects.toThrow(
127+
"not implemented by Bexio yet"
128+
);
57129
});
58130
});
59131

@@ -66,7 +138,7 @@ describe("BillsV4", () => {
66138
// @ts-ignore
67139
expect(BaseCrud.prototype.request).toHaveBeenCalledWith(
68140
"PUT",
69-
`undefined/${id}/bookings/${status}`
141+
`/4.0/purchase/bills/${id}/bookings/${status}`
70142
);
71143
});
72144
});
@@ -80,7 +152,7 @@ describe("BillsV4", () => {
80152
// @ts-ignore
81153
expect(BaseCrud.prototype.request).toHaveBeenCalledWith(
82154
"POST",
83-
`undefined/${id}/actions`,
155+
`/4.0/purchase/bills/${id}/actions`,
84156
undefined,
85157
{ action }
86158
);

0 commit comments

Comments
 (0)