diff options
author | Sebastian <sebasjm@gmail.com> | 2022-03-23 16:20:39 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-03-23 16:20:39 -0300 |
commit | e21c1b31928cd6bfe90150ea2de19799b6359c40 (patch) | |
tree | 10558ee2b86d5ac01de0995bfe12bdb7da44edde /packages/taler-wallet-webextension/src/test-utils.ts | |
parent | 136c39ba9fb86080beaa27cd9e0c4d5086a2d691 (diff) | |
download | wallet-core-e21c1b31928cd6bfe90150ea2de19799b6359c40.tar.xz |
splitted rollup config for testing and first component state unit test
Diffstat (limited to 'packages/taler-wallet-webextension/src/test-utils.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/test-utils.ts | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/packages/taler-wallet-webextension/src/test-utils.ts b/packages/taler-wallet-webextension/src/test-utils.ts index 8c721a9d5..30b37ab8c 100644 --- a/packages/taler-wallet-webextension/src/test-utils.ts +++ b/packages/taler-wallet-webextension/src/test-utils.ts @@ -62,7 +62,9 @@ interface Mounted<T> { waitNextUpdate: () => Promise<void>; } -export function mountBrowser<T>(callback: () => T, Context?: ({ children }: { children: any }) => VNode): Mounted<T> { +const isNode = typeof window === "undefined" + +export function mountHook<T>(callback: () => T, Context?: ({ children }: { children: any }) => VNode): Mounted<T> { const result: { current: T | null } = { current: null } @@ -81,23 +83,6 @@ export function mountBrowser<T>(callback: () => T, Context?: ({ children }: { ch // create the vdom with context if required const vdom = !Context ? create(Component, {}) : create(Context, { children: [create(Component, {})] },); - // in non-browser environment (server side rendering) just serialize to - // string and exit - if (typeof window === "undefined") { - renderToString(vdom); - return { unmount: () => null, result } as any - } - - // do the render into the DOM - const div = document.createElement("div"); - document.body.appendChild(div); - renderIntoDom(vdom, div); - - // clean up callback - function unmount(): any { - document.body.removeChild(div); - } - // waiter callback async function waitNextUpdate(): Promise<void> { await new Promise((res, rej) => { @@ -112,11 +97,22 @@ export function mountBrowser<T>(callback: () => T, Context?: ({ children }: { ch }) } + const customElement = {} as Element + const parentElement = isNode ? customElement : document.createElement("div"); + if (!isNode) { + document.body.appendChild(parentElement); + } + + renderIntoDom(vdom, parentElement); + + // clean up callback + function unmount() { + if (!isNode) { + document.body.removeChild(parentElement); + } + } + return { unmount, result, waitNextUpdate } } - -const nullTestFunction = {} as TestFunction -export const justBrowser_it: PendingTestFunction | TestFunction = - typeof it === 'undefined' ? nullTestFunction : (typeof window === 'undefined' ? it.skip : it)
\ No newline at end of file |