|
1 | 1 | // @vitest-environment happy-dom |
2 | 2 |
|
3 | | -import React, { act } from "react"; |
| 3 | +import React, { act, useState } from "react"; |
4 | 4 | import { createRoot } from "react-dom/client"; |
5 | 5 | import { afterEach, describe, expect, it, vi } from "vitest"; |
6 | | -import { FlatTextLayerList } from "./propertyPanelFlatTextSection"; |
| 6 | +import { FlatTextLayerList, FlatTextSection } from "./propertyPanelFlatTextSection"; |
| 7 | +import type { DomEditSelection, DomEditTextField } from "./domEditingTypes"; |
7 | 8 |
|
8 | 9 | (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
9 | 10 |
|
@@ -86,3 +87,166 @@ describe("FlatTextLayerList", () => { |
86 | 87 | act(() => root.unmount()); |
87 | 88 | }); |
88 | 89 | }); |
| 90 | + |
| 91 | +function makeMultiFieldElement(): DomEditSelection { |
| 92 | + return { |
| 93 | + element: document.createElement("div"), |
| 94 | + id: "multi", |
| 95 | + selector: ".multi", |
| 96 | + label: "Multi", |
| 97 | + tagName: "div", |
| 98 | + sourceFile: "index.html", |
| 99 | + compositionPath: "index.html", |
| 100 | + isCompositionHost: false, |
| 101 | + isInsideLockedComposition: false, |
| 102 | + boundingBox: { x: 0, y: 0, width: 100, height: 100 }, |
| 103 | + textContent: "Headline Subhead", |
| 104 | + dataAttributes: {}, |
| 105 | + inlineStyles: {}, |
| 106 | + computedStyles: {}, |
| 107 | + textFields: [ |
| 108 | + { |
| 109 | + key: "a", |
| 110 | + label: "Text", |
| 111 | + value: "Headline", |
| 112 | + tagName: "div", |
| 113 | + attributes: [], |
| 114 | + inlineStyles: {}, |
| 115 | + computedStyles: {}, |
| 116 | + source: "self", |
| 117 | + }, |
| 118 | + { |
| 119 | + key: "b", |
| 120 | + label: "Text", |
| 121 | + value: "Subhead", |
| 122 | + tagName: "span", |
| 123 | + attributes: [], |
| 124 | + inlineStyles: {}, |
| 125 | + computedStyles: {}, |
| 126 | + source: "self", |
| 127 | + }, |
| 128 | + ], |
| 129 | + capabilities: { |
| 130 | + canSelect: true, |
| 131 | + canEditStyles: true, |
| 132 | + canCrop: true, |
| 133 | + canMove: true, |
| 134 | + canResize: true, |
| 135 | + canApplyManualOffset: true, |
| 136 | + canApplyManualSize: true, |
| 137 | + canApplyManualRotation: true, |
| 138 | + }, |
| 139 | + } as DomEditSelection; |
| 140 | +} |
| 141 | + |
| 142 | +describe("FlatTextSection — multi-field", () => { |
| 143 | + it("shows the layer list, switches the active field's rows on selection, and has no doubled heading (this component never renders its own heading — the parent FlatGroup does)", () => { |
| 144 | + const host = document.createElement("div"); |
| 145 | + document.body.append(host); |
| 146 | + const root = createRoot(host); |
| 147 | + act(() => { |
| 148 | + root.render( |
| 149 | + <FlatTextSection |
| 150 | + element={makeMultiFieldElement()} |
| 151 | + styles={{}} |
| 152 | + fontAssets={[]} |
| 153 | + onSetText={vi.fn()} |
| 154 | + onSetTextFieldStyle={vi.fn()} |
| 155 | + onAddTextField={vi.fn()} |
| 156 | + onRemoveTextField={vi.fn()} |
| 157 | + />, |
| 158 | + ); |
| 159 | + }); |
| 160 | + expect(host.textContent).toContain("Headline"); |
| 161 | + expect(host.textContent).toContain("Subhead"); |
| 162 | + // Active field's editor rows are visible (Font/Weight/etc. from FlatTextFieldEditor). |
| 163 | + expect(host.textContent).toContain("Weight"); |
| 164 | + // Exactly one "Text layers" micro-label — this component doesn't duplicate its own list. |
| 165 | + const layerLabels = Array.from(host.querySelectorAll("div")).filter( |
| 166 | + (el) => el.textContent === "Text layers", |
| 167 | + ); |
| 168 | + expect(layerLabels.length).toBeLessThanOrEqual(1); |
| 169 | + |
| 170 | + const rows = host.querySelectorAll('[data-flat-text-layer-row="true"]'); |
| 171 | + act(() => rows[1].dispatchEvent(new MouseEvent("click", { bubbles: true }))); |
| 172 | + expect(host.textContent).toContain("Subhead"); |
| 173 | + act(() => root.unmount()); |
| 174 | + }); |
| 175 | + |
| 176 | + it("wires onAdd/onRemove end-to-end: async onAddTextField switches the active field once it appears in props, and the resync effect falls back to the first field when the active one disappears", async () => { |
| 177 | + let addResolved = false; |
| 178 | + |
| 179 | + function Harness() { |
| 180 | + const [fields, setFields] = useState<DomEditTextField[]>(makeMultiFieldElement().textFields); |
| 181 | + const element: DomEditSelection = { ...makeMultiFieldElement(), textFields: fields }; |
| 182 | + return ( |
| 183 | + <FlatTextSection |
| 184 | + element={element} |
| 185 | + styles={{}} |
| 186 | + fontAssets={[]} |
| 187 | + onSetText={vi.fn()} |
| 188 | + onSetTextFieldStyle={vi.fn()} |
| 189 | + onAddTextField={() => |
| 190 | + Promise.resolve().then(() => { |
| 191 | + addResolved = true; |
| 192 | + setFields((prev) => [ |
| 193 | + ...prev, |
| 194 | + { |
| 195 | + key: "c", |
| 196 | + label: "Text", |
| 197 | + value: "Third", |
| 198 | + tagName: "div", |
| 199 | + attributes: [], |
| 200 | + inlineStyles: {}, |
| 201 | + computedStyles: {}, |
| 202 | + source: "self", |
| 203 | + }, |
| 204 | + ]); |
| 205 | + return "c"; |
| 206 | + }) |
| 207 | + } |
| 208 | + onRemoveTextField={(fieldKey: string) => |
| 209 | + setFields((prev) => prev.filter((field) => field.key !== fieldKey)) |
| 210 | + } |
| 211 | + /> |
| 212 | + ); |
| 213 | + } |
| 214 | + |
| 215 | + const host = document.createElement("div"); |
| 216 | + document.body.append(host); |
| 217 | + const root = createRoot(host); |
| 218 | + act(() => { |
| 219 | + root.render(<Harness />); |
| 220 | + }); |
| 221 | + |
| 222 | + let rows = host.querySelectorAll('[data-flat-text-layer-row="true"]'); |
| 223 | + expect(rows).toHaveLength(2); |
| 224 | + expect((rows[0] as HTMLElement).getAttribute("data-active")).toBe("true"); |
| 225 | + |
| 226 | + const addButton = host.querySelector<HTMLButtonElement>('[data-flat-text-layer-add="true"]'); |
| 227 | + await act(async () => { |
| 228 | + addButton?.dispatchEvent(new MouseEvent("click", { bubbles: true })); |
| 229 | + await Promise.resolve(); |
| 230 | + await Promise.resolve(); |
| 231 | + }); |
| 232 | + expect(addResolved).toBe(true); |
| 233 | + rows = host.querySelectorAll('[data-flat-text-layer-row="true"]'); |
| 234 | + expect(rows).toHaveLength(3); |
| 235 | + expect((rows[2] as HTMLElement).getAttribute("data-active")).toBe("true"); |
| 236 | + |
| 237 | + // Remove the active field ("c") through the wired onRemoveTextField — the |
| 238 | + // resync useEffect must fall back to the first remaining field ("a") |
| 239 | + // since "c" no longer exists in element.textFields. |
| 240 | + const removeButtons = host.querySelectorAll<HTMLButtonElement>( |
| 241 | + '[data-flat-text-layer-remove="true"]', |
| 242 | + ); |
| 243 | + act(() => { |
| 244 | + removeButtons[2].dispatchEvent(new MouseEvent("click", { bubbles: true })); |
| 245 | + }); |
| 246 | + rows = host.querySelectorAll('[data-flat-text-layer-row="true"]'); |
| 247 | + expect(rows).toHaveLength(2); |
| 248 | + expect((rows[0] as HTMLElement).getAttribute("data-active")).toBe("true"); |
| 249 | + |
| 250 | + act(() => root.unmount()); |
| 251 | + }); |
| 252 | +}); |
0 commit comments