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
8 changes: 4 additions & 4 deletions @simulcast/preact/tests/useBroadcast.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import { broadcast, type EventRegistry } from "@simulcast/core";
import { useBroadcast } from "@simulcast/preact";
import { assertStrictEquals } from "@std/assert";
import { type ComponentProps, render, type TargetedMouseEvent } from "preact";
import { useState } from "preact/hooks";
import { mockDOM } from "@test/mockDOM.ts";
import { timeout } from "@test/timeout.ts";
import { type ComponentProps, render, type TargetedMouseEvent } from "preact";
import { useState } from "preact/hooks";

const CountComponent = (properties: ComponentProps<"button">) => {
const [count, setCount] = useState(0);
Expand Down Expand Up @@ -78,9 +78,9 @@ Deno.test(
) as HTMLButtonElement;

addButton.click(); // Click button that will re-render once
await timeout();
await timeout(10);
addButton.click(); // Click button that will re-render twice
await timeout();
await timeout(10);
broadcastButton.click(); // Click broadcast button once
assertStrictEquals(state.calledTimes, 1); // State should be updated once
assertStrictEquals(addButton.textContent, "2"); // Even when it re-rendered twice
Expand Down
8 changes: 4 additions & 4 deletions @simulcast/react/tests/useBroadcast.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import { broadcast, type EventRegistry } from "@simulcast/core";
import { useBroadcast } from "@simulcast/react";
import { assertStrictEquals } from "@std/assert";
import { type ComponentProps, type MouseEvent, useState } from "react";
import { createRoot } from "react-dom/client";
import { mockDOM } from "@test/mockDOM.ts";
import { timeout } from "@test/timeout.ts";
import { type ComponentProps, type MouseEvent, useState } from "react";
import { createRoot } from "react-dom/client";

const CountComponent = (properties: ComponentProps<"button">) => {
const [count, setCount] = useState(0);
Expand Down Expand Up @@ -69,7 +69,7 @@ Deno.test(
),
);

await timeout(3);
await timeout(10);

const addButton = document.querySelector<HTMLButtonElement>(
"button.add",
Expand Down Expand Up @@ -131,7 +131,7 @@ Deno.test("Broadcast's on handler is removed when unmounted", async () => {

root.render(<App />);

await timeout(1);
await timeout(10);

const toggleButton = document.querySelector<HTMLButtonElement>(
"button.toggle",
Expand Down
10 changes: 5 additions & 5 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
"@types/react": "npm:@types/react@^19.2.7",
"@types/react-dom": "npm:@types/react-dom@^19.2.3",
"@vue/test-utils": "npm:@vue/test-utils@^2.4.6",
"linkedom": "npm:linkedom@^0.18.12",
"preact": "npm:preact@~10.27.2",
"preact-render-to-string": "npm:preact-render-to-string@~6.6.3",
"react": "npm:react@^19.2.0",
"react-dom": "npm:react-dom@^19.2.0",
"happy-dom": "npm:happy-dom@^20.0.11",
"preact": "npm:preact@~10.28.0",
"preact-render-to-string": "npm:preact-render-to-string@~6.6.4",
"react": "npm:react@^19.2.3",
"react-dom": "npm:react-dom@^19.2.3",
"vue": "npm:vue@^3.5.25"
},
"lint": {
Expand Down
11 changes: 7 additions & 4 deletions test/mockDOM.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseHTML } from "linkedom/worker";
// deno-coverage-ignore-file
import { Window } from "happy-dom";

Error.stackTraceLimit = Infinity;

Expand All @@ -8,7 +9,7 @@ const properties = [
"SVGElement",
"document",
"window",
] as const satisfies ReadonlyArray<keyof ReturnType<typeof parseHTML>>;
] as const satisfies ReadonlyArray<keyof InstanceType<typeof Window>>;

/**
* Sets a global mocked DOM or resets it if it's already set.
Expand All @@ -30,10 +31,12 @@ export const mockDOM = (
template?: string;
} = {},
): void => {
const parsed = parseHTML(template);
const window = new Window();

window.document.body.innerHTML = template;

properties.forEach((property) =>
Object.assign(globalThis, { [property]: parsed[property] })
Object.assign(globalThis, { [property]: window[property] })
);

if (fakeTimers) {
Expand Down
1 change: 1 addition & 0 deletions test/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// deno-coverage-ignore-file
import { mockDOM } from "./mockDOM.ts";

mockDOM();