From ccb50c636054819f5af8778cc3ebe5258b1c2e87 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 11 Apr 2022 11:36:32 -0300 Subject: new test api to test hooks rendering iteration, testing state of withdraw page --- .../taler-wallet-webextension/src/test-utils.ts | 43 +++++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'packages/taler-wallet-webextension/src/test-utils.ts') diff --git a/packages/taler-wallet-webextension/src/test-utils.ts b/packages/taler-wallet-webextension/src/test-utils.ts index 39ffbda08..f10e49ac4 100644 --- a/packages/taler-wallet-webextension/src/test-utils.ts +++ b/packages/taler-wallet-webextension/src/test-utils.ts @@ -64,23 +64,27 @@ export function renderNodeOrBrowser(Component: any, args: any): void { interface Mounted { unmount: () => void; - result: { current: T | null }; + getLastResult: () => T | null; + getLastResultOrThrow: () => T; + assertNoPendingUpdate: () => void; waitNextUpdate: (s?: string) => Promise; } const isNode = typeof window === "undefined" export function mountHook(callback: () => T, Context?: ({ children }: { children: any }) => VNode): Mounted { - const result: { current: T | null } = { - current: null - } + // const result: { current: T | null } = { + // current: null + // } + let lastResult: T | null = null; + const listener: Array<() => void> = [] // component that's going to hold the hook function Component(): VNode { const hookResult = callback() // save the hook result - result.current = hookResult + lastResult = hookResult // notify to everyone waiting for an update and clean the queue listener.splice(0, listener.length).forEach(cb => cb()) return create(Fragment, {}) @@ -119,7 +123,34 @@ export function mountHook(callback: () => T, Context?: ({ children }: { child } } + function getLastResult(): T | null { + const copy = lastResult + lastResult = null + return copy; + } + + function getLastResultOrThrow(): T { + const r = getLastResult() + if (!r) throw Error('there was no last result') + return r; + } + + async function assertNoPendingUpdate(): Promise { + await new Promise((res, rej) => { + const tid = setTimeout(() => { + res(undefined) + }, 10) + + listener.push(() => { + clearTimeout(tid) + rej(Error(`Expecting no pending result but the hook get updated. Check the dependencies of the hooks.`)) + }) + }) + + const r = getLastResult() + if (r) throw Error('There are still pending results. This may happen because the hook did a new update but the test didn\'t get the result using getLastResult'); + } return { - unmount, result, waitNextUpdate + unmount, getLastResult, getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate } } -- cgit v1.2.3