Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
369 changes: 223 additions & 146 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@
".": "./dist/index.js"
},
"dependencies": {
"@open-wc/testing": "^4.0.0",
"@pionjs/pion": "^2.0.0",
"lit-html": "^3.0.0"
},
"devDependencies": {
"@commitlint/cli": "^20.0.0",
"@commitlint/config-conventional": "^20.0.0",
"@neovici/cfg": "^2.0.0",
"@esm-bundle/chai": "^4.3.4-fix.0",
"@neovici/cfg": "^2.8.0",
"@semantic-release/changelog": "^6.0.0",
"@semantic-release/git": "^10.0.0",
"@types/mocha": "^10.0.6",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './render-hook';
export * from './wait-until';
2 changes: 1 addition & 1 deletion src/render-hook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mkResult, type RenderResult } from './result';
import { mkRenderer } from './renderer';
import { waitUntil } from '@open-wc/testing';
import { waitUntil } from './wait-until';
import type { RenderHookOptions } from './types';

const tillNextUpdate =
Expand Down
11 changes: 7 additions & 4 deletions src/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { RendererProps, Wrapper } from './types';
import { component } from '@pionjs/pion';
import { unsafeStatic, html } from 'lit-html/static.js';
import { litFixtureSync } from '@open-wc/testing';
import { TemplateResult } from 'lit-html';
import { render as litRender, TemplateResult } from 'lit-html';

interface HarnessProps<TProps> {
hookProps?: TProps;
Expand All @@ -29,15 +28,19 @@ export function mkRenderer<TProps, TResult>(
}
};
return (props?: TProps) => {
const root = litFixtureSync(
const container = document.createElement('div');
document.body.appendChild(container);
litRender(
wrapper(
html`<${unsafeStatic(tagName)}
.render=${render}
.hookProps=${props}
></${unsafeStatic(tagName)}>`,
props
)
),
container
);
const root = container.firstElementChild as HTMLElement;
const el = (
root.matches(tagName) ? root : root.querySelector(tagName)
) as HTMLElement & HarnessProps<TProps>;
Expand Down
48 changes: 48 additions & 0 deletions src/wait-until.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Waits until the given predicate returns a truthy value.
* Calls and awaits the predicate function at the given interval time.
* Can be used to poll until a certain condition is true.
*
* Cloned from @open-wc/testing-helpers to remove the dependency.
*
* @param predicate - predicate function which is called each poll interval.
* The predicate is awaited, so it can return a promise.
* @param message - an optional message to display when the condition timed out
* @param options - timeout and polling interval
*/
export function waitUntil(
predicate: () => unknown | Promise<unknown>,
message?: string,
options: { interval?: number; timeout?: number } = {},
): Promise<void> {
const { interval = 50, timeout = 1000 } = options;
const { stack } = new Error();

return new Promise((resolve, reject) => {
let timeoutId: ReturnType<typeof setTimeout>;

setTimeout(() => {
clearTimeout(timeoutId);
const error = new Error(
message
? `Timeout: ${message}`
: `waitUntil timed out after ${timeout}ms`,
);
error.stack = stack;
reject(error);
}, timeout);

async function nextInterval() {
try {
if (await predicate()) {
resolve();
} else {
timeoutId = setTimeout(nextInterval, interval);
}
} catch (error) {
reject(error);
}
}
nextInterval();
});
}
2 changes: 1 addition & 1 deletion test/render-hook.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderHook } from '../src';
import { expect } from '@open-wc/testing';
import { expect } from '@esm-bundle/chai';
import {
useState,
useCallback,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"module": "esnext",
"moduleResolution": "bundler",
"strict": true,
"target": "esnext"
"target": "esnext",
"skipLibCheck": true
}
}