commit 4536c0c8f92818856076a2fba6e8015deca135c6
parent f0e1944ffd82d1d6a8093230a590a6762a13bc6b
Author: ms <ms@taler.net>
Date: Wed, 15 Dec 2021 15:01:36 +0100
bank: test successful registration
Diffstat:
2 files changed, 28 insertions(+), 57 deletions(-)
diff --git a/packages/bank/src/pages/home/index.tsx b/packages/bank/src/pages/home/index.tsx
@@ -103,58 +103,11 @@ function usePageState(
*****************/
/**
- * Wrappers for HTTP requests specific to individual API calls.
- * Each wrapper will (1) manage HTTP requests and responses,
- * and (2) update the state accordingly.
- *
- * Their signature is:
- * RequestType x ResponseType x use*State() => void
- *
- * For example, a 'wrap()' function can look like:
- *
- * wrap(url: string,
- * req: WrapTypeReq,
- * state: StateType // Will only have setters used.
- * ) {
- *
- * let cb: (resp: WrapTypeRes) => void = {
- * // implementation here.
- * // ..
- * state.setter(...)
- * };
- *
- * post(url, req, (resp) => cb(resp))
- * }
- *
- ***/
-
-/**
- * This function requests GET /accounts/{account_name}.
- *
- * It's only a information retriever, without any effect
- * on the state.
+ * A 'wrapper' is typically a function that prepares one
+ * particular API call and updates the state accordingly.
+ * Whether a new component should be returned too, depends
+ * on each case.
*/
-async function accountInfoCall(
- backendState: BackendStateType,
- accountStateSetter: (fn: (state: AccountStateType) => void) => void
-) {
- const url = new URL(`accounts/${backendState.username}`, backendState.url);
- const handleResp = (respStatus: number) => {
- switch (respStatus) {
- case 200: {
- accountStateSetter((state) => ({ ...state, balance: "1 EUR" }));
- break;
- }
- default: {
- accountStateSetter((state) => ({
- ...state,
- error: "Missing information.",
- }));
- }
- }
- };
- handleResp(200);
-}
/**
* This function requests /register.
@@ -202,11 +155,13 @@ async function registrationCall(
/**
* Show only the account's balance.
*/
-export function Account(props: any) {
+function Account(props: any) {
const { data, error } = useSWR(`accounts/${props.accountLabel}`);
- console.log("data", data);
- console.log("error", error);
- return <p>Your balance is coming...</p>;
+ if (typeof error != "undefined") {
+ return <p>Account information could not be retrieved</p>
+ }
+ if (!data) return <p>Retrieving the balance...</p>;
+ return <p>Your balance is {data.balance.amount}.</p>;
}
/**
@@ -219,7 +174,7 @@ function SWRWithCredentials(props: any): VNode {
"Authorization",
`Basic ${Buffer.from(username + ":" + password).toString("base64")}`
);
- console.log("API call to", backendUrl);
+ console.log("Likely backend base URL", backendUrl);
return (
<SWRConfig
value={{
diff --git a/packages/bank/tests/__tests__/homepage.js b/packages/bank/tests/__tests__/homepage.js
@@ -38,7 +38,7 @@ describe("home page", () => {
)
})
- test("new registration network failure", async () => {
+ test("registration network failure", async () => {
render(<BankHome />);
let submitButton = fillRegistrationForm();
// Mocking network failure.
@@ -50,4 +50,20 @@ describe("home page", () => {
{body: JSON.stringify({username: "foo", password: "bar"}), method: "POST"},
)
})
+
+ test("registration success", async () => {
+ render(<BankHome />);
+ let submitButton = fillRegistrationForm();
+ fetch.once("{}", {
+ status: 200
+ }).once(JSON.stringify({
+ balance: {
+ amount: "EUR:10",
+ credit_debit_indicator: "credit"
+ },
+ paytoUri: "payto://iban/123/ABC"
+ }))
+ fireEvent.click(submitButton);
+ await screen.findByText("balance is EUR:10", {exact: false})
+ })
})