wallet-core/packages/taler-wallet-webextension/src/wallet/DepositPage.test.ts

431 lines
14 KiB
TypeScript
Raw Normal View History

2022-03-23 21:50:06 +01:00
/*
This file is part of GNU Taler
2022-06-06 17:05:26 +02:00
(C) 2022 Taler Systems S.A.
2022-03-23 21:50:06 +01:00
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
*
* @author Sebastian Javier Marchano (sebasjm)
*/
2022-06-06 05:09:25 +02:00
import {
Amounts,
Balance,
BalancesResponse,
parsePaytoUri,
} from "@gnu-taler/taler-util";
2022-03-29 17:18:58 +02:00
import { DepositGroupFees } from "@gnu-taler/taler-wallet-core/src/operations/deposits";
2022-03-23 21:50:06 +01:00
import { expect } from "chai";
2022-03-29 04:41:07 +02:00
import { mountHook } from "../test-utils.js";
import { useComponentState } from "./DepositPage.js";
2022-04-22 21:10:21 +02:00
import * as wxApi from "../wxApi.js";
2022-03-23 21:50:06 +01:00
2022-06-06 05:09:25 +02:00
const currency = "EUR";
2022-04-22 21:10:21 +02:00
const withoutFee = async (): Promise<DepositGroupFees> => ({
coin: Amounts.parseOrThrow(`${currency}:0`),
wire: Amounts.parseOrThrow(`${currency}:0`),
2022-06-06 05:09:25 +02:00
refresh: Amounts.parseOrThrow(`${currency}:0`),
});
2022-04-22 21:10:21 +02:00
const withSomeFee = async (): Promise<DepositGroupFees> => ({
2022-03-23 21:50:06 +01:00
coin: Amounts.parseOrThrow(`${currency}:1`),
wire: Amounts.parseOrThrow(`${currency}:1`),
2022-06-06 05:09:25 +02:00
refresh: Amounts.parseOrThrow(`${currency}:1`),
});
2022-03-23 21:50:06 +01:00
2022-06-06 05:09:25 +02:00
const freeJustForIBAN = async (account: string): Promise<DepositGroupFees> =>
/IBAN/i.test(account) ? withoutFee() : withSomeFee();
2022-04-22 21:10:21 +02:00
2022-06-06 05:09:25 +02:00
const someBalance = [
{
available: "EUR:10",
} as Balance,
];
2022-03-23 21:50:06 +01:00
2022-04-22 21:10:21 +02:00
const nullFunction: any = () => null;
type VoidFunction = () => void;
2022-03-23 21:50:06 +01:00
describe("DepositPage states", () => {
2022-04-22 21:10:21 +02:00
it("should have status 'no-balance' when balance is empty", async () => {
2022-06-06 05:09:25 +02:00
const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(currency, nullFunction, nullFunction, {
getBalance: async () =>
2022-06-06 17:05:26 +02:00
({
balances: [{ available: `${currency}:0` }],
} as Partial<BalancesResponse>),
2022-06-06 05:09:25 +02:00
listKnownBankAccounts: async () => ({ accounts: [] }),
} as Partial<typeof wxApi> as any),
);
2022-03-23 21:50:06 +01:00
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const { status } = getLastResultOrThrow();
expect(status).equal("loading");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const { status } = getLastResultOrThrow();
expect(status).equal("no-balance");
2022-03-23 21:50:06 +01:00
}
2022-06-06 05:09:25 +02:00
await assertNoPendingUpdate();
2022-04-22 21:10:21 +02:00
});
it("should have status 'no-accounts' when balance is not empty and accounts is empty", async () => {
2022-06-06 05:09:25 +02:00
const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(currency, nullFunction, nullFunction, {
getBalance: async () =>
2022-06-06 17:05:26 +02:00
({
balances: [{ available: `${currency}:1` }],
} as Partial<BalancesResponse>),
2022-06-06 05:09:25 +02:00
listKnownBankAccounts: async () => ({ accounts: [] }),
} as Partial<typeof wxApi> as any),
);
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const { status } = getLastResultOrThrow();
expect(status).equal("loading");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "no-accounts") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
}
2022-06-06 05:09:25 +02:00
await assertNoPendingUpdate();
2022-04-22 21:10:21 +02:00
});
const ibanPayto = parsePaytoUri("payto://iban/ES8877998399652238")!;
2022-06-06 05:09:25 +02:00
const talerBankPayto = parsePaytoUri(
"payto://x-taler-bank/ES8877998399652238",
)!;
2022-04-22 21:10:21 +02:00
it("should have status 'ready' but unable to deposit ", async () => {
2022-06-06 05:09:25 +02:00
const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(currency, nullFunction, nullFunction, {
getBalance: async () =>
2022-06-06 17:05:26 +02:00
({
balances: [{ available: `${currency}:1` }],
} as Partial<BalancesResponse>),
2022-06-06 05:09:25 +02:00
listKnownBankAccounts: async () => ({ accounts: [ibanPayto] }),
} as Partial<typeof wxApi> as any),
);
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const { status } = getLastResultOrThrow();
expect(status).equal("loading");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("0");
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
}
2022-06-06 05:09:25 +02:00
await assertNoPendingUpdate();
2022-04-22 21:10:21 +02:00
});
it("should not be able to deposit more than the balance ", async () => {
2022-06-06 05:09:25 +02:00
const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(currency, nullFunction, nullFunction, {
getBalance: async () =>
2022-06-06 17:05:26 +02:00
({
balances: [{ available: `${currency}:1` }],
} as Partial<BalancesResponse>),
2022-06-06 05:09:25 +02:00
listKnownBankAccounts: async () => ({ accounts: [ibanPayto] }),
getFeeForDeposit: withoutFee,
} as Partial<typeof wxApi> as any),
);
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const { status } = getLastResultOrThrow();
expect(status).equal("loading");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("0");
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
2022-06-06 05:09:25 +02:00
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`));
2022-04-22 21:10:21 +02:00
2022-06-06 05:09:25 +02:00
r.amount.onInput("10");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("10");
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`));
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
}
2022-06-06 05:09:25 +02:00
await assertNoPendingUpdate();
2022-04-22 21:10:21 +02:00
});
it("should calculate the fee upon entering amount ", async () => {
2022-06-06 05:09:25 +02:00
const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(currency, nullFunction, nullFunction, {
getBalance: async () =>
2022-06-06 17:05:26 +02:00
({
balances: [{ available: `${currency}:1` }],
} as Partial<BalancesResponse>),
2022-06-06 05:09:25 +02:00
listKnownBankAccounts: async () => ({ accounts: [ibanPayto] }),
getFeeForDeposit: withSomeFee,
} as Partial<typeof wxApi> as any),
);
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const { status } = getLastResultOrThrow();
expect(status).equal("loading");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("0");
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
2022-06-06 05:09:25 +02:00
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`));
2022-04-22 21:10:21 +02:00
2022-06-06 05:09:25 +02:00
r.amount.onInput("10");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("10");
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`));
expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:7`));
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
}
2022-06-06 05:09:25 +02:00
await assertNoPendingUpdate();
2022-04-22 21:10:21 +02:00
});
it("should calculate the fee upon selecting account ", async () => {
2022-06-06 05:09:25 +02:00
const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(currency, nullFunction, nullFunction, {
getBalance: async () =>
2022-06-06 17:05:26 +02:00
({
balances: [{ available: `${currency}:1` }],
} as Partial<BalancesResponse>),
2022-06-06 05:09:25 +02:00
listKnownBankAccounts: async () => ({
accounts: [ibanPayto, talerBankPayto],
}),
getFeeForDeposit: freeJustForIBAN,
} as Partial<typeof wxApi> as any),
);
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const { status } = getLastResultOrThrow();
expect(status).equal("loading");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("0");
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
2022-06-06 05:09:25 +02:00
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`));
2022-04-22 21:10:21 +02:00
if (r.account.onChange === undefined) expect.fail();
2022-06-06 05:09:25 +02:00
r.account.onChange("1");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("1");
expect(r.amount.value).eq("0");
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`));
expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:0`));
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
2022-06-06 05:09:25 +02:00
r.amount.onInput("10");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("1");
expect(r.amount.value).eq("10");
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`));
expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:7`));
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
if (r.account.onChange === undefined) expect.fail();
2022-06-06 05:09:25 +02:00
r.account.onChange("0");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("10");
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`));
expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:10`));
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
}
2022-06-06 05:09:25 +02:00
await assertNoPendingUpdate();
2022-03-23 21:50:06 +01:00
});
2022-04-22 21:10:21 +02:00
it("should be able to deposit if has the enough balance ", async () => {
2022-06-06 05:09:25 +02:00
const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } =
mountHook(() =>
useComponentState(currency, nullFunction, nullFunction, {
getBalance: async () =>
2022-06-06 17:05:26 +02:00
({
balances: [{ available: `${currency}:15` }],
} as Partial<BalancesResponse>),
2022-06-06 05:09:25 +02:00
listKnownBankAccounts: async () => ({ accounts: [ibanPayto] }),
getFeeForDeposit: withSomeFee,
} as Partial<typeof wxApi> as any),
);
2022-03-23 21:50:06 +01:00
{
2022-06-06 05:09:25 +02:00
const { status } = getLastResultOrThrow();
expect(status).equal("loading");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("0");
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
2022-06-06 05:09:25 +02:00
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`));
2022-04-22 21:10:21 +02:00
2022-06-06 05:09:25 +02:00
r.amount.onInput("10");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("10");
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`));
expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:7`));
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).not.undefined;
2022-06-06 05:09:25 +02:00
r.amount.onInput("13");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("13");
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`));
expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:10`));
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).not.undefined;
2022-06-06 05:09:25 +02:00
r.amount.onInput("15");
2022-03-23 21:50:06 +01:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("15");
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`));
expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:12`));
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).not.undefined;
2022-06-06 05:09:25 +02:00
r.amount.onInput("17");
2022-04-22 21:10:21 +02:00
}
2022-06-06 05:09:25 +02:00
await waitNextUpdate();
2022-04-22 21:10:21 +02:00
{
2022-06-06 05:09:25 +02:00
const r = getLastResultOrThrow();
2022-04-22 21:10:21 +02:00
if (r.status !== "ready") expect.fail();
expect(r.cancelHandler.onClick).not.undefined;
expect(r.currency).eq(currency);
2022-06-06 05:09:25 +02:00
expect(r.account.value).eq("0");
expect(r.amount.value).eq("17");
expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`));
expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:14`));
2022-04-22 21:10:21 +02:00
expect(r.depositHandler.onClick).undefined;
}
2022-06-06 05:09:25 +02:00
await assertNoPendingUpdate();
2022-03-23 21:50:06 +01:00
});
2022-06-06 05:09:25 +02:00
});