merchant-backoffice

ZZZ: Inactive/Deprecated
Log | Files | Refs | Submodules | README

commit 589f954dce1b52fa19d7cf18839a0930a454e479
parent b269cfea9ee83e6f6e06a8c5b6c0c3573e666264
Author: ms <ms@taler.net>
Date:   Wed, 12 Jan 2022 21:56:11 +0100

testing public histories (WIP)

Diffstat:
Mpackages/bank/src/pages/home/index.tsx | 71++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Mpackages/bank/tests/__tests__/homepage.js | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 120 insertions(+), 25 deletions(-)

diff --git a/packages/bank/src/pages/home/index.tsx b/packages/bank/src/pages/home/index.tsx @@ -85,9 +85,11 @@ function prepareHeaders(username: string, password: string) { } const getRootPath = () => { - return typeof window !== undefined + const maybeRootPath = typeof window !== undefined ? window.location.origin + window.location.pathname : "/"; + if (!maybeRootPath.endsWith("/")) return maybeRootPath + "/"; + return maybeRootPath; }; /******************* @@ -510,25 +512,22 @@ function Transactions(Props: any): VNode { } } } - var txsPages = <p>"loading..."</p> - - if (data) { - console.log("History data", data); - txsPages = <div>{ - data.transactions.map(function(item: any) { - const sign = item.direction == "DBIT" ? "-" : ""; - const counterpart = item.direction == "DBIT" ? item.creditorIban : item.debtorIban; - // Pattern: - // - // DD/MM YYYY subject -5 EUR - // DD/MM YYYY subject 5 EUR - const dateRegex = /^([0-9]{4})-([0-9]{2})-([0-9]{1,2})/ - const dateParse = dateRegex.exec(item.date) - const date = dateParse !== null ? `${dateParse[3]}/${dateParse[2]} ${dateParse[3]}` : "date not found" - return <span> {date} {item.subject} {sign}{item.amount} {item.currency}</span> - })}</div> - } - return txsPages; + if (!data) return <p>"Transactions page loading..."</p>; + + console.log("History data", data); + return <div>{ + data.transactions.map(function(item: any) { + const sign = item.direction == "DBIT" ? "-" : ""; + const counterpart = item.direction == "DBIT" ? item.creditorIban : item.debtorIban; + // Pattern: + // + // DD/MM YYYY subject -5 EUR + // DD/MM YYYY subject 5 EUR + const dateRegex = /^([0-9]{4})-([0-9]{2})-([0-9]{1,2})/ + const dateParse = dateRegex.exec(item.date) + const date = dateParse !== null ? `${dateParse[3]}/${dateParse[2]} ${dateParse[1]}` : "date not found" + return <span> {date} {item.subject} {sign}{item.amount} {item.currency}</span> + })}</div> } /** @@ -639,12 +638,30 @@ function SWRWithCredentials(props: any): VNode { ); } +function SWRWithoutCredentials(props: any): VNode { + const { baseUrl } = props; + console.log("Base URL", baseUrl); + return ( + <SWRConfig + value={{ + fetcher: (url) => + fetch(baseUrl + url || "").then( + (r) => { + if (!r.ok) { + throw {status: r.status, json: r.json()}; + } + return r.json() + } + ), + }}>{props.children}</SWRConfig> + ); +} + /** * Show histories of public accounts. */ -export function PublicHistories(): VNode { - - const { data, error } = useSWR(`access-api/public-accounts`); +function PublicHistories(): VNode { + const { data, error } = useSWR("access-api/public-accounts") if (typeof error !== "undefined") { console.log("account error", error); switch(error.status) { @@ -656,9 +673,10 @@ export function PublicHistories(): VNode { } } } - if (!data) return <p>Loading...</p> + if (!data) return <p>Waiting public accounts list...</p> var txs = []; for (const account of data.publicAccounts) { + console.log("Asking transactions for", account.accountLabel) txs.push(<Transactions accountLabel={account.accountLabel} pageNumber={0} />) } return <div>{txs}</div>; @@ -804,7 +822,10 @@ export function BankHome(): VNode { }}>{i18n`Sign in`}</button> <div> <p>{i18n`Public transactions:`}</p> - <PublicHistories /> + <SWRWithoutCredentials + baseUrl={getRootPath()}> + <PublicHistories /> + </SWRWithoutCredentials> </div> </Fragment> ); diff --git a/packages/bank/tests/__tests__/homepage.js b/packages/bank/tests/__tests__/homepage.js @@ -192,6 +192,80 @@ describe("home page", () => { fetch.resetMocks(); cleanup(); }) + test("public histories", async () => { + // First, mock the public accounts list. + fetch.mockImplementationOnce(JSON.stringify({ + "publicAccounts" : [ { + "balance" : "EUR:1", + "iban" : "XXX", + "accountLabel" : "foo" + }, { + "balance" : "EUR:2", + "iban" : "YYY", + "accountLabel" : "bar" + }] + })) + fetch.mockImplementationOnce(JSON.stringify({ // Response to history request. + transactions: [{ + debtorIban: "XXX", + debtorBic: "YYY", + debtorName: "Foo", + creditorIban: "AAA", + creditorBic: "BBB", + creditorName: "Bar", + direction: "DBIT", + amount: "EUR:5", + subject: "Reimbursement", + date: "1970-01-01" + }, { + debtorIban: "XXX", + debtorBic: "YYY", + debtorName: "Foo", + creditorIban: "AAA", + creditorBic: "BBB", + creditorName: "Bar", + direction: "CRDT", + amount: "EUR:5", + subject: "Bonus", + date: "2000-01-01" + }] + })) + fetch.mockResponseOnce(JSON.stringify({ // Response to history request. + transactions: [{ + debtorIban: "XXX", + debtorBic: "YYY", + debtorName: "Foo", + creditorIban: "AAA", + creditorBic: "BBB", + creditorName: "Bar", + direction: "DBIT", + amount: "EUR:5", + subject: "Donation", + date: "1970-01-01" + }, { + debtorIban: "XXX", + debtorBic: "YYY", + debtorName: "Foo", + creditorIban: "AAA", + creditorBic: "BBB", + creditorName: "Bar", + direction: "CRDT", + amount: "EUR:5", + subject: "Refund", + date: "2000-01-01" + }] + })) + render(<BankHome />); + await expect(fetch).toHaveBeenCalledWith( + "http://localhost/demobanks/default/access-api/public-accounts" + ) + await expect(fetch).toHaveBeenCalledWith( + "http://localhost/demobanks/default/access-api/accounts/foo/transactions?page=0" + ) + await expect(fetch).toHaveBeenCalledWith( + "http://localhost/demobanks/default/access-api/accounts/bar/transactions?page=0" + ) + }) // check page informs about the current balance // after a successful registration.