Skip to content

Commit 54f6443

Browse files
committed
Add tests
1 parent 69e6c7f commit 54f6443

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

packages/react/runtime/test/browser/suspense.test.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
33

44
import { createElement, lazy, useLayoutEffect, Suspense } from "react";
5-
import { signal } from "@preact/signals-core";
5+
import { signal, Signal } from "@preact/signals-core";
66
import {
77
useComputed,
88
useSignalEffect,
@@ -19,6 +19,7 @@ import {
1919
describe("Suspense", () => {
2020
let scratch: HTMLDivElement;
2121
let root: Root;
22+
let originalSubscribe: typeof Signal.prototype._subscribe;
2223

2324
async function render(element: Parameters<Root["render"]>[0]) {
2425
await act(() => root.render(element));
@@ -29,6 +30,7 @@ describe("Suspense", () => {
2930
document.body.appendChild(scratch);
3031
root = await createRoot(scratch);
3132
getConsoleErrorSpy().resetHistory();
33+
originalSubscribe = Signal.prototype._subscribe;
3234
});
3335

3436
afterEach(async () => {
@@ -40,6 +42,7 @@ describe("Suspense", () => {
4042
//
4143
// checkConsoleErrorLogs();
4244
checkHangingAct();
45+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
4346
});
4447

4548
it("should handle suspending and unsuspending", async () => {
@@ -122,15 +125,19 @@ describe("Suspense", () => {
122125
signal1.value++;
123126
signal2.value++;
124127
});
128+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
129+
125130
await act(async () => {
126131
signal1.value--;
127132
signal2.value--;
128133
});
134+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
129135

130136
await act(async () => {
131137
resolveMiddleProm();
132138
await middleProm;
133139
});
140+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
134141

135142
expect(scratch.innerHTML).to.be.oneOf([
136143
// react 17+
@@ -143,6 +150,7 @@ describe("Suspense", () => {
143150
unsuspend();
144151
await prom;
145152
});
153+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
146154

147155
// react 16 uses `style.setProperty()` to clear display value, which leaves an empty style attr in innerHTML.
148156
// react 17 does not do this, so we normalize 16 behavior to 17 here.
@@ -157,6 +165,8 @@ describe("Suspense", () => {
157165
signal1.value++;
158166
signal2.value++;
159167
});
168+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
169+
160170
expect(scratch.innerHTML).to.equal(
161171
`<p>1</p><div data-foo="1"><span>lazy</span></div>`
162172
);
@@ -236,6 +246,7 @@ describe("Suspense", () => {
236246

237247
// Initial render - should trigger watched callback
238248
await render(<Parent />);
249+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
239250
expect(scratch.innerHTML).to.contain("Loading first...");
240251
expect(scratch.innerHTML).to.contain("Loading second...");
241252
expect(scratch.innerHTML).to.contain("Regular");
@@ -249,6 +260,7 @@ describe("Suspense", () => {
249260
resolveFirstProm();
250261
await firstProm;
251262
});
263+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
252264

253265
expect(scratch.innerHTML).to.contain("First");
254266
expect(scratch.innerHTML).to.contain("Loading second...");
@@ -258,6 +270,7 @@ describe("Suspense", () => {
258270
resolveSecondProm();
259271
await secondProm;
260272
});
273+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
261274

262275
expect(scratch.innerHTML).to.contain("First");
263276
expect(scratch.innerHTML).to.contain("Second");
@@ -267,6 +280,7 @@ describe("Suspense", () => {
267280
await act(async () => {
268281
trackedSignal.value = 42;
269282
});
283+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
270284

271285
expect(scratch.innerHTML).to.contain('data-parent="42"');
272286
expect(scratch.innerHTML).to.contain('data-regular="42"');
@@ -281,7 +295,7 @@ describe("Suspense", () => {
281295
expect(scratch.innerHTML).to.equal("");
282296

283297
// Wait for cleanup to complete
284-
await new Promise(resolve => setTimeout(resolve, 10));
298+
await new Promise(resolve => setTimeout(resolve, 100));
285299

286300
// After unmount, the signal should be unwatched
287301
expect(unwatchedCallCount).to.be.greaterThan(0);
@@ -385,6 +399,7 @@ describe("Suspense", () => {
385399

386400
// Initial render - should trigger watched callback
387401
await render(<Parent />);
402+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
388403
expect(scratch.innerHTML).to.contain("Loading first...");
389404
expect(scratch.innerHTML).to.contain("Loading second...");
390405
expect(scratch.innerHTML).to.contain("Regular");
@@ -399,6 +414,7 @@ describe("Suspense", () => {
399414
await firstProm;
400415
});
401416

417+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
402418
expect(scratch.innerHTML).to.contain("First");
403419
expect(scratch.innerHTML).to.contain("Loading second...");
404420

@@ -408,6 +424,7 @@ describe("Suspense", () => {
408424
await secondProm;
409425
});
410426

427+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
411428
expect(scratch.innerHTML).to.contain("First");
412429
expect(scratch.innerHTML).to.contain("Second");
413430
expect(scratch.innerHTML).to.contain("Regular");
@@ -417,6 +434,7 @@ describe("Suspense", () => {
417434
trackedSignal.value = 42;
418435
});
419436

437+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
420438
expect(scratch.innerHTML).to.contain('data-parent="42"');
421439
expect(scratch.innerHTML).to.contain('data-regular="42"');
422440
expect(scratch.innerHTML).to.contain('data-first="42"');
@@ -427,10 +445,11 @@ describe("Suspense", () => {
427445
root.unmount();
428446
});
429447

448+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
430449
expect(scratch.innerHTML).to.equal("");
431450

432451
// Wait for cleanup to complete
433-
await new Promise(resolve => setTimeout(resolve, 10));
452+
await new Promise(resolve => setTimeout(resolve, 100));
434453

435454
// After unmount, the signal should be unwatched
436455
expect(unwatchedCallCount).to.be.greaterThan(0);
@@ -478,6 +497,7 @@ describe("Suspense", () => {
478497

479498
// Initial render - should trigger watched callback
480499
await render(<Parent />);
500+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
481501
expect(scratch.innerHTML).to.contain("Regular");
482502

483503
// Signal should be watched by now
@@ -488,6 +508,7 @@ describe("Suspense", () => {
488508
await act(async () => {
489509
trackedSignal.value = 10;
490510
});
511+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
491512

492513
expect(scratch.innerHTML).to.contain('data-parent="10"');
493514
expect(scratch.innerHTML).to.contain('data-regular="10"');
@@ -497,6 +518,7 @@ describe("Suspense", () => {
497518
trackedSignal.value = 20;
498519
});
499520

521+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
500522
expect(scratch.innerHTML).to.contain('data-parent="20"');
501523
expect(scratch.innerHTML).to.contain('data-regular="20"');
502524

@@ -508,10 +530,11 @@ describe("Suspense", () => {
508530
root.unmount();
509531
});
510532

533+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
511534
expect(scratch.innerHTML).to.equal("");
512535

513536
// Wait for cleanup to complete
514-
await new Promise(resolve => setTimeout(resolve, 10));
537+
await new Promise(resolve => setTimeout(resolve, 100));
515538

516539
// After unmount, the signal should be unwatched
517540
expect(unwatchedCallCount).to.be.greaterThan(0);

packages/react/runtime/test/browser/useSignals.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const MANAGED_HOOK = 2;
1515
describe("useSignals", () => {
1616
let scratch: HTMLDivElement;
1717
let root: Root;
18+
let originalSubscribe: typeof Signal.prototype._subscribe;
1819

1920
async function render(element: Parameters<Root["render"]>[0]) {
2021
await act(() => root.render(element));
@@ -53,6 +54,7 @@ describe("useSignals", () => {
5354
document.body.appendChild(scratch);
5455
root = await createRoot(scratch);
5556
getConsoleErrorSpy().resetHistory();
57+
originalSubscribe = Signal.prototype._subscribe;
5658
});
5759

5860
afterEach(async () => {
@@ -64,6 +66,7 @@ describe("useSignals", () => {
6466
//
6567
// checkConsoleErrorLogs();
6668
checkHangingAct();
69+
expect(Signal.prototype._subscribe).to.equal(originalSubscribe);
6770
});
6871

6972
it("should rerender components when signals they use change", async () => {

0 commit comments

Comments
 (0)