//@ts-nocheck import { testFetch } from "../../utils"; import bankAccountFuctions from "./index"; import Chance from "chance"; import { MISSING_ACCOUNT_ID_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE, MISSING_AMOUNTS_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_HOLDER_NAME_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_HOLDER_TYPE_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_NUMBER_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_ROUTING_NUMBER_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_TYPE_ERROR_MESSAGE, MISSING_BANK_ACCOUNT_ROUTING_NUMBER_LENGTH_ERROR_MESSAGE, MISSING_BANK_PAYLOAD } from "../../constants"; const bankAccounts = bankAccountFuctions(testFetch); const chance = new Chance(); describe("bank accounts", () => { let accountID, bankAccountID, bankAccount, amounts, badData, token; beforeEach(() => { accountID = chance.guid(); bankAccountID = chance.guid(); bankAccount = { holderName: chance.name(), holderType: "individual", accountNumber: chance.string({ length: 10 }), bankAccountType: "checking", routingNumber: chance.string({ length: 9 }) }; token = chance.string({ length: 10 }); badData = chance.string(); amounts = [chance.integer(), chance.integer()]; }); describe("link bank account", () => { beforeEach(() => { bankAccount = { holderName: chance.name(), holderType: "individual", accountNumber: chance.string({ length: 10 }), bankAccountType: "checking", routingNumber: chance.string({ length: 9 }) }; }); it("should return an error if there is no accountID", () => { return bankAccounts.link({ badData }).catch((resp) => { expect(resp.error).toEqual(MISSING_ACCOUNT_ID_ERROR_MESSAGE); }); }); it("should error if there is no bank account or token", async () => { delete bankAccount.accountNumber; const resp = await bankAccounts.link({ accountID }).catch((err) => err); expect(resp.error).toEqual(MISSING_BANK_PAYLOAD); }); it("should error if there is no account number", async () => { delete bankAccount.accountNumber; const resp = await bankAccounts.link({ accountID, bankAccount }).catch((err) => err); expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_NUMBER_ERROR_MESSAGE); }); it("should error if there is no routing number", async () => { delete bankAccount.routingNumber; return bankAccounts.link({ accountID, bankAccount }).catch((resp) => { expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_ROUTING_NUMBER_ERROR_MESSAGE); }); }); it("should error if routing number not 9 digits", async () => { bankAccount.routingNumber = chance.string({ length: 8 }); return bankAccounts.link({ accountID, bankAccount }).catch((resp) => { expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_ROUTING_NUMBER_LENGTH_ERROR_MESSAGE); }); }); it("should error if there is no holder name", () => { delete bankAccount.holderName; return bankAccounts.link({ accountID, bankAccount }).catch((resp) => { expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_HOLDER_NAME_ERROR_MESSAGE); }); }); it("should error if there is no holder type", () => { delete bankAccount.holderType; return bankAccounts.link({ accountID, bankAccount }).catch((resp) => { expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_HOLDER_TYPE_ERROR_MESSAGE); }); }); it("should error if there is no bank account type", () => { delete bankAccount.bankAccountType; return bankAccounts.link({ accountID, bankAccount }).catch((resp) => { expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_TYPE_ERROR_MESSAGE); }); }); it("should error if invalid bank account type", () => { bankAccount.bankAccountType = chance.string({ length: 6 }); return bankAccounts.link({ accountID, bankAccount }).catch((resp) => { expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_TYPE_ERROR_MESSAGE); }); }); it("should succeed if there is an accountID and bank account", () => { return bankAccounts.link({ accountID, bankAccount }).then((resp) => { expect(resp.url).toEqual(`/accounts/${accountID}/bank-accounts`); }); }); it("should succeed if there is an accountID and plaid token", () => { return bankAccounts.link({ accountID, plaidToken: token }).then((resp) => { expect(resp.url).toEqual(`/accounts/${accountID}/bank-accounts`); expect(resp.opts.body.plaid.token).toEqual(token); }); }); it("should succeed if there is an accountID and mx auth code", () => { return bankAccounts.link({ accountID, mxAuthorizationCode: token }).then((resp) => { expect(resp.url).toEqual(`/accounts/${accountID}/bank-accounts`); expect(resp.opts.body.mx.authorizationCode).toEqual(token); }); }); }); describe("list bank accounts", () => { it("should return an error if there is no accountID", () => { return bankAccounts.list({ badData }).catch((resp) => { expect(resp.error).toEqual(MISSING_ACCOUNT_ID_ERROR_MESSAGE); }); }); it("should succeed if there is an accountID", async () => { const resp = await bankAccounts.list({ accountID }); expect(resp.url).toEqual(`/accounts/${accountID}/bank-accounts`); expect(resp.opts.method).toEqual("GET"); }); }); describe("get bank account", () => { it("should return an error if there is no accountID", () => { return bankAccounts.get({ bankAccountID }).catch((resp) => { expect(resp.error).toEqual(MISSING_ACCOUNT_ID_ERROR_MESSAGE); }); }); it("should error if no bankAccountID", () => { return bankAccounts.get({ accountID }).catch((resp) => { expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE); }); }); it("should succeed if there is an accountID and bankAccountID", async () => { const resp = await bankAccounts.get({ accountID, bankAccountID }); expect(resp.url).toEqual(`/accounts/${accountID}/bank-accounts/${bankAccountID}`); expect(resp.opts.method).toEqual("GET"); }); }); describe("delete bank account", () => { it("should return an error if there is no accountID", async () => { return bankAccounts.delete({ bankAccountID }).catch((resp) => { expect(resp.error).toEqual(MISSING_ACCOUNT_ID_ERROR_MESSAGE); }); }); it("should error if no bankAccountID", () => { return bankAccounts.delete({ accountID }).catch((resp) => { expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE); }); }); it("should succeed if there is an accountID and bankAccountID", async () => { const resp = await bankAccounts.delete({ accountID, bankAccountID }); expect(resp.url).toEqual(`/accounts/${accountID}/bank-accounts/${bankAccountID}`); expect(resp.opts.method).toEqual("DELETE"); }); }); describe("startMicroDepositVerification", () => { it("should return an error if there is no accountID", () => { return bankAccounts.startMicroDepositVerification({ bankAccountID }).catch((resp) => { expect(resp.error).toEqual(MISSING_ACCOUNT_ID_ERROR_MESSAGE); }); }); it("should error if no bankAccountID", () => { return bankAccounts.startMicroDepositVerification({ accountID }).catch((resp) => { expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE); }); }); it("should succeed if there is an accountID, bankAccountID", async () => { const resp = await bankAccounts.startMicroDepositVerification({ accountID, bankAccountID }); expect(resp.url).toEqual(`/accounts/${accountID}/bank-accounts/${bankAccountID}/micro-deposits`); expect(resp.opts.method).toEqual("POST"); }); }); describe("completeMicroDepositVerification", () => { it("should return an error if there is no accountID", () => { return bankAccounts.completeMicroDepositVerification({ bankAccountID }).catch((resp) => { expect(resp.error).toEqual(MISSING_ACCOUNT_ID_ERROR_MESSAGE); }); }); it("should error if no bankAccountID", () => { return bankAccounts.completeMicroDepositVerification({ accountID }).catch((resp) => { expect(resp.error).toEqual(MISSING_BANK_ACCOUNT_ID_ERROR_MESSAGE); }); }); it("should error if there are no amounts", () => { return bankAccounts .completeMicroDepositVerification({ accountID, bankAccountID }) .catch((err) => { expect(err.error).toEqual(MISSING_AMOUNTS_ERROR_MESSAGE); }); }); it("should succeed if there is an accountID, bankAccountID, and amounts", async () => { const resp = await bankAccounts.completeMicroDepositVerification({ accountID, bankAccountID, amounts }); expect(resp.url).toEqual(`/accounts/${accountID}/bank-accounts/${bankAccountID}/micro-deposits`); expect(resp.opts.method).toEqual("PUT"); expect(resp.opts.body).toEqual({ amounts }); }); }); });