Skip to content

Commit 5928a61

Browse files
committed
Apply fixed Coderabbitai review.
1 parent 010d48d commit 5928a61

12 files changed

Lines changed: 128 additions & 23 deletions

‎resources/tests/debug-bootstrap.test.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { test, vi } from "vitest";
44

55
import { bootDebugPage, fire, settle } from "./debug-page-harness.js";
66

7-
/* The Database panel feature is the failure fixture: it is the only module the
8-
bootstrap loads on demand here, and its import must reject for the degraded
9-
callout to appear. No other scenario in this file ships a panel marker. */
7+
/**
8+
* The Database panel feature is the failure fixture: it is the only module the
9+
* bootstrap loads on demand here, and its import must reject for the degraded
10+
* callout to appear. No other scenario in this file ships a panel marker.
11+
*/
1012
vi.doMock("../src/panels/db.js", () => {
1113
throw new Error("Chunk load failed.");
1214
});

‎resources/tests/debug-grid-url.test.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,10 @@ test("a field that cannot place a caret is only refocused", async () => {
451451
const page = await bootDebugPage({
452452
body: '<input name="Debug[url]" value="/site/index">',
453453
prepare(documentValue) {
454-
/* Older engines expose no selection API on some input types; the source
455-
must still hand the field back its focus. */
454+
/**
455+
* Older engines expose no selection API on some input types; the source
456+
* must still hand the field back its focus.
457+
*/
456458
documentValue.querySelector("input").setSelectionRange = undefined;
457459
},
458460
sessionData: {

‎resources/tests/debug-page-harness.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ export async function bootDebugPage(options) {
215215
settings.prepare(documentValue);
216216
}
217217

218-
/* jsdom's EventTarget methods reject an inheriting receiver, so the facade
219-
forwards them to the window they were taken from. */
218+
/**
219+
* jsdom's EventTarget methods reject an inheriting receiver, so the facade
220+
* forwards them to the window they were taken from.
221+
*/
220222
["addEventListener", "dispatchEvent", "removeEventListener"].forEach(
221223
function (name) {
222224
define(facade, name, win[name].bind(win));

‎resources/tests/toolbar-element-ajax.test.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
// @vitest-environment jsdom
22
import assert from "node:assert/strict";
3-
import { test } from "vitest";
3+
import { afterEach, test } from "vitest";
44

55
import {
66
createToolbar,
77
installLocalStorage,
88
installMatchMedia,
99
renderToolbar,
10+
teardownToolbars,
1011
toolbarPayload,
1112
} from "./toolbar-element-harness.js";
1213

1314
var storage = installLocalStorage({ "yii-debug-toolbar-expanded": "1" });
1415

1516
installMatchMedia();
1617

18+
/**
19+
* Detaches the fixtures and restores the expanded flag unconditionally: a
20+
* thrown assertion must not hand the next test a collapsed bar.
21+
*/
22+
afterEach(() => {
23+
teardownToolbars();
24+
storage.set("yii-debug-toolbar-expanded", "1");
25+
});
26+
1727
function request(overrides) {
1828
return Object.assign(
1929
{

‎resources/tests/toolbar-element-drawer.test.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
// @vitest-environment jsdom
22
import assert from "node:assert/strict";
3-
import { test } from "vitest";
3+
import { afterEach, test } from "vitest";
44

55
import {
66
installLocalStorage,
77
installMatchMedia,
88
renderToolbar,
99
stubBoundingHeight,
1010
stubViewportHeight,
11+
teardownToolbars,
1112
toolbarPayload,
1213
} from "./toolbar-element-harness.js";
1314

1415
var storage = installLocalStorage({ "yii-debug-toolbar-expanded": "1" });
1516

1617
installMatchMedia();
1718

19+
/**
20+
* Detaches the fixtures and restores the expanded flag, which the collapse
21+
* control persists as part of the behaviour under test.
22+
*/
23+
afterEach(() => {
24+
teardownToolbars();
25+
storage.set("yii-debug-toolbar-expanded", "1");
26+
});
27+
1828
function payload(overrides) {
1929
return toolbarPayload(
2030
Object.assign(

‎resources/tests/toolbar-element-extensions.test.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
// @vitest-environment jsdom
22
import assert from "node:assert/strict";
3-
import { test } from "vitest";
3+
import { afterEach, test } from "vitest";
44

55
import {
66
installLocalStorage,
77
installMatchMedia,
88
renderToolbar,
9+
teardownToolbars,
910
toolbarPayload,
1011
} from "./toolbar-element-harness.js";
1112

1213
installLocalStorage({ "yii-debug-toolbar-expanded": "1" });
1314
installMatchMedia();
1415

16+
/** Detaches the fixtures a thrown assertion would leave connected. */
17+
afterEach(teardownToolbars);
18+
1519
function payload(overrides) {
1620
return toolbarPayload(
1721
Object.assign(

‎resources/tests/toolbar-element-harness.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,30 @@ export function installXmlHttpRequest() {
134134
};
135135
}
136136

137+
/**
138+
* Fixtures this module attached to the document, in attachment order.
139+
*
140+
* Only what the harness appends is recorded: a detached `createToolbar()`
141+
* fixture stays out of it, so the lifecycle tests keep driving
142+
* `disconnectedCallback()` themselves.
143+
*/
144+
var attached = [];
145+
146+
/**
147+
* Detaches every recorded fixture still in the document and forgets all of
148+
* them, so a thrown assertion cannot leave a connected toolbar registered in
149+
* `toolbars`, listening on the document or holding a theme observer.
150+
*/
151+
export function teardownToolbars() {
152+
for (var i = attached.length - 1; i >= 0; i--) {
153+
if (attached[i].isConnected) {
154+
attached[i].remove();
155+
}
156+
}
157+
158+
attached.length = 0;
159+
}
160+
137161
export function defineToolbar() {
138162
if (!window.customElements.get(tagName)) {
139163
window.customElements.define(tagName, YiiDebugToolbar);
@@ -159,6 +183,7 @@ export function connectToolbar(attributes) {
159183
var element = createToolbar(attributes);
160184

161185
document.body.appendChild(element);
186+
attached.push(element);
162187

163188
return element;
164189
}
@@ -212,6 +237,7 @@ export function stubFrameWindow(frame) {
212237
var donor = document.createElement("iframe");
213238

214239
document.body.appendChild(donor);
240+
attached.push(donor);
215241
Object.defineProperty(frame, "contentWindow", {
216242
configurable: true,
217243
value: donor.contentWindow,

‎resources/tests/toolbar-element-lifecycle.test.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @vitest-environment jsdom
22
import assert from "node:assert/strict";
3-
import { test, vi } from "vitest";
3+
import { afterEach, test, vi } from "vitest";
44

55
import { toolbars } from "../src/toolbar/state.js";
66
import {
@@ -9,11 +9,27 @@ import {
99
installMatchMedia,
1010
removeMatchMedia,
1111
renderToolbar,
12+
teardownToolbars,
1213
toolbarPayload,
1314
} from "./toolbar-element-harness.js";
1415

16+
var mutationObserver = window.MutationObserver;
17+
1518
installLocalStorage({ "yii-debug-toolbar-expanded": "1" });
1619

20+
/**
21+
* Restores what a thrown assertion would leave behind: a connected toolbar, a
22+
* fake clock, the deleted `MutationObserver` and the removed media query. The
23+
* detached `createToolbar()` fixtures stay untouched, so the tests that drive
24+
* `disconnectedCallback()` themselves keep proving what it does.
25+
*/
26+
afterEach(() => {
27+
teardownToolbars();
28+
vi.useRealTimers();
29+
window.MutationObserver = mutationObserver;
30+
installMatchMedia();
31+
});
32+
1733
function payload() {
1834
return toolbarPayload({
1935
items: [

‎resources/tests/toolbar-element-loading.test.js‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
// @vitest-environment jsdom
22
import assert from "node:assert/strict";
3-
import { test, vi } from "vitest";
3+
import { afterEach, test, vi } from "vitest";
44

55
import {
6+
connectToolbar,
67
createToolbar,
78
installLocalStorage,
89
installMatchMedia,
910
installXmlHttpRequest,
11+
teardownToolbars,
1012
} from "./toolbar-element-harness.js";
1113

1214
installLocalStorage({ "yii-debug-toolbar-expanded": "1" });
1315
installMatchMedia();
1416

1517
var transport = installXmlHttpRequest();
1618

19+
/** Detaches the fixtures and drops a fake clock a thrown assertion left set. */
20+
afterEach(() => {
21+
teardownToolbars();
22+
vi.useRealTimers();
23+
});
24+
1725
var snapshot = JSON.stringify({
1826
items: [{ id: "db", title: "Database", url: "/debug/db" }],
1927
tag: "tag-1",
2028
title: "Yii Debugger",
2129
});
2230

2331
function mount(attributes) {
24-
var element = createToolbar(attributes);
25-
26-
document.body.appendChild(element);
27-
28-
return element;
32+
return connectToolbar(attributes);
2933
}
3034

3135
function errorMessage(element) {

‎resources/tests/toolbar-element-render.test.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
// @vitest-environment jsdom
22
import assert from "node:assert/strict";
3-
import { test } from "vitest";
3+
import { afterEach, test } from "vitest";
44

55
import {
66
createToolbar,
77
installLocalStorage,
88
installMatchMedia,
99
renderToolbar,
10+
teardownToolbars,
1011
toolbarPayload,
1112
} from "./toolbar-element-harness.js";
1213

1314
var storage = installLocalStorage();
1415

1516
installMatchMedia();
1617

18+
/** Detaches the fixtures a thrown assertion would leave connected. */
19+
afterEach(teardownToolbars);
20+
1721
function expanded() {
1822
storage.set("yii-debug-toolbar-expanded", "1");
1923
}

0 commit comments

Comments
 (0)