UNPKG

xero-hero

Version:

Heroic utilities to simplify and enable your progress with the [xero-node](https://www.npmjs.com/package/xero-node) SDK.

95 lines (87 loc) 3.25 kB
// src/common/response/__tests__/selectors.test.ts import { BankTransaction, BankTransactions, Contact, Contacts, Invoice, Invoices, RepeatingInvoices } from "xero-node"; // src/common/instance/operations.ts import { isObject } from "deep-cuts"; // src/common/response/selectors.ts var getListFromResponse = (response) => { if (response.body) { const propertyName = Object.getOwnPropertyNames(response.body); for (const name of propertyName) { if (Array.isArray(response.body[name])) { return response.body[name]; } } } return void 0; }; // 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"; // src/accounting/journals/links.ts import qs2 from "qs"; // src/projects/timeEntries.ts import { roundToNearestFraction } from "deep-cuts"; // src/common/response/__tests__/selectors.test.ts describe("common/response/selectors", () => { describe("getListFromResponse()", () => { it("should return the contacts list from a contacts response", () => { const contactsList = [new Contact(), new Contact(), new Contact()]; const contactsBody = new Contacts(); contactsBody.contacts = contactsList; expect(getListFromResponse({ body: contactsBody })).toBe(contactsList); }); it("should return the invoice list from a invoices response", () => { const invoicesList = [new Invoice(), new Invoice()]; const invoicesBody = new Invoices(); invoicesBody.invoices = invoicesList; expect(getListFromResponse({ body: invoicesBody })).toBe(invoicesList); }); it("should return the bankTransactions from a bankTransactions response", () => { const bankTransactionsList = [ new BankTransaction(), new BankTransaction(), new BankTransaction(), new BankTransaction() ]; const bankTransactionsBody = new BankTransactions(); bankTransactionsBody.bankTransactions = bankTransactionsList; expect(getListFromResponse({ body: bankTransactionsBody })).toBe( bankTransactionsList ); }); it("should play nice with an object that looks like a Xero response and has an array property on the body", () => { const itemList = [1, 2, 3, 4, 5]; const itemBody = { items: itemList }; expect(getListFromResponse({ body: itemBody })).toBe(itemList); }); it("should return undefined for responses that lack an array property", () => { const repeatingInvoices = new RepeatingInvoices(); expect(getListFromResponse({ body: repeatingInvoices })).toBe(void 0); }); it("should return undefined for responses without a body", () => { expect(getListFromResponse({})).toBe(void 0); }); it("should throw an error when passed an undefined response", () => { expect(() => { return getListFromResponse(); }).toThrowError(); }); it("should throw an error when passed a null response", () => { expect(() => { return getListFromResponse(null); }).toThrowError(); }); }); }); //# sourceMappingURL=selectors.test.mjs.map