Skip to content

Commit fbb83f2

Browse files
committed
refactor(studio): share the flat-inspector test render harness across panel suites
The eight flat property-panel suites each carried an identical copy of the React act-environment setup and a local renderInto() helper. Extract both into testRenderUtils so the harness has one definition to fix. No behavior change: setupReactActEnvironment() registers the same document.body cleanup each file registered inline, and renderInto() is the same createRoot + act wrapper.
1 parent a3c8f89 commit fbb83f2

9 files changed

Lines changed: 54 additions & 122 deletions

packages/studio/src/components/editor/PropertyPanel.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
getCssFilterFunctionPx,
99
inferBoxShadowPreset,
1010
inferClipPathPreset,
11+
isSelectedElementHidden,
1112
normalizePanelPxValue,
1213
parseInsetClipPathSides,
1314
setCssFilterFunctionPx,
14-
} from "./PropertyPanel";
15-
import { isSelectedElementHidden } from "./propertyPanelHelpers";
15+
} from "./propertyPanelHelpers";
1616

1717
describe("PropertyPanel style helpers", () => {
1818
it("normalizes bounded pixel values without accepting incompatible units", () => {

packages/studio/src/components/editor/PropertyPanelEmptyState.test.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
// @vitest-environment happy-dom
22

3-
import React, { act } from "react";
4-
import { createRoot } from "react-dom/client";
5-
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { act } from "react";
4+
import { describe, expect, it, vi } from "vitest";
65
import { PropertyPanelEmptyState } from "./PropertyPanelEmptyState";
76
import type { DomEditSelection } from "./domEditingTypes";
7+
import { renderInto, setupReactActEnvironment } from "./testRenderUtils";
88

9-
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
10-
11-
afterEach(() => {
12-
document.body.innerHTML = "";
13-
});
14-
15-
function renderInto(node: React.ReactElement) {
16-
const host = document.createElement("div");
17-
document.body.append(host);
18-
const root = createRoot(host);
19-
act(() => {
20-
root.render(node);
21-
});
22-
return { host, root };
23-
}
9+
setupReactActEnvironment();
2410

2511
describe("PropertyPanelEmptyState — flat empty", () => {
2612
it("shows the cursor glyph, headline, and the two shortcut rows", () => {

packages/studio/src/components/editor/propertyPanelFlatColorGradingSection.test.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// @vitest-environment happy-dom
22

3-
import React, { act } from "react";
4-
import { createRoot } from "react-dom/client";
5-
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { act } from "react";
4+
import { describe, expect, it, vi } from "vitest";
65
import {
76
FlatColorGradingAccessory,
87
FlatColorGradingSection,
@@ -11,22 +10,9 @@ import {
1110
normalizeHfColorGrading,
1211
type NormalizedHfColorGrading,
1312
} from "@hyperframes/core/color-grading";
13+
import { renderInto, setupReactActEnvironment } from "./testRenderUtils";
1414

15-
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
16-
17-
afterEach(() => {
18-
document.body.innerHTML = "";
19-
});
20-
21-
function renderInto(node: React.ReactElement) {
22-
const host = document.createElement("div");
23-
document.body.append(host);
24-
const root = createRoot(host);
25-
act(() => {
26-
root.render(node);
27-
});
28-
return { host, root };
29-
}
15+
setupReactActEnvironment();
3016

3117
function neutralGrading() {
3218
const grading = normalizeHfColorGrading("neutral");

packages/studio/src/components/editor/propertyPanelFlatLayoutSection.test.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
11
// @vitest-environment happy-dom
22

3-
import React, { act } from "react";
4-
import { createRoot } from "react-dom/client";
5-
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { act } from "react";
4+
import { describe, expect, it, vi } from "vitest";
65
import {
76
FlatLayoutSection,
87
LayoutFlexBlock,
98
LayoutGeometryRows,
109
LayoutTransform3DBlock,
1110
LayoutZIndexRow,
1211
} from "./propertyPanelFlatLayoutSection";
12+
import { renderInto, setupReactActEnvironment } from "./testRenderUtils";
1313

14-
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
15-
16-
afterEach(() => {
17-
document.body.innerHTML = "";
18-
});
19-
20-
function renderInto(node: React.ReactElement) {
21-
const host = document.createElement("div");
22-
document.body.append(host);
23-
const root = createRoot(host);
24-
act(() => {
25-
root.render(node);
26-
});
27-
return { host, root };
28-
}
14+
setupReactActEnvironment();
2915

3016
function getFlatRowInput(host: HTMLElement, label: string): HTMLInputElement {
3117
const rows = Array.from(host.querySelectorAll<HTMLElement>(".group"));

packages/studio/src/components/editor/propertyPanelFlatMotionSection.test.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// @vitest-environment happy-dom
22

3-
import React, { act } from "react";
4-
import { createRoot } from "react-dom/client";
3+
import { act } from "react";
54
import { afterEach, describe, expect, it, vi } from "vitest";
65
import { FlatMotionSection, FlatTimingRow } from "./propertyPanelFlatMotionSection";
76
import type { DomEditSelection } from "./domEditing";
87
import { usePlayerStore } from "../../player";
8+
import { renderInto, setupReactActEnvironment } from "./testRenderUtils";
99

10-
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
10+
setupReactActEnvironment();
1111

1212
afterEach(() => {
13-
document.body.innerHTML = "";
1413
// The store is module-global, so a test that parks a focused ease segment
1514
// would otherwise leak it into every test that runs after it.
1615
usePlayerStore.getState().reset();
@@ -47,16 +46,6 @@ function baseElement(overrides: Partial<DomEditSelection> = {}): DomEditSelectio
4746
} as DomEditSelection;
4847
}
4948

50-
function renderInto(node: React.ReactElement) {
51-
const host = document.createElement("div");
52-
document.body.append(host);
53-
const root = createRoot(host);
54-
act(() => {
55-
root.render(node);
56-
});
57-
return { host, root };
58-
}
59-
6049
describe("FlatTimingRow", () => {
6150
it("renders Start, End, and Duration from the element's data attributes", () => {
6251
const { host, root } = renderInto(

packages/studio/src/components/editor/propertyPanelFlatPrimitives.test.tsx

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
11
// @vitest-environment happy-dom
22

33
import React, { act } from "react";
4-
import { createRoot } from "react-dom/client";
5-
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import { describe, expect, it, vi } from "vitest";
65
import {
76
FlatGroupHeader,
87
FlatRow,
98
FlatSegmentedRow,
109
FlatSelectRow,
1110
FlatSlider,
1211
} from "./propertyPanelFlatPrimitives";
12+
import { renderInto, setupReactActEnvironment } from "./testRenderUtils";
1313

14-
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
15-
16-
afterEach(() => {
17-
document.body.innerHTML = "";
18-
});
19-
20-
function renderInto(node: React.ReactElement) {
21-
const host = document.createElement("div");
22-
document.body.append(host);
23-
const root = createRoot(host);
24-
act(() => {
25-
root.render(node);
26-
});
27-
return { host, root };
28-
}
14+
setupReactActEnvironment();
2915

3016
describe("FlatRow", () => {
3117
it("renders the default tier with no reset button", () => {

packages/studio/src/components/editor/propertyPanelFlatTextSection.test.tsx

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
// @vitest-environment happy-dom
22

3-
import React, { act, useState } from "react";
3+
import { act, useState } from "react";
44
import { createRoot } from "react-dom/client";
55
import postcss from "postcss";
66
import tailwindcss from "tailwindcss";
7-
import { afterEach, describe, expect, it, vi } from "vitest";
7+
import { describe, expect, it, vi } from "vitest";
88
import { FlatTextLayerList, FlatTextSection } from "./propertyPanelFlatTextSection";
99
import type { DomEditSelection, DomEditTextField } from "./domEditingTypes";
10+
import { renderInto, setupReactActEnvironment } from "./testRenderUtils";
1011

11-
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
12-
13-
afterEach(() => {
14-
document.body.innerHTML = "";
15-
});
16-
17-
function renderInto(node: React.ReactElement) {
18-
const host = document.createElement("div");
19-
document.body.append(host);
20-
const root = createRoot(host);
21-
act(() => {
22-
root.render(node);
23-
});
24-
return { host, root };
25-
}
12+
setupReactActEnvironment();
2613

2714
const FIELDS = [
2815
{

packages/studio/src/components/editor/propertyPanelFlatToggle.test.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
11
// @vitest-environment happy-dom
22

3-
import React, { act } from "react";
4-
import { createRoot } from "react-dom/client";
5-
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import { act } from "react";
4+
import { describe, expect, it, vi } from "vitest";
65
import { FlatToggle } from "./propertyPanelFlatToggle";
6+
import { renderInto, setupReactActEnvironment } from "./testRenderUtils";
77

8-
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
9-
10-
afterEach(() => {
11-
document.body.innerHTML = "";
12-
});
13-
14-
function renderInto(node: React.ReactElement) {
15-
const host = document.createElement("div");
16-
document.body.append(host);
17-
const root = createRoot(host);
18-
act(() => {
19-
root.render(node);
20-
});
21-
return { host, root };
22-
}
8+
setupReactActEnvironment();
239

2410
describe("FlatToggle", () => {
2511
it("renders the off state with a dim label and dim knob, and fires onChange(true) on click", () => {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, { act } from "react";
2+
import { createRoot } from "react-dom/client";
3+
import { afterEach } from "vitest";
4+
5+
/**
6+
* Marks the current environment as React-act-aware and registers the shared
7+
* per-test DOM cleanup. Call once per test file, at module scope, before any
8+
* `describe`/`it` blocks run.
9+
*/
10+
export function setupReactActEnvironment(): void {
11+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
12+
13+
afterEach(() => {
14+
document.body.innerHTML = "";
15+
});
16+
}
17+
18+
export function renderInto(node: React.ReactElement) {
19+
const host = document.createElement("div");
20+
document.body.append(host);
21+
const root = createRoot(host);
22+
act(() => {
23+
root.render(node);
24+
});
25+
return { host, root };
26+
}

0 commit comments

Comments
 (0)