aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/cta/Payment
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-12-15 17:12:03 -0300
committerSebastian <sebasjm@gmail.com>2022-12-15 17:12:03 -0300
commitf1f8f818dbe631fbeeba64af9dfcae1aa7842615 (patch)
tree856d3b443556ce271c3e9fdab6aed7ae7728ab65 /packages/taler-wallet-webextension/src/cta/Payment
parentf93bd51499ed34844b666bf6d333227adf4368bf (diff)
downloadwallet-core-f1f8f818dbe631fbeeba64af9dfcae1aa7842615.tar.xz
pretty
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta/Payment')
-rw-r--r--packages/taler-wallet-webextension/src/cta/Payment/index.ts2
-rw-r--r--packages/taler-wallet-webextension/src/cta/Payment/state.ts11
-rw-r--r--packages/taler-wallet-webextension/src/cta/Payment/test.ts324
3 files changed, 188 insertions, 149 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/Payment/index.ts b/packages/taler-wallet-webextension/src/cta/Payment/index.ts
index 45e4a5b88..2dc6b6741 100644
--- a/packages/taler-wallet-webextension/src/cta/Payment/index.ts
+++ b/packages/taler-wallet-webextension/src/cta/Payment/index.ts
@@ -19,7 +19,7 @@ import {
PreparePayResult,
PreparePayResultAlreadyConfirmed,
PreparePayResultInsufficientBalance,
- PreparePayResultPaymentPossible
+ PreparePayResultPaymentPossible,
} from "@gnu-taler/taler-util";
import { Loading } from "../../components/Loading.js";
import { HookError } from "../../hooks/useAsyncAsHook.js";
diff --git a/packages/taler-wallet-webextension/src/cta/Payment/state.ts b/packages/taler-wallet-webextension/src/cta/Payment/state.ts
index 7690910e6..d4adf4bcb 100644
--- a/packages/taler-wallet-webextension/src/cta/Payment/state.ts
+++ b/packages/taler-wallet-webextension/src/cta/Payment/state.ts
@@ -28,11 +28,14 @@ import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js";
import { ButtonHandler } from "../../mui/handlers.js";
import { Props, State } from "./index.js";
-export function useComponentState(
- { talerPayUri, cancel, goToWalletManualWithdraw, onSuccess }: Props,
-): State {
+export function useComponentState({
+ talerPayUri,
+ cancel,
+ goToWalletManualWithdraw,
+ onSuccess,
+}: Props): State {
const [payErrMsg, setPayErrMsg] = useState<TalerError | undefined>(undefined);
- const api = useBackendContext()
+ const api = useBackendContext();
const hook = useAsyncAsHook(async () => {
if (!talerPayUri) throw Error("ERROR_NO-URI-FOR-PAYMENT");
diff --git a/packages/taler-wallet-webextension/src/cta/Payment/test.ts b/packages/taler-wallet-webextension/src/cta/Payment/test.ts
index aba76fcf4..077930972 100644
--- a/packages/taler-wallet-webextension/src/cta/Payment/test.ts
+++ b/packages/taler-wallet-webextension/src/cta/Payment/test.ts
@@ -45,22 +45,26 @@ describe("Payment CTA states", () => {
onSuccess: nullFunction,
};
- const hookBehavior = await tests.hookBehaveLikeThis(useComponentState, props, [
- ({ status, error }) => {
- expect(status).equals("loading");
- expect(error).undefined;
- },
- ({ status, error }) => {
- expect(status).equals("loading-uri");
- if (error === undefined) expect.fail();
- expect(error.hasError).true;
- expect(error.operational).false;
- },
- ], TestingContext)
+ const hookBehavior = await tests.hookBehaveLikeThis(
+ useComponentState,
+ props,
+ [
+ ({ status, error }) => {
+ expect(status).equals("loading");
+ expect(error).undefined;
+ },
+ ({ status, error }) => {
+ expect(status).equals("loading-uri");
+ if (error === undefined) expect.fail();
+ expect(error.hasError).true;
+ expect(error.operational).false;
+ },
+ ],
+ TestingContext,
+ );
- expect(hookBehavior).deep.equal({ result: "ok" })
+ expect(hookBehavior).deep.equal({ result: "ok" });
expect(handler.getCallingQueueState()).eq("empty");
-
});
it("should response with no balance", async () => {
@@ -86,22 +90,27 @@ describe("Payment CTA states", () => {
{ balances: [] },
);
- const hookBehavior = await tests.hookBehaveLikeThis(useComponentState, props, [
- ({ status, error }) => {
- expect(status).equals("loading");
- expect(error).undefined;
- },
- (state) => {
- if (state.status !== "no-balance-for-currency") {
- expect(state).eq({});
- return;
- }
- expect(state.balance).undefined;
- expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:10"));
- },
- ], TestingContext)
+ const hookBehavior = await tests.hookBehaveLikeThis(
+ useComponentState,
+ props,
+ [
+ ({ status, error }) => {
+ expect(status).equals("loading");
+ expect(error).undefined;
+ },
+ (state) => {
+ if (state.status !== "no-balance-for-currency") {
+ expect(state).eq({});
+ return;
+ }
+ expect(state.balance).undefined;
+ expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:10"));
+ },
+ ],
+ TestingContext,
+ );
- expect(hookBehavior).deep.equal({ result: "ok" })
+ expect(hookBehavior).deep.equal({ result: "ok" });
expect(handler.getCallingQueueState()).eq("empty");
});
@@ -138,19 +147,24 @@ describe("Payment CTA states", () => {
},
);
- const hookBehavior = await tests.hookBehaveLikeThis(useComponentState, props, [
- ({ status, error }) => {
- expect(status).equals("loading");
- expect(error).undefined;
- },
- (state) => {
- if (state.status !== "no-enough-balance") expect.fail();
- expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:5"));
- expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:10"));
- },
- ], TestingContext)
+ const hookBehavior = await tests.hookBehaveLikeThis(
+ useComponentState,
+ props,
+ [
+ ({ status, error }) => {
+ expect(status).equals("loading");
+ expect(error).undefined;
+ },
+ (state) => {
+ if (state.status !== "no-enough-balance") expect.fail();
+ expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:5"));
+ expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:10"));
+ },
+ ],
+ TestingContext,
+ );
- expect(hookBehavior).deep.equal({ result: "ok" })
+ expect(hookBehavior).deep.equal({ result: "ok" });
expect(handler.getCallingQueueState()).eq("empty");
});
@@ -187,25 +201,29 @@ describe("Payment CTA states", () => {
],
},
);
- const hookBehavior = await tests.hookBehaveLikeThis(useComponentState, props, [
- ({ status, error }) => {
- expect(status).equals("loading");
- expect(error).undefined;
- },
- (state) => {
- if (state.status !== "ready") {
- expect(state).eq({});
- return;
- }
- expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
- expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:10"));
- expect(state.payHandler.onClick).not.undefined;
- },
- ], TestingContext)
+ const hookBehavior = await tests.hookBehaveLikeThis(
+ useComponentState,
+ props,
+ [
+ ({ status, error }) => {
+ expect(status).equals("loading");
+ expect(error).undefined;
+ },
+ (state) => {
+ if (state.status !== "ready") {
+ expect(state).eq({});
+ return;
+ }
+ expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
+ expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:10"));
+ expect(state.payHandler.onClick).not.undefined;
+ },
+ ],
+ TestingContext,
+ );
- expect(hookBehavior).deep.equal({ result: "ok" })
+ expect(hookBehavior).deep.equal({ result: "ok" });
expect(handler.getCallingQueueState()).eq("empty");
-
});
it("should be able to pay (with fee)", async () => {
@@ -241,20 +259,25 @@ describe("Payment CTA states", () => {
],
},
);
- const hookBehavior = await tests.hookBehaveLikeThis(useComponentState, props, [
- ({ status, error }) => {
- expect(status).equals("loading");
- expect(error).undefined;
- },
- (state) => {
- if (state.status !== "ready") expect.fail();
- expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
- expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
- expect(state.payHandler.onClick).not.undefined;
- },
- ], TestingContext)
+ const hookBehavior = await tests.hookBehaveLikeThis(
+ useComponentState,
+ props,
+ [
+ ({ status, error }) => {
+ expect(status).equals("loading");
+ expect(error).undefined;
+ },
+ (state) => {
+ if (state.status !== "ready") expect.fail();
+ expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
+ expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
+ expect(state.payHandler.onClick).not.undefined;
+ },
+ ],
+ TestingContext,
+ );
- expect(hookBehavior).deep.equal({ result: "ok" })
+ expect(hookBehavior).deep.equal({ result: "ok" });
expect(handler.getCallingQueueState()).eq("empty");
});
@@ -297,26 +320,30 @@ describe("Payment CTA states", () => {
contractTerms: {},
} as ConfirmPayResult);
- const hookBehavior = await tests.hookBehaveLikeThis(useComponentState, props, [
- ({ status, error }) => {
- expect(status).equals("loading");
- expect(error).undefined;
- },
- (state) => {
- if (state.status !== "ready") {
- expect(state).eq({});
- return;
- }
- expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
- expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
- if (state.payHandler.onClick === undefined) expect.fail();
- state.payHandler.onClick();
- },
- ], TestingContext)
+ const hookBehavior = await tests.hookBehaveLikeThis(
+ useComponentState,
+ props,
+ [
+ ({ status, error }) => {
+ expect(status).equals("loading");
+ expect(error).undefined;
+ },
+ (state) => {
+ if (state.status !== "ready") {
+ expect(state).eq({});
+ return;
+ }
+ expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
+ expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
+ if (state.payHandler.onClick === undefined) expect.fail();
+ state.payHandler.onClick();
+ },
+ ],
+ TestingContext,
+ );
- expect(hookBehavior).deep.equal({ result: "ok" })
+ expect(hookBehavior).deep.equal({ result: "ok" });
expect(handler.getCallingQueueState()).eq("empty");
-
});
it("should not stay in ready state after pay with error", async () => {
@@ -357,40 +384,44 @@ describe("Payment CTA states", () => {
lastError: { code: 1 },
} as ConfirmPayResult);
- const hookBehavior = await tests.hookBehaveLikeThis(useComponentState, props, [
- ({ status, error }) => {
- expect(status).equals("loading");
- expect(error).undefined;
- },
- (state) => {
- if (state.status !== "ready") expect.fail();
- expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
- expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
- // expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1"));
- if (state.payHandler.onClick === undefined) expect.fail();
- state.payHandler.onClick();
- },
- (state) => {
- if (state.status !== "ready") expect.fail();
- expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
- expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
- // expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1"));
- expect(state.payHandler.onClick).undefined;
- if (state.payHandler.error === undefined) expect.fail();
- //FIXME: error message here is bad
- expect(state.payHandler.error.errorDetail.hint).eq(
- "could not confirm payment",
- );
- expect(state.payHandler.error.errorDetail.payResult).deep.equal({
- type: ConfirmPayResultType.Pending,
- lastError: { code: 1 },
- });
- },
- ], TestingContext)
+ const hookBehavior = await tests.hookBehaveLikeThis(
+ useComponentState,
+ props,
+ [
+ ({ status, error }) => {
+ expect(status).equals("loading");
+ expect(error).undefined;
+ },
+ (state) => {
+ if (state.status !== "ready") expect.fail();
+ expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
+ expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
+ // expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1"));
+ if (state.payHandler.onClick === undefined) expect.fail();
+ state.payHandler.onClick();
+ },
+ (state) => {
+ if (state.status !== "ready") expect.fail();
+ expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
+ expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
+ // expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1"));
+ expect(state.payHandler.onClick).undefined;
+ if (state.payHandler.error === undefined) expect.fail();
+ //FIXME: error message here is bad
+ expect(state.payHandler.error.errorDetail.hint).eq(
+ "could not confirm payment",
+ );
+ expect(state.payHandler.error.errorDetail.payResult).deep.equal({
+ type: ConfirmPayResultType.Pending,
+ lastError: { code: 1 },
+ });
+ },
+ ],
+ TestingContext,
+ );
- expect(hookBehavior).deep.equal({ result: "ok" })
+ expect(hookBehavior).deep.equal({ result: "ok" });
expect(handler.getCallingQueueState()).eq("empty");
-
});
it("should update balance if a coins is withdraw", async () => {
@@ -455,30 +486,35 @@ describe("Payment CTA states", () => {
},
);
- const hookBehavior = await tests.hookBehaveLikeThis(useComponentState, props, [
- ({ status, error }) => {
- expect(status).equals("loading");
- expect(error).undefined;
- },
- (state) => {
- if (state.status !== "ready") expect.fail()
- expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:10"));
- expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
- // expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1"));
- expect(state.payHandler.onClick).not.undefined;
-
- handler.notifyEventFromWallet(NotificationType.CoinWithdrawn);
- },
- (state) => {
- if (state.status !== "ready") expect.fail()
- expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
- expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
- // expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1"));
- expect(state.payHandler.onClick).not.undefined;
- },
- ], TestingContext)
+ const hookBehavior = await tests.hookBehaveLikeThis(
+ useComponentState,
+ props,
+ [
+ ({ status, error }) => {
+ expect(status).equals("loading");
+ expect(error).undefined;
+ },
+ (state) => {
+ if (state.status !== "ready") expect.fail();
+ expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:10"));
+ expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
+ // expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1"));
+ expect(state.payHandler.onClick).not.undefined;
+
+ handler.notifyEventFromWallet(NotificationType.CoinWithdrawn);
+ },
+ (state) => {
+ if (state.status !== "ready") expect.fail();
+ expect(state.balance).deep.equal(Amounts.parseOrThrow("USD:15"));
+ expect(state.amount).deep.equal(Amounts.parseOrThrow("USD:9"));
+ // expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1"));
+ expect(state.payHandler.onClick).not.undefined;
+ },
+ ],
+ TestingContext,
+ );
- expect(hookBehavior).deep.equal({ result: "ok" })
+ expect(hookBehavior).deep.equal({ result: "ok" });
expect(handler.getCallingQueueState()).eq("empty");
});
});