diff options
author | Sebastian <sebasjm@gmail.com> | 2022-06-06 00:09:25 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-06-06 00:09:25 -0300 |
commit | fb6aff76d2152d95caa2ba6cb0d91cb01e0687fe (patch) | |
tree | 2dc9b0a4935f60ba7229fd81332ec4a41ad4698c /packages/taler-wallet-webextension/src/cta/Pay.test.ts | |
parent | 912813fd09ee2f757e4d6d509a911159b46153a7 (diff) | |
download | wallet-core-fb6aff76d2152d95caa2ba6cb0d91cb01e0687fe.tar.xz |
prettier
Diffstat (limited to 'packages/taler-wallet-webextension/src/cta/Pay.test.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/cta/Pay.test.ts | 471 |
1 files changed, 256 insertions, 215 deletions
diff --git a/packages/taler-wallet-webextension/src/cta/Pay.test.ts b/packages/taler-wallet-webextension/src/cta/Pay.test.ts index 7e9d5338f..a45ebd3a2 100644 --- a/packages/taler-wallet-webextension/src/cta/Pay.test.ts +++ b/packages/taler-wallet-webextension/src/cta/Pay.test.ts @@ -19,7 +19,16 @@ * @author Sebastian Javier Marchano (sebasjm) */ -import { AmountJson, Amounts, BalancesResponse, ConfirmPayResult, ConfirmPayResultType, NotificationType, PreparePayResult, PreparePayResultType } from "@gnu-taler/taler-util"; +import { + AmountJson, + Amounts, + BalancesResponse, + ConfirmPayResult, + ConfirmPayResultType, + NotificationType, + PreparePayResult, + PreparePayResultType, +} from "@gnu-taler/taler-util"; import { expect } from "chai"; import { mountHook } from "../test-utils.js"; import * as wxApi from "../wxApi.js"; @@ -29,8 +38,8 @@ const nullFunction: any = () => null; type VoidFunction = () => void; type Subs = { - [key in NotificationType]?: VoidFunction -} + [key in NotificationType]?: VoidFunction; +}; export class SubsHandler { private subs: Subs = {}; @@ -39,311 +48,340 @@ export class SubsHandler { this.saveSubscription = this.saveSubscription.bind(this); } - saveSubscription(messageTypes: NotificationType[], callback: VoidFunction): VoidFunction { - messageTypes.forEach(m => { + saveSubscription( + messageTypes: NotificationType[], + callback: VoidFunction, + ): VoidFunction { + messageTypes.forEach((m) => { this.subs[m] = callback; - }) + }); return nullFunction; } notifyEvent(event: NotificationType): void { const cb = this.subs[event]; - if (cb === undefined) expect.fail(`Expected to have a subscription for ${event}`); - cb() + if (cb === undefined) + expect.fail(`Expected to have a subscription for ${event}`); + cb(); } } - describe("Pay CTA states", () => { it("should tell the user that the URI is missing", async () => { - - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = mountHook(() => - useComponentState(undefined, { - onUpdateNotification: nullFunction, - } as Partial<typeof wxApi> as any) - ); + const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + mountHook(() => + useComponentState(undefined, { + onUpdateNotification: nullFunction, + } as Partial<typeof wxApi> as any), + ); { - const { status, hook } = getLastResultOrThrow() - expect(status).equals('loading') + const { status, hook } = getLastResultOrThrow(); + expect(status).equals("loading"); expect(hook).undefined; } - await waitNextUpdate() + await waitNextUpdate(); { - const { status, hook } = getLastResultOrThrow() + const { status, hook } = getLastResultOrThrow(); - expect(status).equals('loading') - if (hook === undefined) expect.fail() + expect(status).equals("loading"); + if (hook === undefined) expect.fail(); expect(hook.hasError).true; expect(hook.operational).false; } - await assertNoPendingUpdate() + await assertNoPendingUpdate(); }); it("should response with no balance", async () => { - - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = mountHook(() => - useComponentState('taller://pay', { - onUpdateNotification: nullFunction, - preparePay: async () => ({ - amountRaw: 'USD:10', - status: PreparePayResultType.InsufficientBalance, - } as Partial<PreparePayResult>), - getBalance: async () => ({ - balances: [] - } as Partial<BalancesResponse>), - } as Partial<typeof wxApi> as any) - ); + const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + mountHook(() => + useComponentState("taller://pay", { + onUpdateNotification: nullFunction, + preparePay: async () => + ({ + amountRaw: "USD:10", + status: PreparePayResultType.InsufficientBalance, + } as Partial<PreparePayResult>), + getBalance: async () => + ({ + balances: [], + } as Partial<BalancesResponse>), + } as Partial<typeof wxApi> as any), + ); { - const { status, hook } = getLastResultOrThrow() - expect(status).equals('loading') + const { status, hook } = getLastResultOrThrow(); + expect(status).equals("loading"); expect(hook).undefined; } await waitNextUpdate(); { - const r = getLastResultOrThrow() - if (r.status !== 'ready') expect.fail() + const r = getLastResultOrThrow(); + if (r.status !== "ready") expect.fail(); expect(r.balance).undefined; - expect(r.amount).deep.equal(Amounts.parseOrThrow('USD:10')) + expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:10")); expect(r.payHandler.onClick).undefined; } - await assertNoPendingUpdate() + await assertNoPendingUpdate(); }); it("should not be able to pay if there is no enough balance", async () => { - - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = mountHook(() => - useComponentState('taller://pay', { - onUpdateNotification: nullFunction, - preparePay: async () => ({ - amountRaw: 'USD:10', - status: PreparePayResultType.InsufficientBalance, - } as Partial<PreparePayResult>), - getBalance: async () => ({ - balances: [{ - available: 'USD:5' - }] - } as Partial<BalancesResponse>), - } as Partial<typeof wxApi> as any) - ); + const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + mountHook(() => + useComponentState("taller://pay", { + onUpdateNotification: nullFunction, + preparePay: async () => + ({ + amountRaw: "USD:10", + status: PreparePayResultType.InsufficientBalance, + } as Partial<PreparePayResult>), + getBalance: async () => + ({ + balances: [ + { + available: "USD:5", + }, + ], + } as Partial<BalancesResponse>), + } as Partial<typeof wxApi> as any), + ); { - const { status, hook } = getLastResultOrThrow() - expect(status).equals('loading') + const { status, hook } = getLastResultOrThrow(); + expect(status).equals("loading"); expect(hook).undefined; } await waitNextUpdate(); { - const r = getLastResultOrThrow() - if (r.status !== 'ready') expect.fail() - expect(r.balance).deep.equal(Amounts.parseOrThrow('USD:5')); - expect(r.amount).deep.equal(Amounts.parseOrThrow('USD:10')) + const r = getLastResultOrThrow(); + if (r.status !== "ready") expect.fail(); + expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:5")); + expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:10")); expect(r.payHandler.onClick).undefined; } - await assertNoPendingUpdate() + await assertNoPendingUpdate(); }); it("should be able to pay (without fee)", async () => { - - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = mountHook(() => - useComponentState('taller://pay', { - onUpdateNotification: nullFunction, - preparePay: async () => ({ - amountRaw: 'USD:10', - amountEffective: 'USD:10', - status: PreparePayResultType.PaymentPossible, - } as Partial<PreparePayResult>), - getBalance: async () => ({ - balances: [{ - available: 'USD:15' - }] - } as Partial<BalancesResponse>), - } as Partial<typeof wxApi> as any) - ); + const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + mountHook(() => + useComponentState("taller://pay", { + onUpdateNotification: nullFunction, + preparePay: async () => + ({ + amountRaw: "USD:10", + amountEffective: "USD:10", + status: PreparePayResultType.PaymentPossible, + } as Partial<PreparePayResult>), + getBalance: async () => + ({ + balances: [ + { + available: "USD:15", + }, + ], + } as Partial<BalancesResponse>), + } as Partial<typeof wxApi> as any), + ); { - const { status, hook } = getLastResultOrThrow() - expect(status).equals('loading') + const { status, hook } = getLastResultOrThrow(); + expect(status).equals("loading"); expect(hook).undefined; } await waitNextUpdate(); { - const r = getLastResultOrThrow() - if (r.status !== 'ready') expect.fail() - expect(r.balance).deep.equal(Amounts.parseOrThrow('USD:15')); - expect(r.amount).deep.equal(Amounts.parseOrThrow('USD:10')) - expect(r.totalFees).deep.equal(Amounts.parseOrThrow('USD:0')) + const r = getLastResultOrThrow(); + if (r.status !== "ready") expect.fail(); + expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:15")); + expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:10")); + expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:0")); expect(r.payHandler.onClick).not.undefined; } - await assertNoPendingUpdate() + await assertNoPendingUpdate(); }); it("should be able to pay (with fee)", async () => { - - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = mountHook(() => - useComponentState('taller://pay', { - onUpdateNotification: nullFunction, - preparePay: async () => ({ - amountRaw: 'USD:9', - amountEffective: 'USD:10', - status: PreparePayResultType.PaymentPossible, - } as Partial<PreparePayResult>), - getBalance: async () => ({ - balances: [{ - available: 'USD:15' - }] - } as Partial<BalancesResponse>), - } as Partial<typeof wxApi> as any) - ); + const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + mountHook(() => + useComponentState("taller://pay", { + onUpdateNotification: nullFunction, + preparePay: async () => + ({ + amountRaw: "USD:9", + amountEffective: "USD:10", + status: PreparePayResultType.PaymentPossible, + } as Partial<PreparePayResult>), + getBalance: async () => + ({ + balances: [ + { + available: "USD:15", + }, + ], + } as Partial<BalancesResponse>), + } as Partial<typeof wxApi> as any), + ); { - const { status, hook } = getLastResultOrThrow() - expect(status).equals('loading') + const { status, hook } = getLastResultOrThrow(); + expect(status).equals("loading"); expect(hook).undefined; } await waitNextUpdate(); { - const r = getLastResultOrThrow() - if (r.status !== 'ready') expect.fail() - expect(r.balance).deep.equal(Amounts.parseOrThrow('USD:15')); - expect(r.amount).deep.equal(Amounts.parseOrThrow('USD:9')) - expect(r.totalFees).deep.equal(Amounts.parseOrThrow('USD:1')) + const r = getLastResultOrThrow(); + if (r.status !== "ready") expect.fail(); + expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:15")); + expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:9")); + expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1")); expect(r.payHandler.onClick).not.undefined; } - await assertNoPendingUpdate() + await assertNoPendingUpdate(); }); it("should get confirmation done after pay successfully", async () => { - - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = mountHook(() => - useComponentState('taller://pay', { - onUpdateNotification: nullFunction, - preparePay: async () => ({ - amountRaw: 'USD:9', - amountEffective: 'USD:10', - status: PreparePayResultType.PaymentPossible, - } as Partial<PreparePayResult>), - getBalance: async () => ({ - balances: [{ - available: 'USD:15' - }] - } as Partial<BalancesResponse>), - confirmPay: async () => ({ - type: ConfirmPayResultType.Done, - contractTerms: {} - } as Partial<ConfirmPayResult>), - } as Partial<typeof wxApi> as any) - ); + const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + mountHook(() => + useComponentState("taller://pay", { + onUpdateNotification: nullFunction, + preparePay: async () => + ({ + amountRaw: "USD:9", + amountEffective: "USD:10", + status: PreparePayResultType.PaymentPossible, + } as Partial<PreparePayResult>), + getBalance: async () => + ({ + balances: [ + { + available: "USD:15", + }, + ], + } as Partial<BalancesResponse>), + confirmPay: async () => + ({ + type: ConfirmPayResultType.Done, + contractTerms: {}, + } as Partial<ConfirmPayResult>), + } as Partial<typeof wxApi> as any), + ); { - const { status, hook } = getLastResultOrThrow() - expect(status).equals('loading') + const { status, hook } = getLastResultOrThrow(); + expect(status).equals("loading"); expect(hook).undefined; } await waitNextUpdate(); { - const r = getLastResultOrThrow() - if (r.status !== 'ready') expect.fail() - expect(r.balance).deep.equal(Amounts.parseOrThrow('USD:15')); - expect(r.amount).deep.equal(Amounts.parseOrThrow('USD:9')) - expect(r.totalFees).deep.equal(Amounts.parseOrThrow('USD:1')) + const r = getLastResultOrThrow(); + if (r.status !== "ready") expect.fail(); + expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:15")); + expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:9")); + expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1")); if (r.payHandler.onClick === undefined) expect.fail(); - r.payHandler.onClick() + r.payHandler.onClick(); } await waitNextUpdate(); { - const r = getLastResultOrThrow() - if (r.status !== 'confirmed') expect.fail() - expect(r.balance).deep.equal(Amounts.parseOrThrow('USD:15')); - expect(r.amount).deep.equal(Amounts.parseOrThrow('USD:9')) - expect(r.totalFees).deep.equal(Amounts.parseOrThrow('USD:1')) + const r = getLastResultOrThrow(); + if (r.status !== "confirmed") expect.fail(); + expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:15")); + expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:9")); + expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1")); if (r.payResult.type !== ConfirmPayResultType.Done) expect.fail(); expect(r.payResult.contractTerms).not.undefined; expect(r.payHandler.onClick).undefined; } - await assertNoPendingUpdate() + await assertNoPendingUpdate(); }); it("should not stay in ready state after pay with error", async () => { - - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = mountHook(() => - useComponentState('taller://pay', { - onUpdateNotification: nullFunction, - preparePay: async () => ({ - amountRaw: 'USD:9', - amountEffective: 'USD:10', - status: PreparePayResultType.PaymentPossible, - } as Partial<PreparePayResult>), - getBalance: async () => ({ - balances: [{ - available: 'USD:15' - }] - } as Partial<BalancesResponse>), - confirmPay: async () => ({ - type: ConfirmPayResultType.Pending, - lastError: { code: 1 }, - } as Partial<ConfirmPayResult>), - } as Partial<typeof wxApi> as any) - ); + const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + mountHook(() => + useComponentState("taller://pay", { + onUpdateNotification: nullFunction, + preparePay: async () => + ({ + amountRaw: "USD:9", + amountEffective: "USD:10", + status: PreparePayResultType.PaymentPossible, + } as Partial<PreparePayResult>), + getBalance: async () => + ({ + balances: [ + { + available: "USD:15", + }, + ], + } as Partial<BalancesResponse>), + confirmPay: async () => + ({ + type: ConfirmPayResultType.Pending, + lastError: { code: 1 }, + } as Partial<ConfirmPayResult>), + } as Partial<typeof wxApi> as any), + ); { - const { status, hook } = getLastResultOrThrow() - expect(status).equals('loading') + const { status, hook } = getLastResultOrThrow(); + expect(status).equals("loading"); expect(hook).undefined; } await waitNextUpdate(); { - const r = getLastResultOrThrow() - if (r.status !== 'ready') expect.fail() - expect(r.balance).deep.equal(Amounts.parseOrThrow('USD:15')); - expect(r.amount).deep.equal(Amounts.parseOrThrow('USD:9')) - expect(r.totalFees).deep.equal(Amounts.parseOrThrow('USD:1')) + const r = getLastResultOrThrow(); + if (r.status !== "ready") expect.fail(); + expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:15")); + expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:9")); + expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1")); if (r.payHandler.onClick === undefined) expect.fail(); - r.payHandler.onClick() + r.payHandler.onClick(); } await waitNextUpdate(); { - const r = getLastResultOrThrow() - if (r.status !== 'ready') expect.fail() - expect(r.balance).deep.equal(Amounts.parseOrThrow('USD:15')); - expect(r.amount).deep.equal(Amounts.parseOrThrow('USD:9')) - expect(r.totalFees).deep.equal(Amounts.parseOrThrow('USD:1')) + const r = getLastResultOrThrow(); + if (r.status !== "ready") expect.fail(); + expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:15")); + expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:9")); + expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1")); expect(r.payHandler.onClick).undefined; if (r.payHandler.error === undefined) expect.fail(); //FIXME: error message here is bad - expect(r.payHandler.error.errorDetail.hint).eq("could not confirm payment") + expect(r.payHandler.error.errorDetail.hint).eq( + "could not confirm payment", + ); expect(r.payHandler.error.errorDetail.payResult).deep.equal({ type: ConfirmPayResultType.Pending, - lastError: { code: 1 } - }) + lastError: { code: 1 }, + }); } - await assertNoPendingUpdate() + await assertNoPendingUpdate(); }); it("should update balance if a coins is withdraw", async () => { @@ -351,40 +389,45 @@ describe("Pay CTA states", () => { let availableBalance = Amounts.parseOrThrow("USD:10"); function notifyCoinWithdrawn(newAmount: AmountJson): void { - availableBalance = Amounts.add(availableBalance, newAmount).amount - subscriptions.notifyEvent(NotificationType.CoinWithdrawn) + availableBalance = Amounts.add(availableBalance, newAmount).amount; + subscriptions.notifyEvent(NotificationType.CoinWithdrawn); } - const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = mountHook(() => - useComponentState('taller://pay', { - onUpdateNotification: subscriptions.saveSubscription, - preparePay: async () => ({ - amountRaw: 'USD:9', - amountEffective: 'USD:10', - status: PreparePayResultType.PaymentPossible, - } as Partial<PreparePayResult>), - getBalance: async () => ({ - balances: [{ - available: Amounts.stringify(availableBalance) - }] - } as Partial<BalancesResponse>), - } as Partial<typeof wxApi> as any) - ); + const { getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } = + mountHook(() => + useComponentState("taller://pay", { + onUpdateNotification: subscriptions.saveSubscription, + preparePay: async () => + ({ + amountRaw: "USD:9", + amountEffective: "USD:10", + status: PreparePayResultType.PaymentPossible, + } as Partial<PreparePayResult>), + getBalance: async () => + ({ + balances: [ + { + available: Amounts.stringify(availableBalance), + }, + ], + } as Partial<BalancesResponse>), + } as Partial<typeof wxApi> as any), + ); { - const { status, hook } = getLastResultOrThrow() - expect(status).equals('loading') + const { status, hook } = getLastResultOrThrow(); + expect(status).equals("loading"); expect(hook).undefined; } await waitNextUpdate(); { - const r = getLastResultOrThrow() - if (r.status !== 'ready') expect.fail() - expect(r.balance).deep.equal(Amounts.parseOrThrow('USD:10')); - expect(r.amount).deep.equal(Amounts.parseOrThrow('USD:9')) - expect(r.totalFees).deep.equal(Amounts.parseOrThrow('USD:1')) + const r = getLastResultOrThrow(); + if (r.status !== "ready") expect.fail(); + expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:10")); + expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:9")); + expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1")); expect(r.payHandler.onClick).not.undefined; notifyCoinWithdrawn(Amounts.parseOrThrow("USD:5")); @@ -393,16 +436,14 @@ describe("Pay CTA states", () => { await waitNextUpdate(); { - const r = getLastResultOrThrow() - if (r.status !== 'ready') expect.fail() - expect(r.balance).deep.equal(Amounts.parseOrThrow('USD:15')); - expect(r.amount).deep.equal(Amounts.parseOrThrow('USD:9')) - expect(r.totalFees).deep.equal(Amounts.parseOrThrow('USD:1')) + const r = getLastResultOrThrow(); + if (r.status !== "ready") expect.fail(); + expect(r.balance).deep.equal(Amounts.parseOrThrow("USD:15")); + expect(r.amount).deep.equal(Amounts.parseOrThrow("USD:9")); + expect(r.totalFees).deep.equal(Amounts.parseOrThrow("USD:1")); expect(r.payHandler.onClick).not.undefined; } - await assertNoPendingUpdate() + await assertNoPendingUpdate(); }); - - -});
\ No newline at end of file +}); |