summaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/cta/Tip
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-10-25 12:23:08 -0300
committerSebastian <sebasjm@gmail.com>2022-10-25 12:23:52 -0300
commit3f2db7707fdf4eb4c1dfdb527d5726dd1694e126 (patch)
tree4366d443db56eb200ba29760bf1f4a0b9def6c97 /packages/taler-wallet-webextension/src/cta/Tip
parent587674dd10bd714b68ff5a6e836eb21113c0337a (diff)
downloadwallet-core-3f2db7707fdf4eb4c1dfdb527d5726dd1694e126.tar.gz
wallet-core-3f2db7707fdf4eb4c1dfdb527d5726dd1694e126.tar.bz2
wallet-core-3f2db7707fdf4eb4c1dfdb527d5726dd1694e126.zip
using new wallet api (typed interface)
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta/Tip')
-rw-r--r--packages/taler-wallet-webextension/src/cta/Tip/index.ts4
-rw-r--r--packages/taler-wallet-webextension/src/cta/Tip/state.ts17
-rw-r--r--packages/taler-wallet-webextension/src/cta/Tip/test.ts163
3 files changed, 99 insertions, 85 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/Tip/index.ts b/packages/taler-wallet-webextension/src/cta/Tip/index.ts
index 03cbd2196..520d854f2 100644
--- a/packages/taler-wallet-webextension/src/cta/Tip/index.ts
+++ b/packages/taler-wallet-webextension/src/cta/Tip/index.ts
@@ -19,13 +19,13 @@ import { Loading } from "../../components/Loading.js";
import { HookError } from "../../hooks/useAsyncAsHook.js";
import { ButtonHandler } from "../../mui/handlers.js";
import { compose, StateViewMap } from "../../utils/index.js";
-import * as wxApi from "../../wxApi.js";
+import { wxApi } from "../../wxApi.js";
import { useComponentState } from "./state.js";
import {
AcceptedView,
IgnoredView,
LoadingUriView,
- ReadyView,
+ ReadyView
} from "./views.js";
export interface Props {
diff --git a/packages/taler-wallet-webextension/src/cta/Tip/state.ts b/packages/taler-wallet-webextension/src/cta/Tip/state.ts
index f6721d504..00e1fddad 100644
--- a/packages/taler-wallet-webextension/src/cta/Tip/state.ts
+++ b/packages/taler-wallet-webextension/src/cta/Tip/state.ts
@@ -15,20 +15,18 @@
*/
import { Amounts } from "@gnu-taler/taler-util";
-import { useState } from "preact/hooks";
+import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
import { useAsyncAsHook } from "../../hooks/useAsyncAsHook.js";
-import * as wxApi from "../../wxApi.js";
+import { wxApi } from "../../wxApi.js";
import { Props, State } from "./index.js";
export function useComponentState(
{ talerTipUri, onCancel, onSuccess }: Props,
api: typeof wxApi,
): State {
- const [tipIgnored, setTipIgnored] = useState(false);
-
const tipInfo = useAsyncAsHook(async () => {
if (!talerTipUri) throw Error("ERROR_NO-URI-FOR-TIP");
- const tip = await api.prepareTip({ talerTipUri });
+ const tip = await api.wallet.call(WalletApiOperation.PrepareTip, { talerTipUri });
return { tip };
});
@@ -48,7 +46,7 @@ export function useComponentState(
const { tip } = tipInfo.response;
const doAccept = async (): Promise<void> => {
- const res = await api.acceptTip({ walletTipId: tip.walletTipId });
+ const res = await api.wallet.call(WalletApiOperation.AcceptTip, { walletTipId: tip.walletTipId });
//FIX: this may not be seen since we are moving to the success also
tipInfo.retry();
@@ -65,13 +63,6 @@ export function useComponentState(
},
};
- if (tipIgnored) {
- return {
- status: "ignored",
- ...baseInfo,
- };
- }
-
if (tip.accepted) {
return {
status: "accepted",
diff --git a/packages/taler-wallet-webextension/src/cta/Tip/test.ts b/packages/taler-wallet-webextension/src/cta/Tip/test.ts
index 47d9aa8db..69badbede 100644
--- a/packages/taler-wallet-webextension/src/cta/Tip/test.ts
+++ b/packages/taler-wallet-webextension/src/cta/Tip/test.ts
@@ -19,14 +19,18 @@
* @author Sebastian Javier Marchano (sebasjm)
*/
-import { Amounts, PrepareTipResult } from "@gnu-taler/taler-util";
+import { Amounts } from "@gnu-taler/taler-util";
+import { WalletApiOperation } from "@gnu-taler/taler-wallet-core";
import { expect } from "chai";
import { mountHook } from "../../test-utils.js";
+import { createWalletApiMock } from "../../test-utils.js";
import { useComponentState } from "./state.js";
describe("Tip CTA states", () => {
it("should tell the user that the URI is missing", async () => {
- const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
+ const { handler, mock } = createWalletApiMock();
+
+ const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(
{
@@ -38,23 +42,20 @@ describe("Tip CTA states", () => {
null;
},
},
- {
- prepareTip: async () => ({}),
- acceptTip: async () => ({}),
- } as any,
+ mock,
),
);
{
- const { status, error } = getLastResultOrThrow();
+ const { status, error } = pullLastResultOrThrow();
expect(status).equals("loading");
expect(error).undefined;
}
- await waitNextUpdate();
+ expect(await waitForStateUpdate()).true;
{
- const { status, error } = getLastResultOrThrow();
+ const { status, error } = pullLastResultOrThrow();
expect(status).equals("loading-uri");
if (!error) expect.fail();
@@ -64,12 +65,26 @@ describe("Tip CTA states", () => {
}
await assertNoPendingUpdate();
+ expect(handler.getCallingQueueState()).eq("empty")
});
it("should be ready for accepting the tip", async () => {
- let tipAccepted = false;
- const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
+ const { handler, mock } = createWalletApiMock();
+
+ handler.addWalletCallResponse(WalletApiOperation.PrepareTip, undefined, {
+ accepted: false,
+ exchangeBaseUrl: "exchange url",
+ merchantBaseUrl: "merchant url",
+ tipAmountEffective: "EUR:1",
+ walletTipId: "tip_id",
+ expirationTimestamp: {
+ t_s: 1
+ },
+ tipAmountRaw: ""
+ });
+
+ const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(
{
@@ -81,58 +96,79 @@ describe("Tip CTA states", () => {
null;
},
},
- {
- prepareTip: async () =>
- ({
- accepted: tipAccepted,
- exchangeBaseUrl: "exchange url",
- merchantBaseUrl: "merchant url",
- tipAmountEffective: "EUR:1",
- walletTipId: "tip_id",
- } as PrepareTipResult as any),
- acceptTip: async () => {
- tipAccepted = true;
- },
- } as any,
+ mock,
),
);
{
- const { status, error } = getLastResultOrThrow();
+ const { status, error } = pullLastResultOrThrow();
expect(status).equals("loading");
expect(error).undefined;
}
- await waitNextUpdate();
+ expect(await waitForStateUpdate()).true;
{
- const state = getLastResultOrThrow();
+ const state = pullLastResultOrThrow();
- if (state.status !== "ready") expect.fail();
+ if (state.status !== "ready") {
+ expect(state).eq({ status: "ready" })
+ return;
+ }
if (state.error) expect.fail();
expect(state.amount).deep.eq(Amounts.parseOrThrow("EUR:1"));
expect(state.merchantBaseUrl).eq("merchant url");
expect(state.exchangeBaseUrl).eq("exchange url");
if (state.accept.onClick === undefined) expect.fail();
+ handler.addWalletCallResponse(WalletApiOperation.AcceptTip);
state.accept.onClick();
}
- await waitNextUpdate();
+ handler.addWalletCallResponse(WalletApiOperation.PrepareTip, undefined, {
+ accepted: true,
+ exchangeBaseUrl: "exchange url",
+ merchantBaseUrl: "merchant url",
+ tipAmountEffective: "EUR:1",
+ walletTipId: "tip_id",
+ expirationTimestamp: {
+ t_s: 1
+ },
+ tipAmountRaw: ""
+ });
+ expect(await waitForStateUpdate()).true;
+
{
- const state = getLastResultOrThrow();
+ const state = pullLastResultOrThrow();
- if (state.status !== "accepted") expect.fail();
+ if (state.status !== "accepted") {
+ expect(state).eq({ status: "accepted" })
+ return;
+ }
if (state.error) expect.fail();
expect(state.amount).deep.eq(Amounts.parseOrThrow("EUR:1"));
expect(state.merchantBaseUrl).eq("merchant url");
expect(state.exchangeBaseUrl).eq("exchange url");
}
await assertNoPendingUpdate();
+ expect(handler.getCallingQueueState()).eq("empty")
});
it("should be ignored after clicking the ignore button", async () => {
- const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
+ const { handler, mock } = createWalletApiMock();
+ handler.addWalletCallResponse(WalletApiOperation.PrepareTip, undefined, {
+ exchangeBaseUrl: "exchange url",
+ merchantBaseUrl: "merchant url",
+ tipAmountEffective: "EUR:1",
+ walletTipId: "tip_id",
+ accepted: false,
+ expirationTimestamp: {
+ t_s: 1,
+ },
+ tipAmountRaw: ""
+ });
+
+ const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(
{
@@ -144,52 +180,48 @@ describe("Tip CTA states", () => {
null;
},
},
- {
- prepareTip: async () =>
- ({
- exchangeBaseUrl: "exchange url",
- merchantBaseUrl: "merchant url",
- tipAmountEffective: "EUR:1",
- walletTipId: "tip_id",
- } as PrepareTipResult as any),
- acceptTip: async () => ({}),
- } as any,
+ mock,
),
);
{
- const { status, error } = getLastResultOrThrow();
+ const { status, error } = pullLastResultOrThrow();
expect(status).equals("loading");
expect(error).undefined;
}
- await waitNextUpdate();
+ expect(await waitForStateUpdate()).true;
{
- const state = getLastResultOrThrow();
+ const state = pullLastResultOrThrow();
if (state.status !== "ready") expect.fail();
if (state.error) expect.fail();
expect(state.amount).deep.eq(Amounts.parseOrThrow("EUR:1"));
expect(state.merchantBaseUrl).eq("merchant url");
expect(state.exchangeBaseUrl).eq("exchange url");
- // if (state.ignore.onClick === undefined) expect.fail();
-
- // state.ignore.onClick();
}
- // await waitNextUpdate();
- // {
- // const state = getLastResultOrThrow();
-
- // if (state.status !== "ignored") expect.fail();
- // if (state.error) expect.fail();
- // }
await assertNoPendingUpdate();
+ expect(handler.getCallingQueueState()).eq("empty")
});
it("should render accepted if the tip has been used previously", async () => {
- const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
+ const { handler, mock } = createWalletApiMock();
+
+ handler.addWalletCallResponse(WalletApiOperation.PrepareTip, undefined, {
+ accepted: true,
+ exchangeBaseUrl: "exchange url",
+ merchantBaseUrl: "merchant url",
+ tipAmountEffective: "EUR:1",
+ walletTipId: "tip_id",
+ expirationTimestamp: {
+ t_s: 1,
+ },
+ tipAmountRaw: "",
+ });
+
+ const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(
{
@@ -201,30 +233,20 @@ describe("Tip CTA states", () => {
null;
},
},
- {
- prepareTip: async () =>
- ({
- accepted: true,
- exchangeBaseUrl: "exchange url",
- merchantBaseUrl: "merchant url",
- tipAmountEffective: "EUR:1",
- walletTipId: "tip_id",
- } as PrepareTipResult as any),
- acceptTip: async () => ({}),
- } as any,
+ mock,
),
);
{
- const { status, error } = getLastResultOrThrow();
+ const { status, error } = pullLastResultOrThrow();
expect(status).equals("loading");
expect(error).undefined;
}
- await waitNextUpdate();
+ expect(await waitForStateUpdate()).true;
{
- const state = getLastResultOrThrow();
+ const state = pullLastResultOrThrow();
if (state.status !== "accepted") expect.fail();
if (state.error) expect.fail();
@@ -233,5 +255,6 @@ describe("Tip CTA states", () => {
expect(state.exchangeBaseUrl).eq("exchange url");
}
await assertNoPendingUpdate();
+ expect(handler.getCallingQueueState()).eq("empty")
});
});