diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/hooks')
3 files changed, 65 insertions, 28 deletions
diff --git a/packages/taler-wallet-webextension/src/hooks/useBackupStatus.ts b/packages/taler-wallet-webextension/src/hooks/useBackupStatus.ts index 8a8fd6f2f..df1e82676 100644 --- a/packages/taler-wallet-webextension/src/hooks/useBackupStatus.ts +++ b/packages/taler-wallet-webextension/src/hooks/useBackupStatus.ts @@ -43,8 +43,8 @@ function getStatusPaidOrder(a: ProviderPaymentPaid, b: ProviderPaymentPaid) { return a.paidUntil.t_ms === "never" ? -1 : b.paidUntil.t_ms === "never" - ? 1 - : a.paidUntil.t_ms - b.paidUntil.t_ms; + ? 1 + : a.paidUntil.t_ms - b.paidUntil.t_ms; } export function useBackupStatus(): BackupStatus | undefined { diff --git a/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.test.ts b/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.test.ts new file mode 100644 index 000000000..99934ef0b --- /dev/null +++ b/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.test.ts @@ -0,0 +1,60 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + 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/> + */ +import { useTalerActionURL } from "./useTalerActionURL" +import { justBrowser_it, mountBrowser } from "../test-utils"; +import { IoCProviderForTesting } from "../context/iocContext"; +import { h, VNode } from "preact"; +import { act } from "preact/test-utils"; + +describe('useTalerActionURL hook', () => { + + // eslint-disable-next-line jest/expect-expect + justBrowser_it('should be set url to undefined when dismiss', async () => { + + const ctx = ({ children }: { children: any }): VNode => { + return h(IoCProviderForTesting, { + value: { + findTalerUriInActiveTab: async () => "asd", + }, children + }) + } + + const { result, waitNextUpdate } = mountBrowser(useTalerActionURL, ctx) + + { + const [url] = result.current! + if (url !== undefined) throw Error('invalid') + } + + await waitNextUpdate() + + { + const [url] = result.current! + if (url !== "asd") throw Error(`invalid: ${url}`) + } + + await act(() => { + const [, setDismissed] = result.current! + setDismissed(true) + }) + + { + const [url] = result.current! + if (url !== undefined) throw Error('invalid') + } + + }) +})
\ No newline at end of file diff --git a/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts b/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts index 96a278401..53e1e912d 100644 --- a/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts +++ b/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts @@ -14,8 +14,8 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { classifyTalerUri, TalerUriType } from "@gnu-taler/taler-util"; import { useEffect, useState } from "preact/hooks"; +import { useIocContext } from "../context/iocContext"; export function useTalerActionURL(): [ string | undefined, @@ -25,6 +25,8 @@ export function useTalerActionURL(): [ undefined, ); const [dismissed, setDismissed] = useState(false); + const { findTalerUriInActiveTab } = useIocContext() + useEffect(() => { async function check(): Promise<void> { const talerUri = await findTalerUriInActiveTab(); @@ -35,28 +37,3 @@ export function useTalerActionURL(): [ const url = dismissed ? undefined : talerActionUrl; return [url, setDismissed]; } - -async function findTalerUriInActiveTab(): Promise<string | undefined> { - return new Promise((resolve, reject) => { - chrome.tabs.executeScript( - { - code: ` - (() => { - let x = document.querySelector("a[href^='taler://'") || document.querySelector("a[href^='taler+http://'"); - return x ? x.href.toString() : null; - })(); - `, - allFrames: false, - }, - (result) => { - if (chrome.runtime.lastError) { - console.error(chrome.runtime.lastError); - resolve(undefined); - return; - } - console.log("got result", result); - resolve(result[0]); - }, - ); - }); -} |