summaryrefslogtreecommitdiff
path: root/packages/anastasis-webui/src/utils
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-06-11 19:10:26 -0300
committerSebastian <sebasjm@gmail.com>2022-06-11 19:10:26 -0300
commit6d06b52605005f4d25381fc73383c3c9e48f20f8 (patch)
treed1e01d71c538602a92848595f92d24bf214c264f /packages/anastasis-webui/src/utils
parent716da3246b7d544fc81265d1942ae64067ecd8b7 (diff)
downloadwallet-core-6d06b52605005f4d25381fc73383c3c9e48f20f8.tar.gz
wallet-core-6d06b52605005f4d25381fc73383c3c9e48f20f8.tar.bz2
wallet-core-6d06b52605005f4d25381fc73383c3c9e48f20f8.zip
add testing to web components
Diffstat (limited to 'packages/anastasis-webui/src/utils')
-rw-r--r--packages/anastasis-webui/src/utils/index.tsx36
1 files changed, 35 insertions, 1 deletions
diff --git a/packages/anastasis-webui/src/utils/index.tsx b/packages/anastasis-webui/src/utils/index.tsx
index 204c48d18..63bed9392 100644
--- a/packages/anastasis-webui/src/utils/index.tsx
+++ b/packages/anastasis-webui/src/utils/index.tsx
@@ -21,13 +21,26 @@ import {
ReducerState,
ReducerStateRecovery,
} from "@gnu-taler/anastasis-core";
-import { FunctionalComponent, h, VNode } from "preact";
+import { ComponentChildren, FunctionalComponent, h, VNode } from "preact";
import { AnastasisProvider } from "../context/anastasis.js";
const noop = async (): Promise<void> => {
return;
};
+export function createExampleWithoutAnastasis<Props>(
+ Component: FunctionalComponent<Props>,
+ props: Partial<Props> | (() => Partial<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;
+ const Render = (args: any): VNode => h(Component, args);
+ Render.args = evaluatedProps;
+ return Render;
+}
+
export function createExample<Props>(
Component: FunctionalComponent<Props>,
currentReducerState?: ReducerState,
@@ -293,3 +306,24 @@ export const reducerStatesExample = {
backup_state: BackupStates.TruthsPaying,
} as ReducerState,
};
+
+export type StateFunc<S> = (p: S) => VNode;
+
+export type StateViewMap<StateType extends { status: string }> = {
+ [S in StateType as S["status"]]: StateFunc<S>;
+};
+
+export function compose<SType extends { status: string }, PType>(
+ name: string,
+ hook: (p: PType) => SType,
+ vs: StateViewMap<SType>,
+): (p: PType) => VNode {
+ const Component = (p: PType): VNode => {
+ const state = hook(p);
+ const s = state.status as unknown as SType["status"];
+ const c = vs[s] as unknown as StateFunc<SType>;
+ return c(state);
+ };
+ Component.name = `${name}`;
+ return Component;
+}