diff options
author | Sebastian <sebasjm@gmail.com> | 2022-10-25 12:23:08 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-10-25 19:19:09 -0300 |
commit | a286649b0a611d87916a178a795c1acd2917741c (patch) | |
tree | 86d0a8eefb791ffebc877a88d6876d1ff764cf2b /packages/taler-wallet-webextension/src/wallet/DepositPage | |
parent | 5fcd434f662bcef7e9e043763766c8b104ad6b97 (diff) | |
download | wallet-core-a286649b0a611d87916a178a795c1acd2917741c.tar.xz |
cherry-pick: using new wallet typed api missing test
Diffstat (limited to 'packages/taler-wallet-webextension/src/wallet/DepositPage')
3 files changed, 246 insertions, 364 deletions
diff --git a/packages/taler-wallet-webextension/src/wallet/DepositPage/index.ts b/packages/taler-wallet-webextension/src/wallet/DepositPage/index.ts index 81d401a70..77661fe15 100644 --- a/packages/taler-wallet-webextension/src/wallet/DepositPage/index.ts +++ b/packages/taler-wallet-webextension/src/wallet/DepositPage/index.ts @@ -14,26 +14,25 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +import { AmountJson, PaytoUri } from "@gnu-taler/taler-util"; import { Loading } from "../../components/Loading.js"; import { HookError } from "../../hooks/useAsyncAsHook.js"; +import { + ButtonHandler, + SelectFieldHandler, + TextFieldHandler +} from "../../mui/handlers.js"; import { compose, StateViewMap } from "../../utils/index.js"; +import { wxApi } from "../../wxApi.js"; +import { AddAccountPage } from "../AddAccount/index.js"; +import { useComponentState } from "./state.js"; import { AmountOrCurrencyErrorView, LoadingErrorView, NoAccountToDepositView, NoEnoughBalanceView, - ReadyView, + ReadyView } from "./views.js"; -import * as wxApi from "../../wxApi.js"; -import { useComponentState } from "./state.js"; -import { AmountJson, PaytoUri } from "@gnu-taler/taler-util"; -import { - ButtonHandler, - SelectFieldHandler, - TextFieldHandler, - ToggleHandler, -} from "../../mui/handlers.js"; -import { AddAccountPage } from "../AddAccount/index.js"; export interface Props { amount?: string; diff --git a/packages/taler-wallet-webextension/src/wallet/DepositPage/state.ts b/packages/taler-wallet-webextension/src/wallet/DepositPage/state.ts index 57380a632..686cfb4b4 100644 --- a/packages/taler-wallet-webextension/src/wallet/DepositPage/state.ts +++ b/packages/taler-wallet-webextension/src/wallet/DepositPage/state.ts @@ -21,11 +21,12 @@ import { KnownBankAccountsInfo, parsePaytoUri, PaytoUri, - stringifyPaytoUri, + stringifyPaytoUri } from "@gnu-taler/taler-util"; +import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; import { useState } from "preact/hooks"; 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( @@ -36,8 +37,10 @@ export function useComponentState( const currency = parsed !== undefined ? parsed.currency : currencyStr; const hook = useAsyncAsHook(async () => { - const { balances } = await api.getBalance(); - const { accounts } = await api.listKnownBankAccounts(currency); + const { balances } = await api.wallet.call(WalletApiOperation.GetBalances, {}); + const { accounts } = await api.wallet.call(WalletApiOperation.ListKnownBankAccounts, { + currency + }); return { accounts, balances }; }); @@ -127,25 +130,29 @@ export function useComponentState( // const newSelected = !accountMap[accountStr] ? undefined : accountMap[accountStr]; // if (!newSelected) return; const uri = !accountStr ? undefined : parsePaytoUri(accountStr); - setSelectedAccount(uri); if (uri && parsedAmount) { try { const result = await getFeeForAmount(uri, parsedAmount, api); + setSelectedAccount(uri); setFee(result); } catch (e) { + console.error(e) + setSelectedAccount(uri); setFee(undefined); } } } async function updateAmount(numStr: string): Promise<void> { - setAmount(numStr); const parsed = Amounts.parse(`${currency}:${numStr}`); if (parsed && selectedAccount) { try { const result = await getFeeForAmount(selectedAccount, parsed, api); + setAmount(numStr); setFee(result); } catch (e) { + console.error(e) + setAmount(numStr); setFee(undefined); } } @@ -165,10 +172,10 @@ export function useComponentState( const amountError = !isDirty ? undefined : !parsedAmount - ? "Invalid amount" - : Amounts.cmp(balance, parsedAmount) === -1 - ? `Too much, your current balance is ${Amounts.stringifyValue(balance)}` - : undefined; + ? "Invalid amount" + : Amounts.cmp(balance, parsedAmount) === -1 + ? `Too much, your current balance is ${Amounts.stringifyValue(balance)}` + : undefined; const unableToDeposit = !parsedAmount || @@ -176,13 +183,16 @@ export function useComponentState( Amounts.isZero(totalToDeposit) || fee === undefined || amountError !== undefined; + // console.log(parsedAmount, selectedAccount, fee, totalToDeposit, amountError) async function doSend(): Promise<void> { if (!selectedAccount || !parsedAmount || !currency) return; - const account = `payto://${selectedAccount.targetType}/${selectedAccount.targetPath}`; + const depositPaytoUri = `payto://${selectedAccount.targetType}/${selectedAccount.targetPath}`; const amount = Amounts.stringify(parsedAmount); - await api.createDepositGroup(account, amount); + await api.wallet.call(WalletApiOperation.CreateDepositGroup, { + amount, depositPaytoUri + }) onSuccess(currency); } @@ -226,9 +236,11 @@ async function getFeeForAmount( a: AmountJson, api: typeof wxApi, ): Promise<DepositGroupFees> { - const account = `payto://${p.targetType}/${p.targetPath}`; + const depositPaytoUri = `payto://${p.targetType}/${p.targetPath}`; const amount = Amounts.stringify(a); - return await api.getFeeForDeposit(account, amount); + return await api.wallet.call(WalletApiOperation.GetFeeForDeposit, { + amount, depositPaytoUri + }) } export function labelForAccountType(id: string) { diff --git a/packages/taler-wallet-webextension/src/wallet/DepositPage/test.ts b/packages/taler-wallet-webextension/src/wallet/DepositPage/test.ts index 57822cfd0..62097c3e4 100644 --- a/packages/taler-wallet-webextension/src/wallet/DepositPage/test.ts +++ b/packages/taler-wallet-webextension/src/wallet/DepositPage/test.ts @@ -20,101 +20,108 @@ */ import { - Amounts, - Balance, - BalancesResponse, - DepositGroupFees, + Amounts, DepositGroupFees, parsePaytoUri, - stringifyPaytoUri, + stringifyPaytoUri } 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, mountHook, nullFunction } from "../../test-utils.js"; -import * as wxApi from "../../wxApi.js"; import { useComponentState } from "./state.js"; const currency = "EUR"; -const withoutFee = async (): Promise<DepositGroupFees> => ({ +const withoutFee = (): DepositGroupFees => ({ coin: Amounts.parseOrThrow(`${currency}:0`), wire: Amounts.parseOrThrow(`${currency}:0`), refresh: Amounts.parseOrThrow(`${currency}:0`), }); -const withSomeFee = async (): Promise<DepositGroupFees> => ({ +const withSomeFee = (): DepositGroupFees => ({ coin: Amounts.parseOrThrow(`${currency}:1`), wire: Amounts.parseOrThrow(`${currency}:1`), refresh: Amounts.parseOrThrow(`${currency}:1`), }); -const freeJustForIBAN = async (account: string): Promise<DepositGroupFees> => - /IBAN/i.test(account) ? withSomeFee() : withoutFee(); - -const someBalance = [ - { - available: "EUR:10", - } as Balance, -]; - -const nullFunction: any = () => null; -type VoidFunction = () => void; - describe("DepositPage states", () => { it("should have status 'no-enough-balance' when balance is empty", async () => { - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + const { handler, mock } = createWalletApiMock(); + const props = { currency, onCancel: nullFunction, onSuccess: nullFunction } + + handler.addWalletCallResponse(WalletApiOperation.GetBalances, undefined, { + balances: [{ + available: `${currency}:0`, + hasPendingTransactions: false, + pendingIncoming: `${currency}:0`, + pendingOutgoing: `${currency}:0`, + requiresUserInput: false, + }], + }) + handler.addWalletCallResponse(WalletApiOperation.ListKnownBankAccounts, undefined, { + accounts: [] + }); + + const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } = mountHook(() => useComponentState( - { currency, onCancel: nullFunction, onSuccess: nullFunction }, - { - getBalance: async () => - ({ - balances: [{ available: `${currency}:0` }], - } as Partial<BalancesResponse>), - listKnownBankAccounts: async () => ({ accounts: {} }), - } as Partial<typeof wxApi> as any, + props, mock ), ); { - const { status } = getLastResultOrThrow(); + const { status } = pullLastResultOrThrow(); expect(status).equal("loading"); } - await waitNextUpdate(); + expect(await waitForStateUpdate()).true; { - const { status } = getLastResultOrThrow(); + const { status } = pullLastResultOrThrow(); expect(status).equal("no-enough-balance"); } await assertNoPendingUpdate(); + expect(handler.getCallingQueueState()).eq("empty") }); - // it("should have status 'no-accounts' when balance is not empty and accounts is empty", async () => { - // const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = - // mountHook(() => - // useComponentState({ currency, onCancel: nullFunction, onSuccess: nullFunction }, { - // getBalance: async () => - // ({ - // balances: [{ available: `${currency}:1` }], - // } as Partial<BalancesResponse>), - // listKnownBankAccounts: async () => ({ accounts: {} }), - // } as Partial<typeof wxApi> as any), - // ); + it("should have status 'no-accounts' when balance is not empty and accounts is empty", async () => { + const { handler, mock } = createWalletApiMock(); + const props = { currency, onCancel: nullFunction, onSuccess: nullFunction } + + handler.addWalletCallResponse(WalletApiOperation.GetBalances, undefined, { + balances: [{ + available: `${currency}:1`, + hasPendingTransactions: false, + pendingIncoming: `${currency}:0`, + pendingOutgoing: `${currency}:0`, + requiresUserInput: false, + }], + }) + handler.addWalletCallResponse(WalletApiOperation.ListKnownBankAccounts, undefined, { + accounts: [] + }); + const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } = + mountHook(() => + useComponentState( + props, mock + ) + ); - // { - // const { status } = getLastResultOrThrow(); - // expect(status).equal("loading"); - // } + { + const { status } = pullLastResultOrThrow(); + expect(status).equal("loading"); + } - // await waitNextUpdate(); - // { - // const r = getLastResultOrThrow(); - // if (r.status !== "no-accounts") expect.fail(); - // expect(r.cancelHandler.onClick).not.undefined; - // } + expect(await waitForStateUpdate()).true; + { + const r = pullLastResultOrThrow(); + if (r.status !== "no-accounts") expect.fail(); + // expect(r.cancelHandler.onClick).not.undefined; + } - // await assertNoPendingUpdate(); - // }); + await assertNoPendingUpdate(); + expect(handler.getCallingQueueState()).eq("empty") + }); const ibanPayto = { uri: parsePaytoUri("payto://iban/ES8877998399652238")!, @@ -130,29 +137,38 @@ describe("DepositPage states", () => { }; it("should have status 'ready' but unable to deposit ", async () => { - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + const { handler, mock } = createWalletApiMock(); + const props = { currency, onCancel: nullFunction, onSuccess: nullFunction } + + handler.addWalletCallResponse(WalletApiOperation.GetBalances, undefined, { + balances: [{ + available: `${currency}:1`, + hasPendingTransactions: false, + pendingIncoming: `${currency}:0`, + pendingOutgoing: `${currency}:0`, + requiresUserInput: false, + }], + }) + handler.addWalletCallResponse(WalletApiOperation.ListKnownBankAccounts, undefined, { + accounts: [ibanPayto] + }); + + const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } = mountHook(() => useComponentState( - { currency, onCancel: nullFunction, onSuccess: nullFunction }, - { - getBalance: async () => - ({ - balances: [{ available: `${currency}:1` }], - } as Partial<BalancesResponse>), - listKnownBankAccounts: async () => ({ accounts: [ibanPayto] }), - } as Partial<typeof wxApi> as any, + props, mock ), ); { - const { status } = getLastResultOrThrow(); + const { status } = pullLastResultOrThrow(); expect(status).equal("loading"); } - await waitNextUpdate(); + expect(await waitForStateUpdate()).true; { - const r = getLastResultOrThrow(); + const r = pullLastResultOrThrow(); if (r.status !== "ready") expect.fail(); expect(r.cancelHandler.onClick).not.undefined; expect(r.currency).eq(currency); @@ -162,33 +178,46 @@ describe("DepositPage states", () => { } await assertNoPendingUpdate(); + expect(handler.getCallingQueueState()).eq("empty") }); - it.skip("should not be able to deposit more than the balance ", async () => { - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + it("should not be able to deposit more than the balance ", async () => { + const { handler, mock } = createWalletApiMock(); + const props = { currency, onCancel: nullFunction, onSuccess: nullFunction } + + handler.addWalletCallResponse(WalletApiOperation.GetBalances, undefined, { + balances: [{ + available: `${currency}:5`, + hasPendingTransactions: false, + pendingIncoming: `${currency}:0`, + pendingOutgoing: `${currency}:0`, + requiresUserInput: false, + }], + }) + handler.addWalletCallResponse(WalletApiOperation.ListKnownBankAccounts, undefined, { + accounts: [ibanPayto] + }); + handler.addWalletCallResponse(WalletApiOperation.GetFeeForDeposit, undefined, withoutFee()) + handler.addWalletCallResponse(WalletApiOperation.GetFeeForDeposit, undefined, withoutFee()) + handler.addWalletCallResponse(WalletApiOperation.GetFeeForDeposit, undefined, withoutFee()) + + const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } = mountHook(() => useComponentState( - { currency, onCancel: nullFunction, onSuccess: nullFunction }, - { - getBalance: async () => - ({ - balances: [{ available: `${currency}:1` }], - } as Partial<BalancesResponse>), - listKnownBankAccounts: async () => ({ accounts: [ibanPayto] }), - getFeeForDeposit: withoutFee, - } as Partial<typeof wxApi> as any, + props, mock ), ); { - const { status } = getLastResultOrThrow(); + const { status } = pullLastResultOrThrow(); expect(status).equal("loading"); } - await waitNextUpdate(); + expect(await waitForStateUpdate()).true; + const accountSelected = stringifyPaytoUri(ibanPayto.uri) { - const r = getLastResultOrThrow(); + const r = pullLastResultOrThrow(); if (r.status !== "ready") expect.fail(); expect(r.cancelHandler.onClick).not.undefined; expect(r.currency).eq(currency); @@ -196,32 +225,20 @@ describe("DepositPage states", () => { expect(r.amount.value).eq("0"); expect(r.depositHandler.onClick).undefined; expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); + expect(r.account.onChange).not.undefined; - r.amount.onInput("10"); + r.account.onChange!(accountSelected) } - await waitNextUpdate(); + expect(await waitForStateUpdate()).true; { - const r = getLastResultOrThrow(); + const r = pullLastResultOrThrow(); if (r.status !== "ready") expect.fail(); expect(r.cancelHandler.onClick).not.undefined; expect(r.currency).eq(currency); - expect(r.account.value).eq(""); - expect(r.amount.value).eq("10"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); - expect(r.depositHandler.onClick).undefined; - } - - await waitNextUpdate(); - - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(""); - expect(r.amount.value).eq("10"); + expect(r.account.value).eq(accountSelected); + expect(r.amount.value).eq("0"); expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); expect(r.depositHandler.onClick).undefined; } @@ -229,100 +246,110 @@ describe("DepositPage states", () => { await assertNoPendingUpdate(); }); - it.skip("should calculate the fee upon entering amount ", async () => { - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = - mountHook(() => - useComponentState( - { currency, onCancel: nullFunction, onSuccess: nullFunction }, - { - getBalance: async () => - ({ - balances: [{ available: `${currency}:1` }], - } as Partial<BalancesResponse>), - listKnownBankAccounts: async () => ({ accounts: [ibanPayto] }), - getFeeForDeposit: withSomeFee, - } as Partial<typeof wxApi> as any, - ), - ); + // it("should calculate the fee upon entering amount ", async () => { + // const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } = + // mountHook(() => + // useComponentState( + // { currency, onCancel: nullFunction, onSuccess: nullFunction }, + // { + // getBalance: async () => + // ({ + // balances: [{ available: `${currency}:1` }], + // } as Partial<BalancesResponse>), + // listKnownBankAccounts: async () => ({ accounts: [ibanPayto] }), + // getFeeForDeposit: withSomeFee, + // } as Partial<typeof wxApi> as any, + // ), + // ); - { - const { status } = getLastResultOrThrow(); - expect(status).equal("loading"); - } + // { + // const { status } = getLastResultOrThrow(); + // expect(status).equal("loading"); + // } - await waitNextUpdate(); + // await waitNextUpdate(); - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(""); - expect(r.amount.value).eq("0"); - expect(r.depositHandler.onClick).undefined; - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); + // { + // const r = getLastResultOrThrow(); + // if (r.status !== "ready") expect.fail(); + // expect(r.cancelHandler.onClick).not.undefined; + // expect(r.currency).eq(currency); + // expect(r.account.value).eq(accountSelected); + // expect(r.amount.value).eq("0"); + // expect(r.depositHandler.onClick).undefined; + // expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); - r.amount.onInput("10"); - } + // r.amount.onInput("10"); + // } - await waitNextUpdate(); + // expect(await waitForStateUpdate()).true; - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(""); - expect(r.amount.value).eq("10"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:10`)); - expect(r.depositHandler.onClick).undefined; - } + // { + // const r = pullLastResultOrThrow(); + // if (r.status !== "ready") expect.fail(); + // expect(r.cancelHandler.onClick).not.undefined; + // expect(r.currency).eq(currency); + // expect(r.account.value).eq(accountSelected); + // expect(r.amount.value).eq("10"); + // expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); + // expect(r.depositHandler.onClick).undefined; - await waitNextUpdate(); + // r.amount.onInput("3"); + // } - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(""); - expect(r.amount.value).eq("10"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:7`)); - expect(r.depositHandler.onClick).undefined; - } + // expect(await waitForStateUpdate()).true; - await assertNoPendingUpdate(); - }); + // { + // const r = pullLastResultOrThrow(); + // if (r.status !== "ready") expect.fail(); + // expect(r.cancelHandler.onClick).not.undefined; + // expect(r.currency).eq(currency); + // expect(r.account.value).eq(accountSelected); + // expect(r.amount.value).eq("3"); + // expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); + // expect(r.depositHandler.onClick).not.undefined; + // } - it.skip("should calculate the fee upon selecting account ", async () => { - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + // await assertNoPendingUpdate(); + // expect(handler.getCallingQueueState()).eq("empty") + // }); + + it("should calculate the fee upon entering amount ", async () => { + const { handler, mock } = createWalletApiMock(); + const props = { currency, onCancel: nullFunction, onSuccess: nullFunction } + + handler.addWalletCallResponse(WalletApiOperation.GetBalances, undefined, { + balances: [{ + available: `${currency}:10`, + hasPendingTransactions: false, + pendingIncoming: `${currency}:0`, + pendingOutgoing: `${currency}:0`, + requiresUserInput: false, + }], + }) + handler.addWalletCallResponse(WalletApiOperation.ListKnownBankAccounts, undefined, { + accounts: [ibanPayto] + }); + handler.addWalletCallResponse(WalletApiOperation.GetFeeForDeposit, undefined, withSomeFee()) + handler.addWalletCallResponse(WalletApiOperation.GetFeeForDeposit, undefined, withSomeFee()) + + const { pullLastResultOrThrow, waitForStateUpdate, assertNoPendingUpdate } = mountHook(() => useComponentState( - { currency, onCancel: nullFunction, onSuccess: nullFunction }, - { - getBalance: async () => - ({ - balances: [{ available: `${currency}:1` }], - } as Partial<BalancesResponse>), - listKnownBankAccounts: async () => ({ - accounts: [ibanPayto, talerBankPayto], - }), - getFeeForDeposit: freeJustForIBAN, - } as Partial<typeof wxApi> as any, + props, mock ), ); { - const { status } = getLastResultOrThrow(); + const { status } = pullLastResultOrThrow(); expect(status).equal("loading"); } - await waitNextUpdate(); + expect(await waitForStateUpdate()).true; + const accountSelected = stringifyPaytoUri(ibanPayto.uri) { - const r = getLastResultOrThrow(); + const r = pullLastResultOrThrow(); if (r.status !== "ready") expect.fail(); expect(r.cancelHandler.onClick).not.undefined; expect(r.currency).eq(currency); @@ -330,198 +357,42 @@ describe("DepositPage states", () => { expect(r.amount.value).eq("0"); expect(r.depositHandler.onClick).undefined; expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); + expect(r.account.onChange).not.undefined; - if (r.account.onChange === undefined) expect.fail(); - r.account.onChange(stringifyPaytoUri(ibanPayto.uri)); - } - - await waitNextUpdate(""); - - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(stringifyPaytoUri(ibanPayto.uri)); - expect(r.amount.value).eq("0"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); - expect(r.depositHandler.onClick).undefined; + r.account.onChange!(accountSelected) } - await waitNextUpdate(""); + expect(await waitForStateUpdate()).true; { - const r = getLastResultOrThrow(); + const r = pullLastResultOrThrow(); if (r.status !== "ready") expect.fail(); expect(r.cancelHandler.onClick).not.undefined; expect(r.currency).eq(currency); - expect(r.account.value).eq(stringifyPaytoUri(ibanPayto.uri)); + expect(r.account.value).eq(accountSelected); expect(r.amount.value).eq("0"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); expect(r.depositHandler.onClick).undefined; - - r.amount.onInput("10"); - } - - await waitNextUpdate(""); - - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(stringifyPaytoUri(ibanPayto.uri)); - expect(r.amount.value).eq("10"); expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:7`)); - expect(r.depositHandler.onClick).undefined; - } - - await waitNextUpdate(""); - - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(stringifyPaytoUri(ibanPayto.uri)); - expect(r.amount.value).eq("10"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:7`)); - expect(r.depositHandler.onClick).undefined; - - if (r.account.onChange === undefined) expect.fail(); - r.account.onChange(stringifyPaytoUri(talerBankPayto.uri)); - } - - await waitNextUpdate(""); - - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(stringifyPaytoUri(talerBankPayto.uri)); - expect(r.amount.value).eq("10"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:7`)); - expect(r.depositHandler.onClick).undefined; - } - - await waitNextUpdate(""); - - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(stringifyPaytoUri(talerBankPayto.uri)); - expect(r.amount.value).eq("10"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:10`)); - expect(r.depositHandler.onClick).undefined; - } - - await assertNoPendingUpdate(); - }); - - it.skip("should be able to deposit if has the enough balance ", async () => { - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = - mountHook(() => - useComponentState( - { currency, onCancel: nullFunction, onSuccess: nullFunction }, - { - getBalance: async () => - ({ - balances: [{ available: `${currency}:15` }], - } as Partial<BalancesResponse>), - listKnownBankAccounts: async () => ({ accounts: [ibanPayto] }), - getFeeForDeposit: withSomeFee, - } as Partial<typeof wxApi> as any, - ), - ); - - { - const { status } = getLastResultOrThrow(); - expect(status).equal("loading"); - } - - await waitNextUpdate(); - - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(""); - expect(r.amount.value).eq("0"); - expect(r.depositHandler.onClick).undefined; - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); r.amount.onInput("10"); } - await waitNextUpdate(); + expect(await waitForStateUpdate()).true; { - const r = getLastResultOrThrow(); + const r = pullLastResultOrThrow(); if (r.status !== "ready") expect.fail(); expect(r.cancelHandler.onClick).not.undefined; expect(r.currency).eq(currency); - expect(r.account.value).eq(""); - expect(r.amount.value).eq("10"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:0`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:10`)); - expect(r.depositHandler.onClick).undefined; - } - - await waitNextUpdate(); - - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(""); + expect(r.account.value).eq(accountSelected); expect(r.amount.value).eq("10"); expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`)); expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:7`)); expect(r.depositHandler.onClick).not.undefined; - - r.amount.onInput("13"); - } - - await waitNextUpdate(); - - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(""); - expect(r.amount.value).eq("13"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:10`)); - expect(r.depositHandler.onClick).not.undefined; - } - - await waitNextUpdate(); - - { - const r = getLastResultOrThrow(); - if (r.status !== "ready") expect.fail(); - expect(r.cancelHandler.onClick).not.undefined; - expect(r.currency).eq(currency); - expect(r.account.value).eq(""); - expect(r.amount.value).eq("13"); - expect(r.totalFee).deep.eq(Amounts.parseOrThrow(`${currency}:3`)); - expect(r.totalToDeposit).deep.eq(Amounts.parseOrThrow(`${currency}:10`)); - expect(r.depositHandler.onClick).not.undefined; } await assertNoPendingUpdate(); + expect(handler.getCallingQueueState()).eq("empty") }); + }); |