xero-hero
Version:
Heroic utilities to simplify and enable your progress with the [xero-node](https://www.npmjs.com/package/xero-node) SDK.
166 lines (159 loc) • 6.9 kB
JavaScript
// src/accounting/invoices/__tests__/lineItems.test.ts
import { LineItem } from "xero-node";
// src/common/instance/operations.ts
import { isObject } from "deep-cuts";
// src/accounting/attachments/requests.ts
import { bufferToStream } from "tranquil-stream";
// src/accounting/contacts/links.ts
import qs from "qs";
// src/accounting/invoices/lineItems.ts
import { isNil } from "deep-cuts";
var filterInvoiceLineItems = (lineItems, minCode, maxCode) => {
if (typeof minCode === "function") {
return (lineItems || []).filter(minCode);
}
const parsedMinCode = isNil(minCode) ? minCode : Number.parseInt(minCode, 10);
const parsedMaxCode = isNil(maxCode) ? maxCode : Number.parseInt(maxCode, 10);
if (parsedMinCode || parsedMaxCode) {
return (lineItems || []).filter(({ itemCode }) => {
const parsedItemCode = isNil(itemCode) ? itemCode : Number.parseInt(itemCode, 10);
if (parsedItemCode) {
const greaterThanOrEqualToMinCode = isNil(parsedMinCode) ? true : parsedItemCode >= parsedMinCode;
const lessThanOrEqualToMaxCode = isNil(parsedMaxCode) ? true : parsedItemCode <= (parsedMaxCode || 0);
return greaterThanOrEqualToMinCode && lessThanOrEqualToMaxCode;
}
return false;
});
}
return lineItems || [];
};
// src/accounting/journals/links.ts
import qs2 from "qs";
// src/projects/timeEntries.ts
import { roundToNearestFraction } from "deep-cuts";
// src/accounting/invoices/__tests__/lineItems.test.ts
var generateLineItemsWithCodes = (itemCodes) => {
return itemCodes.map((itemCode) => {
const lineItem = new LineItem();
lineItem.itemCode = String(itemCode);
return lineItem;
});
};
describe("invoices/lineItems", () => {
describe("filterInvoiceLineItems()", () => {
it("should return an empty array when passed undefined", () => {
expect(filterInvoiceLineItems(void 0, 1e3)).toEqual([]);
});
it("should return an empty array when passed null", () => {
expect(filterInvoiceLineItems(null, String(0))).toEqual([]);
});
it("should return an empty array when passed an empty array", () => {
expect(filterInvoiceLineItems([], 5500)).toEqual([]);
});
it("should return all given line items when no min, max, or decision function is provided", () => {
const lineItems = generateLineItemsWithCodes([1, 50, 5e3]);
const filteredLineItems = filterInvoiceLineItems(lineItems);
expect(filteredLineItems).toHaveLength(3);
expect(filteredLineItems[0].itemCode).toBe("1");
expect(filteredLineItems[1].itemCode).toBe("50");
expect(filteredLineItems[2].itemCode).toBe("5000");
});
it("should remove line items without an item code in the case of an item code match", () => {
const lineItems = generateLineItemsWithCodes([1, 50, 5e3]);
const emptyLineItem = new LineItem();
const filteredLineItems = filterInvoiceLineItems(
lineItems.concat(emptyLineItem),
0,
1e4
);
expect(filteredLineItems).toHaveLength(3);
expect(filteredLineItems[0].itemCode).toBe("1");
expect(filteredLineItems[1].itemCode).toBe("50");
expect(filteredLineItems[2].itemCode).toBe("5000");
});
it("should return line items above the min if only given a min", () => {
const lineItems = generateLineItemsWithCodes([1, 50, 5e3]);
const filteredLineItems = filterInvoiceLineItems(lineItems, 50);
expect(filteredLineItems).toHaveLength(2);
expect(filteredLineItems[0].itemCode).toBe("50");
expect(filteredLineItems[1].itemCode).toBe("5000");
});
it("should return line items above the min if only given a min of string type", () => {
const lineItems = generateLineItemsWithCodes([280, 7001, 2400]);
const filteredLineItems = filterInvoiceLineItems(lineItems, "7000");
expect(filteredLineItems).toHaveLength(1);
expect(filteredLineItems[0].itemCode).toBe("7001");
});
it("should return line items below the max if only given a max", () => {
const lineItems = generateLineItemsWithCodes([280, 7001, 2400]);
const filteredLineItems = filterInvoiceLineItems(
lineItems,
// @ts-expect-error - This is an invalid type for the function.
void 0,
2500
);
expect(filteredLineItems).toHaveLength(2);
expect(filteredLineItems[0].itemCode).toBe("280");
expect(filteredLineItems[1].itemCode).toBe("2400");
});
it("should return line items below the max if only given a max of string type", () => {
const lineItems = generateLineItemsWithCodes([280, 7001, 2400]);
const filteredLineItems = filterInvoiceLineItems(
lineItems,
// @ts-expect-error - This is an invalid type for the function.
void 0,
"280"
);
expect(filteredLineItems).toHaveLength(1);
expect(filteredLineItems[0].itemCode).toBe("280");
});
it("should return line items in between the min and the max inclusive", () => {
const lineItems = generateLineItemsWithCodes([1, 29e3, 2726, 4846]);
const filteredLineItems = filterInvoiceLineItems(lineItems, 1, 2726);
expect(filteredLineItems).toHaveLength(2);
expect(filteredLineItems[0].itemCode).toBe("1");
expect(filteredLineItems[1].itemCode).toBe("2726");
});
it("should return line items in between the min and the max inclusive, when string types", () => {
const lineItems = generateLineItemsWithCodes([1, 29e3, 2726, 4846]);
const filteredLineItems = filterInvoiceLineItems(
lineItems,
"2726",
"29000"
);
expect(filteredLineItems).toHaveLength(3);
expect(filteredLineItems[0].itemCode).toBe("29000");
expect(filteredLineItems[1].itemCode).toBe("2726");
expect(filteredLineItems[2].itemCode).toBe("4846");
});
it("should be able to return line items based on a decision function", () => {
const lineItems = generateLineItemsWithCodes([1, 29e3, 2726, 4846]).map(
(lineItem, index) => {
lineItem.lineAmount = index * 100;
return lineItem;
}
);
const filteredLineItems = filterInvoiceLineItems(lineItems, (lineItem) => {
return (lineItem.lineAmount || 0) > 199;
});
expect(filteredLineItems).toHaveLength(2);
});
it("should ignore the max when a decision function is provided", () => {
const lineItems = generateLineItemsWithCodes([1, 29e3, 2726, 4846]).map(
(lineItem, index) => {
lineItem.lineAmount = index * 100;
return lineItem;
}
);
const filteredLineItems = filterInvoiceLineItems(
lineItems,
(lineItem) => {
return (lineItem.lineAmount || 0) > 199;
},
10
);
expect(filteredLineItems).toHaveLength(2);
});
});
});
//# sourceMappingURL=lineItems.test.mjs.map