Skip to content

Commit db846ae

Browse files
committed
test(web): align plugin render lifecycle and available panel counters
1 parent 2cc3d41 commit db846ae

4 files changed

Lines changed: 298 additions & 229 deletions

File tree

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
import { act, render as renderScreen, type Screen } from "../../test/render";
12
import { EnvironmentId, type PluginNotificationFrame } from "@t3tools/contracts";
2-
import { act } from "react";
3-
import { createRoot, type Root } from "react-dom/client";
4-
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5-
3+
import { expect, it, vi } from "vite-plus/test";
64
const testState = vi.hoisted(() => ({
75
environmentIds: [] as ReadonlyArray<string>,
86
frames: new Map<string, PluginNotificationFrame | null>(),
97
open: new Set<string>(),
108
added: [] as Array<string>,
119
nextToast: 0,
1210
}));
13-
14-
vi.mock("@tanstack/react-router", () => ({ useNavigate: () => () => undefined }));
11+
vi.mock("@tanstack/react-router", async () => {
12+
const router =
13+
await vi.importActual<typeof import("@tanstack/react-router")>("@tanstack/react-router");
14+
return {
15+
createMemoryHistory: router.createMemoryHistory,
16+
createRootRoute: router.createRootRoute,
17+
createRouter: router.createRouter,
18+
RouterProvider: router.RouterProvider,
19+
useNavigate: () => () => undefined,
20+
};
21+
});
1522
vi.mock("../state/environments", () => ({ useEnvironmentIds: () => testState.environmentIds }));
1623
vi.mock("../state/pluginNotifications", () => ({
1724
usePluginNotificationFrame: (environmentId: string) =>
@@ -28,12 +35,9 @@ vi.mock("./ui/toast", () => ({
2835
close: (id: string) => void testState.open.delete(id),
2936
},
3037
}));
31-
3238
import { PluginNotificationCoordinator } from "./PluginNotificationCoordinator";
33-
3439
const A = EnvironmentId.make("env-a");
3540
const B = EnvironmentId.make("env-b");
36-
3741
/** The same epoch, sequence and plugin in each environment, so only the environment differs. */
3842
const frame = (title?: string): PluginNotificationFrame => ({
3943
epoch: "epoch-1",
@@ -50,58 +54,39 @@ const frame = (title?: string): PluginNotificationFrame => ({
5054
},
5155
],
5256
});
53-
54-
let root: Root;
55-
let container: HTMLDivElement;
56-
57-
beforeEach(() => {
58-
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
59-
container = document.createElement("div");
60-
document.body.append(container);
61-
root = createRoot(container);
62-
});
63-
64-
afterEach(async () => {
65-
await act(async () => root.unmount());
66-
container.remove();
67-
testState.open.clear();
68-
testState.added.length = 0;
69-
testState.nextToast = 0;
70-
testState.frames.clear();
71-
vi.unstubAllGlobals();
72-
});
73-
7457
async function render(
58+
screen: Screen,
7559
environmentIds: ReadonlyArray<EnvironmentId>,
7660
frames: ReadonlyArray<[EnvironmentId, PluginNotificationFrame]>,
7761
) {
7862
testState.environmentIds = environmentIds;
7963
for (const [environmentId, next] of frames) testState.frames.set(environmentId, next);
80-
await act(async () => root.render(<PluginNotificationCoordinator />));
64+
await act(async () => await screen.rerender(<PluginNotificationCoordinator />));
8165
}
82-
8366
it("closes a removed environment's toasts and keeps the others", async () => {
67+
using _setup = await setup();
68+
await using screen = await renderScreen(null, { router: false });
8469
// The first frame on launch only sets the mark; the next one's notification is new.
8570
await render(
71+
screen,
8672
[A, B],
8773
[
8874
[A, frame()],
8975
[B, frame()],
9076
],
9177
);
9278
await render(
79+
screen,
9380
[A, B],
9481
[
9582
[A, frame("from A")],
9683
[B, frame("from B")],
9784
],
9885
);
9986
expect([...testState.open].toSorted()).toEqual(["from A#0", "from B#1"]);
100-
101-
await render([B], []);
87+
await render(screen, [B], []);
10288
expect([...testState.open]).toEqual(["from B#1"]);
10389
});
104-
10590
/** A frame retaining `titles` as sequences 1..n of one epoch, oldest first. */
10691
const retained = (epoch: string, titles: ReadonlyArray<[number, string]>) => ({
10792
epoch,
@@ -113,13 +98,14 @@ const retained = (epoch: string, titles: ReadonlyArray<[number, string]>) => ({
11398
createdAt: "2026-10-04T00:00:00.000Z",
11499
})),
115100
});
116-
117101
it("toasts each notification once across reconnects and closes what left the set", async () => {
118-
await render([A], [[A, retained("e1", [[1, "old"]])]]);
102+
using _setup = await setup();
103+
await using screen = await renderScreen(null, { router: false });
104+
await render(screen, [A], [[A, retained("e1", [[1, "old"]])]]);
119105
// Launch: what the server already held is not news.
120106
expect(testState.added).toEqual([]);
121-
122107
await render(
108+
screen,
123109
[A],
124110
[
125111
[
@@ -132,9 +118,9 @@ it("toasts each notification once across reconnects and closes what left the set
132118
],
133119
);
134120
expect(testState.added).toEqual(["first"]);
135-
136121
// Back online after missing one; the reconnect's first frame is the whole set.
137122
await render(
123+
screen,
138124
[A],
139125
[
140126
[
@@ -149,6 +135,7 @@ it("toasts each notification once across reconnects and closes what left the set
149135
);
150136
// A second reconnect with nothing new shows nothing again.
151137
await render(
138+
screen,
152139
[A],
153140
[
154141
[
@@ -163,12 +150,21 @@ it("toasts each notification once across reconnects and closes what left the set
163150
);
164151
expect(testState.added).toEqual(["first", "missed"]);
165152
expect([...testState.open].toSorted()).toEqual(["first#0", "missed#1"]);
166-
167153
// The plugin was disabled while this client was away: its toasts close on reconnect.
168-
await render([A], [[A, retained("e1", [[1, "old"]])]]);
154+
await render(screen, [A], [[A, retained("e1", [[1, "old"]])]]);
169155
expect([...testState.open]).toEqual([]);
170-
171156
// A server restart starts a new epoch, whose notifications are all new.
172-
await render([A], [[A, retained("e2", [[1, "after restart"]])]]);
157+
await render(screen, [A], [[A, retained("e2", [[1, "after restart"]])]]);
173158
expect(testState.added).toEqual(["first", "missed", "after restart"]);
174159
});
160+
161+
async function setup() {
162+
return {
163+
[Symbol.dispose]() {
164+
testState.frames.clear();
165+
testState.nextToast = 0;
166+
testState.added.length = 0;
167+
testState.open.clear();
168+
},
169+
};
170+
}

0 commit comments

Comments
 (0)