Skip to content

Commit 8356d3b

Browse files
committed
test(web): align plugin render lifecycle and available panel counters
1 parent 684267c commit 8356d3b

3 files changed

Lines changed: 157 additions & 53 deletions

File tree

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { act, render as renderScreen, type Screen } from "../../../test/render";
12
import {
23
type ContributionStatusEntry,
34
EnvironmentId,
@@ -6,24 +7,17 @@ import {
67
ProviderSessionId,
78
ThreadId,
89
} from "@t3tools/contracts";
9-
import { act } from "react";
10-
import { createRoot, type Root } from "react-dom/client";
11-
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
12-
10+
import { describe, expect, it, vi } from "vite-plus/test";
1311
const testState = vi.hoisted(() => ({
1412
entries: [] as ReadonlyArray<ContributionStatusEntry>,
1513
}));
16-
1714
vi.mock("../../state/contributionStatus", () => ({
1815
useThreadContributionStatus: () => testState.entries,
1916
}));
20-
2117
import { ThreadContributionStatus } from "./ThreadContributionStatus";
22-
2318
const THREAD = ThreadId.make("thread-1");
2419
const ENVIRONMENT = EnvironmentId.make("environment-1");
2520
const PI_ORIGIN = "From a Pi extension. It can lag a session change.";
26-
2721
const entry = (items: ContributionStatusEntry["items"]): ContributionStatusEntry => ({
2822
threadId: THREAD,
2923
source: {
@@ -34,108 +28,95 @@ const entry = (items: ContributionStatusEntry["items"]): ContributionStatusEntry
3428
},
3529
items,
3630
});
37-
38-
let root: Root;
39-
let container: HTMLDivElement;
40-
41-
beforeEach(() => {
42-
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
43-
container = document.createElement("div");
44-
document.body.append(container);
45-
root = createRoot(container);
46-
});
47-
48-
afterEach(async () => {
49-
await act(async () => root.unmount());
50-
container.remove();
51-
testState.entries = [];
52-
vi.unstubAllGlobals();
53-
});
54-
55-
async function renderStatus(entries: ReadonlyArray<ContributionStatusEntry>) {
31+
async function renderStatus(screen: Screen, entries: ReadonlyArray<ContributionStatusEntry>) {
5632
testState.entries = entries;
5733
await act(async () => {
58-
root.render(<ThreadContributionStatus environmentId={ENVIRONMENT} threadId={THREAD} />);
34+
await screen.rerender(
35+
<ThreadContributionStatus environmentId={ENVIRONMENT} threadId={THREAD} />,
36+
);
5937
});
6038
}
61-
6239
/** The trigger's accessible name: its explicit label, else its text. */
6340
function accessibleName(element: Element) {
6441
return element.getAttribute("aria-label") ?? element.textContent ?? "";
6542
}
66-
67-
function statusButton() {
68-
const button = [...container.querySelectorAll("button")].find((candidate) =>
43+
function statusButton(screen: Screen) {
44+
const button = [...screen.container.querySelectorAll("button")].find((candidate) =>
6945
accessibleName(candidate).startsWith("Provider status"),
7046
);
7147
if (!button) throw new Error("Status button was not rendered");
7248
return button;
7349
}
74-
7550
function popupText() {
7651
return document.querySelector('[data-slot="popover-popup"]')?.textContent ?? null;
7752
}
78-
7953
describe("ThreadContributionStatus", () => {
8054
it("counts statuses past the inline ones and lists every one when tapped", async () => {
81-
await renderStatus([
55+
using _setup = await setup();
56+
await using screen = await renderScreen(null, { router: false });
57+
await renderStatus(screen, [
8258
entry([
8359
{ key: "a", text: "● plan" },
8460
{ key: "b", text: "build 3/9" },
8561
{ key: "c", text: "lint clean", tone: "success" },
8662
{ key: "d", text: "disk almost full", tone: "warning" },
8763
]),
8864
]);
89-
90-
const button = statusButton();
65+
const button = statusButton(screen);
9166
expect(button.textContent).toBe("● planbuild 3/9+2");
9267
expect(popupText()).toBeNull();
93-
9468
await act(async () => {
9569
button.dispatchEvent(
9670
new PointerEvent("pointerdown", { bubbles: true, pointerType: "touch" }),
9771
);
9872
button.click();
9973
});
100-
10174
const text = popupText();
10275
for (const status of ["● plan", "build 3/9", "lint clean", "disk almost full", PI_ORIGIN]) {
10376
expect(text).toContain(status);
10477
}
10578
});
106-
10779
it("is a focusable native button, so keyboard users reach the same list", async () => {
108-
await renderStatus([entry([{ key: "mode", text: "● plan" }])]);
109-
110-
const button = statusButton();
80+
using _setup = await setup();
81+
await using screen = await renderScreen(null, { router: false });
82+
await renderStatus(screen, [entry([{ key: "mode", text: "● plan" }])]);
83+
const button = statusButton(screen);
11184
button.focus();
11285
expect(document.activeElement).toBe(button);
11386
expect(button.tagName).toBe("BUTTON");
11487
});
115-
11688
it("names the control with every status text, even when an item has a tooltip", async () => {
89+
using _setup = await setup();
90+
await using screen = await renderScreen(null, { router: false });
11791
const longText = `● ${"x".repeat(78)}`;
118-
await renderStatus([
92+
await renderStatus(screen, [
11993
entry([
12094
{ key: "a", text: "Plan", tooltip: "12 files to change" },
12195
{ key: "b", text: longText },
12296
{ key: "c", text: "disk almost full", tone: "warning" },
12397
]),
12498
]);
125-
126-
expect(accessibleName(statusButton())).toBe(
99+
expect(accessibleName(statusButton(screen))).toBe(
127100
`Provider status: Plan, ${longText}, disk almost full`,
128101
);
129-
130-
await act(async () => statusButton().click());
102+
await act(async () => statusButton(screen).click());
131103
const text = popupText();
132104
expect(text).toContain("Plan");
133105
expect(text).toContain("12 files to change");
134106
expect(text).toContain(longText);
135107
});
136-
137108
it("renders nothing without statuses", async () => {
138-
await renderStatus([]);
139-
expect(container.childElementCount).toBe(0);
109+
using _setup = await setup();
110+
await using screen = await renderScreen(null, { router: false });
111+
await renderStatus(screen, []);
112+
expect(screen.container.childElementCount).toBe(0);
140113
});
141114
});
115+
116+
async function setup() {
117+
return {
118+
[Symbol.dispose]() {
119+
testState.entries = [];
120+
},
121+
};
122+
}

‎apps/web/src/panels/bundledPanels.browser.test.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ describe("bundled side panels", () => {
148148
pullRequest: 0,
149149
pullRequests: 0,
150150
files: 0,
151-
pluginView: 0,
152151
});
153152
});
154153
});

‎apps/web/src/panels/device/DeviceSidePanel.notices.browser.test.tsx‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,127 @@ it("keeps a late power-off error in the environment that started it", async () =
207207
await screen.rerender(devicePanel(threadA, streaming));
208208
expect(errors()).toHaveLength(1);
209209
});
210+
211+
const otherEnvironment = switches[1][1];
212+
const streaming: Extract<RightPanelSurface, { kind: "device" }> = {
213+
id: "device:phone",
214+
kind: "device",
215+
target: { hostId: "local", deviceId: "phone", platform: "ios", name: "Phone" },
216+
};
217+
218+
describe("DevicePanel notices", () => {
219+
it("keeps discovery warnings dismissed until the diagnostic or environment changes", async () => {
220+
setup();
221+
await using screen = await render(null, { container, router: false });
222+
currentState = {
223+
...deviceState,
224+
hostStatusDetail: "Could not read simulator usage timestamps.",
225+
};
226+
await screen.rerender(devicePanel(threadA));
227+
expect(errors()).toHaveLength(1);
228+
expect(startButton()?.disabled).toBe(false);
229+
await click(container.querySelector('[aria-label="Dismiss device warning"]'));
230+
expect(errors()).toHaveLength(0);
231+
await screen.rerender(devicePanel(threadA));
232+
expect(errors()).toHaveLength(0);
233+
234+
currentState = { ...currentState, hostStatusDetail: "A different discovery warning." };
235+
await screen.rerender(devicePanel(threadA));
236+
expect(errors()).toHaveLength(1);
237+
await click(container.querySelector('[aria-label="Dismiss device warning"]'));
238+
await screen.rerender(devicePanel(otherEnvironment));
239+
expect(errors()).toHaveLength(1);
240+
});
241+
242+
it("keeps the loading content centered when discovery and boot notices arrive or are dismissed", async () => {
243+
setup();
244+
await using screen = await render(null, { container, router: false });
245+
const pick = deferred();
246+
mocks.open.mockReturnValue(pick.promise);
247+
await screen.rerender(devicePanel(threadA));
248+
await click(startButton());
249+
const loadingBounds = () =>
250+
container
251+
.querySelector('[aria-label="Step 1 of 2: open device"]')!
252+
.parentElement!.getBoundingClientRect();
253+
const before = loadingBounds();
254+
const panel = container.getBoundingClientRect();
255+
expect(before.height).toBeGreaterThan(0);
256+
expect(before.top + before.height / 2).toBeCloseTo(panel.top + panel.height / 2, 0);
257+
258+
currentState = {
259+
...deviceState,
260+
hostStatusDetail: "Could not read CoreSimulator plist.\n".repeat(8),
261+
bootingDevices: [{ ...phone, threadId: threadA.threadId }],
262+
};
263+
await screen.rerender(devicePanel(threadA));
264+
expect(loadingBounds().top).toBe(before.top);
265+
expect(loadingBounds().left).toBe(before.left);
266+
await click(container.querySelector('[aria-label="Dismiss device warning"]'));
267+
expect(loadingBounds().top).toBe(before.top);
268+
expect(loadingBounds().left).toBe(before.left);
269+
await act(async () => pick.settle(failed));
270+
});
271+
});
272+
273+
describe("DevicePanel thread switches", () => {
274+
it.each(switches)(
275+
"keeps a pending or failed pick in its starting scope when switching to %s",
276+
async (_, target) => {
277+
setup();
278+
await using screen = await render(null, { container, router: false });
279+
const pick = deferred();
280+
mocks.open.mockReturnValue(pick.promise);
281+
await screen.rerender(devicePanel(threadA));
282+
await click(startButton());
283+
expect(startButton()).toBeNull();
284+
285+
await screen.rerender(devicePanel(target));
286+
expect(startButton()?.disabled).toBe(false);
287+
288+
await act(async () => pick.settle(failed));
289+
expect(errors()).toHaveLength(0);
290+
expect(mocks.openDevice).not.toHaveBeenCalled();
291+
292+
await screen.rerender(devicePanel(threadA));
293+
expect(startButton()?.disabled).toBe(false);
294+
expect(errors()).toHaveLength(1);
295+
296+
await screen.rerender(devicePanel(target));
297+
expect(startButton()?.disabled).toBe(false);
298+
expect(errors()).toHaveLength(0);
299+
},
300+
);
301+
302+
it.each([
303+
[
304+
"pick",
305+
picker,
306+
() => mocks.open,
307+
() => click(startButton()),
308+
() => expect(mocks.openDevice).toHaveBeenCalledExactlyOnceWith(threadA, phoneTarget),
309+
],
310+
[
311+
"power-off",
312+
streaming,
313+
() => mocks.close,
314+
() => click(container.querySelector("button")),
315+
() => expect(mocks.closeSurface).toHaveBeenCalledExactlyOnceWith(threadA, streaming.id),
316+
],
317+
] as const)(
318+
"applies a late %s to its starting thread after switching environments with the same thread id",
319+
async (_, surface, command, start, expectApplied) => {
320+
setup();
321+
await using screen = await render(null, { container, router: false });
322+
const late = deferred();
323+
command().mockReturnValue(late.promise);
324+
await screen.rerender(devicePanel(threadA, surface));
325+
await start();
326+
327+
await screen.rerender(devicePanel(otherEnvironment, surface));
328+
await act(async () => late.settle(opened));
329+
330+
expectApplied();
331+
},
332+
);
333+
});

0 commit comments

Comments
 (0)