summaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/test-utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/taler-wallet-webextension/src/test-utils.ts')
-rw-r--r--packages/taler-wallet-webextension/src/test-utils.ts116
1 files changed, 72 insertions, 44 deletions
diff --git a/packages/taler-wallet-webextension/src/test-utils.ts b/packages/taler-wallet-webextension/src/test-utils.ts
index 9e219daa6..d9b97e4ab 100644
--- a/packages/taler-wallet-webextension/src/test-utils.ts
+++ b/packages/taler-wallet-webextension/src/test-utils.ts
@@ -14,15 +14,23 @@
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
-import { ComponentChildren, Fragment, FunctionalComponent, h as create, options, render as renderIntoDom, VNode } from "preact";
+import {
+ ComponentChildren,
+ Fragment,
+ FunctionalComponent,
+ h as create,
+ options,
+ render as renderIntoDom,
+ VNode,
+} from "preact";
import { render as renderToString } from "preact-render-to-string";
// When doing tests we want the requestAnimationFrame to be as fast as possible.
// without this option the RAF will timeout after 100ms making the tests slower
options.requestAnimationFrame = (fn: () => void) => {
// console.log("RAF called")
- return fn()
-}
+ return fn();
+};
export function createExample<Props>(
Component: FunctionalComponent<Props>,
@@ -30,8 +38,8 @@ export function createExample<Props>(
): ComponentChildren {
//FIXME: props are evaluated on build time
// in some cases we want to evaluated the props on render time so we can get some relative timestamp
- // check how we can build evaluatedProps in render time
- const evaluatedProps = typeof props === "function" ? props() : props
+ // check how we can build evaluatedProps in render time
+ const evaluatedProps = typeof props === "function" ? props() : props;
const Render = (args: any): VNode => create(Component, args);
Render.args = evaluatedProps;
return Render;
@@ -43,14 +51,22 @@ export function createExampleWithCustomContext<Props, ContextProps>(
ContextProvider: FunctionalComponent<ContextProps>,
contextProps: Partial<ContextProps>,
): ComponentChildren {
- const evaluatedProps = typeof props === "function" ? props() : props
+ const evaluatedProps = typeof props === "function" ? props() : props;
const Render = (args: any): VNode => create(Component, args);
- const WithContext = (args: any): VNode => create(ContextProvider, { ...contextProps, children: [Render(args)] } as any);
- WithContext.args = evaluatedProps
- return WithContext
+ const WithContext = (args: any): VNode =>
+ create(ContextProvider, {
+ ...contextProps,
+ children: [Render(args)],
+ } as any);
+ WithContext.args = evaluatedProps;
+ return WithContext;
}
-export function NullLink({ children }: { children?: ComponentChildren }): VNode {
+export function NullLink({
+ children,
+}: {
+ children?: ComponentChildren;
+}): VNode {
return create("a", { children, href: "javascript:void(0);" });
}
@@ -74,53 +90,59 @@ interface Mounted<T> {
waitNextUpdate: (s?: string) => Promise<void>;
}
-const isNode = typeof window === "undefined"
+const isNode = typeof window === "undefined";
-export function mountHook<T>(callback: () => T, Context?: ({ children }: { children: any }) => VNode): Mounted<T> {
+export function mountHook<T>(
+ callback: () => T,
+ Context?: ({ children }: { children: any }) => VNode,
+): Mounted<T> {
// const result: { current: T | null } = {
// current: null
// }
let lastResult: T | Error | null = null;
- const listener: Array<() => void> = []
+ const listener: Array<() => void> = [];
// component that's going to hold the hook
function Component(): VNode {
-
try {
- lastResult = callback()
+ lastResult = callback();
} catch (e) {
if (e instanceof Error) {
- lastResult = e
+ lastResult = e;
} else {
- lastResult = new Error(`mounting the hook throw an exception: ${e}`)
+ lastResult = new Error(`mounting the hook throw an exception: ${e}`);
}
}
// notify to everyone waiting for an update and clean the queue
- listener.splice(0, listener.length).forEach(cb => cb())
- return create(Fragment, {})
+ listener.splice(0, listener.length).forEach((cb) => cb());
+ return create(Fragment, {});
}
// create the vdom with context if required
- const vdom = !Context ? create(Component, {}) : create(Context, { children: [create(Component, {})] },);
+ const vdom = !Context
+ ? create(Component, {})
+ : create(Context, { children: [create(Component, {})] });
// waiter callback
async function waitNextUpdate(_label = ""): Promise<void> {
- if (_label) _label = `. label: "${_label}"`
+ if (_label) _label = `. label: "${_label}"`;
await new Promise((res, rej) => {
const tid = setTimeout(() => {
- rej(Error(`waiting for an update but the hook didn't make one${_label}`))
- }, 100)
+ rej(
+ Error(`waiting for an update but the hook didn't make one${_label}`),
+ );
+ }, 100);
listener.push(() => {
- clearTimeout(tid)
- res(undefined)
- })
- })
+ clearTimeout(tid);
+ res(undefined);
+ });
+ });
}
- const customElement = {} as Element
+ const customElement = {} as Element;
const parentElement = isNode ? customElement : document.createElement("div");
if (!isNode) {
document.body.appendChild(parentElement);
@@ -136,38 +158,44 @@ export function mountHook<T>(callback: () => T, Context?: ({ children }: { child
}
function getLastResult(): T | Error | null {
- const copy = lastResult
- lastResult = null
+ const copy = lastResult;
+ lastResult = null;
return copy;
}
function getLastResultOrThrow(): T {
- const r = getLastResult()
+ const r = getLastResult();
if (r instanceof Error) throw r;
- if (!r) throw Error('there was no last result')
+ if (!r) throw Error("there was no last result");
return r;
}
async function assertNoPendingUpdate(): Promise<void> {
await new Promise((res, rej) => {
const tid = setTimeout(() => {
- res(undefined)
- }, 10)
+ res(undefined);
+ }, 10);
listener.push(() => {
- clearTimeout(tid)
- rej(Error(`Expecting no pending result but the hook got updated.
+ clearTimeout(tid);
+ rej(
+ Error(`Expecting no pending result but the hook got updated.
If the update was not intended you need to check the hook dependencies
(or dependencies of the internal state) but otherwise make
- sure to consume the result before ending the test.`))
- })
- })
-
- const r = getLastResult()
- if (r) throw Error(`There are still pending results.
+ sure to consume the result before ending the test.`),
+ );
+ });
+ });
+
+ 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 consume the result using getLastResult`);
}
return {
- unmount, getLastResultOrThrow, waitNextUpdate, assertNoPendingUpdate
- }
+ unmount,
+ getLastResultOrThrow,
+ waitNextUpdate,
+ assertNoPendingUpdate,
+ };
}