Skip to content

Commit a6a3ec5

Browse files
committed
chore: Made the tests less brittle
1 parent 74ea0a9 commit a6a3ec5

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

tests/basic.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
HandlerResolverComponentProps,
1919
} from "./utils";
2020
import awaitNextFrame from "./utils/awaitNextFrame";
21+
import delay from "./utils/delay";
2122

2223
describe("Vanilla tests", () => {
2324
it("should render with undefined sizes at first", async () => {
@@ -95,10 +96,8 @@ describe("Vanilla tests", () => {
9596
waitForFirstMeasurement: true,
9697
});
9798

98-
await Promise.all([
99-
handler1.setAndAssertSize({ width: 100, height: 200 }),
100-
handler2.setAndAssertSize({ width: 300, height: 400 }),
101-
]);
99+
await handler1.setAndAssertSize({ width: 100, height: 200 });
100+
await handler2.setAndAssertSize({ width: 300, height: 400 });
102101

103102
// By the first measurement the component would've rendered three times:
104103
// - First natural render with "undefined" values

tests/ie/polyfilled.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import useResizeObserver from "../../polyfilled";
1010
import delay from "../utils/delay";
1111
// @ts-ignore
1212
import ROP from "resize-observer-polyfill";
13+
import awaitNextFrame from "../utils/awaitNextFrame";
1314

1415
/**
1516
* This test ensures that the shipped polyfilled version actually works.
@@ -51,7 +52,7 @@ describe("Polyfilled lib testing", () => {
5152

5253
const { assertSize } = await render(Test);
5354

54-
await delay(50);
55+
await awaitNextFrame();
5556
assertSize({ width: 50, height: 40 });
5657
});
5758
});

tests/utils/awaitNextFrame.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// @see https://stackoverflow.com/a/21825207/982092
22
// @ts-ignore
33
const isIE11 = !!window.MSInputMethodContext && !!document.documentMode;
4+
// TODO instead of hardcoded values, we should wait with a timeout for sizes to
5+
// be reported whenever they're available. (rAF in a loop maybe)
46
export default function awaitNextFrame() {
57
return new Promise((resolve) =>
68
// Seems like that on IE with the RO polyfill we need to slow things down a bit
7-
setTimeout(resolve, 1000 / (isIE11 ? 10 : 60))
9+
// Also, 1000 / 60 did not seem to not be enough of a wait sometimes on modern browsers either.
10+
setTimeout(resolve, 1000 / (isIE11 ? 5 : 30))
811
);
912
}

tests/utils/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useRef, RefObject, FunctionComponent } from "react";
22
import ReactDOM from "react-dom";
33
import useResizeObserver from "../..";
44
import useMergedCallbackRef from "./useMergedCallbackRef";
5-
import delay from "./delay";
5+
import awaitNextFrame from "./awaitNextFrame";
66

77
export type Size = {
88
width: number;
@@ -80,7 +80,7 @@ export function createComponentHandler({
8080
};
8181
handler.setAndAssertSize = async (size) => {
8282
handler.setSize(size);
83-
await delay(50);
83+
await awaitNextFrame();
8484
handler.assertSize(size);
8585
};
8686
}
@@ -198,7 +198,7 @@ export function render(
198198
handler: T | T[]
199199
): Promise<void> {
200200
if (waitForFirstMeasurement) {
201-
await delay(50);
201+
await awaitNextFrame();
202202
}
203203

204204
resolve(handler);

0 commit comments

Comments
 (0)