|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import React, { act } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { StyleSections } from "./propertyPanelStyleSections"; |
| 6 | +import { FlatStyleSection } from "./propertyPanelFlatStyleSections"; |
| 7 | +import type { DomEditSelection } from "./domEditing"; |
| 8 | + |
| 9 | +(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 10 | +afterEach(() => { |
| 11 | + document.body.innerHTML = ""; |
| 12 | +}); |
| 13 | + |
| 14 | +function mount( |
| 15 | + Panel: typeof StyleSections | typeof FlatStyleSection, |
| 16 | + tag = "img", |
| 17 | + sourceFile = "index.html", |
| 18 | + src = "assets/old.png", |
| 19 | +) { |
| 20 | + const selected = document.createElement(tag); |
| 21 | + selected.id = "picture"; |
| 22 | + selected.setAttribute("src", src); |
| 23 | + document.body.append(selected); |
| 24 | + const element: DomEditSelection = { |
| 25 | + element: selected, |
| 26 | + id: "picture", |
| 27 | + selector: "#picture", |
| 28 | + label: "Picture", |
| 29 | + tagName: tag, |
| 30 | + sourceFile, |
| 31 | + compositionPath: sourceFile, |
| 32 | + isCompositionHost: false, |
| 33 | + isInsideLockedComposition: false, |
| 34 | + boundingBox: { x: 0, y: 0, width: 320, height: 180 }, |
| 35 | + textContent: "", |
| 36 | + dataAttributes: {}, |
| 37 | + inlineStyles: {}, |
| 38 | + computedStyles: {}, |
| 39 | + textFields: [], |
| 40 | + capabilities: { |
| 41 | + canSelect: true, |
| 42 | + canEditStyles: true, |
| 43 | + canCrop: true, |
| 44 | + canMove: true, |
| 45 | + canResize: true, |
| 46 | + canApplyManualOffset: true, |
| 47 | + canApplyManualSize: true, |
| 48 | + canApplyManualRotation: true, |
| 49 | + }, |
| 50 | + }; |
| 51 | + const host = document.createElement("div"); |
| 52 | + document.body.append(host); |
| 53 | + const root = createRoot(host); |
| 54 | + const onSetStyle = vi.fn((prop: string, value: string) => |
| 55 | + selected.style.setProperty(prop, value), |
| 56 | + ); |
| 57 | + const onSetHtmlAttribute = vi.fn((attr: string, value: string | null) => { |
| 58 | + if (value !== null) selected.setAttribute(attr, value); |
| 59 | + }); |
| 60 | + act(() => |
| 61 | + root.render( |
| 62 | + <Panel |
| 63 | + projectId="fixture" |
| 64 | + element={element} |
| 65 | + styles={{}} |
| 66 | + assets={["assets/old.png", "assets/new.png"]} |
| 67 | + onSetStyle={onSetStyle} |
| 68 | + onSetHtmlAttribute={onSetHtmlAttribute} |
| 69 | + />, |
| 70 | + ), |
| 71 | + ); |
| 72 | + const image = Array.from(host.querySelectorAll("button")).find( |
| 73 | + (button) => button.textContent === "Image", |
| 74 | + ); |
| 75 | + if (!image) throw new Error("Image control must exist"); |
| 76 | + act(() => image.click()); |
| 77 | + // The Image asset select is the one whose options include the fixture asset. |
| 78 | + const assetSelect = Array.from(host.querySelectorAll("select")).find((node) => |
| 79 | + Array.from(node.options).some((option) => option.value === "assets/new.png"), |
| 80 | + ); |
| 81 | + if (!assetSelect) throw new Error("Project asset control must exist"); |
| 82 | + function choose(value: string) { |
| 83 | + const control = assetSelect; |
| 84 | + if (!control) throw new Error("Asset control must exist"); |
| 85 | + act(() => { |
| 86 | + control.value = value; |
| 87 | + control.dispatchEvent(new Event("change", { bubbles: true })); |
| 88 | + }); |
| 89 | + } |
| 90 | + return { host, selected, root, choose, assetSelect, onSetStyle, onSetHtmlAttribute }; |
| 91 | +} |
| 92 | + |
| 93 | +describe.each([ |
| 94 | + ["classic", StyleSections], |
| 95 | + ["flat", FlatStyleSection], |
| 96 | +] as const)("%s Image fill", (_name, Panel) => { |
| 97 | + it("shows the img source as the current project asset", () => { |
| 98 | + const view = mount(Panel); |
| 99 | + expect(view.assetSelect.value).toBe("assets/old.png"); |
| 100 | + act(() => view.root.unmount()); |
| 101 | + }); |
| 102 | + it.each([ |
| 103 | + "assets/100%.png", |
| 104 | + 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100%"></svg>', |
| 105 | + ])("shows an authored source containing a literal percent: %s", (src) => { |
| 106 | + const view = mount(Panel, "img", "index.html", src); |
| 107 | + const input = Array.from(view.host.querySelectorAll("label")) |
| 108 | + .find((node) => node.querySelector("span")?.textContent === "External URL") |
| 109 | + ?.querySelector("input"); |
| 110 | + expect(input?.value).toBe(src); |
| 111 | + act(() => view.root.unmount()); |
| 112 | + }); |
| 113 | + it("replaces the img source rather than writing a hidden background", () => { |
| 114 | + const view = mount(Panel); |
| 115 | + view.choose("assets/new.png"); |
| 116 | + expect(view.selected.getAttribute("src")).toBe("assets/new.png"); |
| 117 | + expect(view.onSetHtmlAttribute).toHaveBeenCalledExactlyOnceWith("src", "assets/new.png"); |
| 118 | + expect(view.onSetStyle).not.toHaveBeenCalled(); |
| 119 | + act(() => view.root.unmount()); |
| 120 | + }); |
| 121 | + it("replaces an img with an external URL without CSS wrapping", () => { |
| 122 | + const view = mount(Panel); |
| 123 | + const row = Array.from(view.host.querySelectorAll("label")).find( |
| 124 | + (node) => |
| 125 | + node.querySelector("span")?.textContent === "External URL" && node.querySelector("input"), |
| 126 | + ); |
| 127 | + const input = row?.querySelector("input"); |
| 128 | + if (!input) throw new Error("External URL input must exist"); |
| 129 | + act(() => { |
| 130 | + Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set?.call( |
| 131 | + input, |
| 132 | + "https://example.com/new.png", |
| 133 | + ); |
| 134 | + input.dispatchEvent(new Event("input", { bubbles: true })); |
| 135 | + }); |
| 136 | + act(() => input.dispatchEvent(new FocusEvent("focusout", { bubbles: true }))); |
| 137 | + expect(view.selected.getAttribute("src")).toBe("https://example.com/new.png"); |
| 138 | + expect(view.onSetStyle).not.toHaveBeenCalled(); |
| 139 | + act(() => view.root.unmount()); |
| 140 | + }); |
| 141 | + it("keeps the img source when cleared", () => { |
| 142 | + const view = mount(Panel); |
| 143 | + view.choose(""); |
| 144 | + expect(view.selected.getAttribute("src")).toBe("assets/old.png"); |
| 145 | + expect(view.onSetHtmlAttribute).not.toHaveBeenCalled(); |
| 146 | + expect(view.onSetStyle).not.toHaveBeenCalled(); |
| 147 | + act(() => view.root.unmount()); |
| 148 | + }); |
| 149 | + it("uses source-relative image paths in nested compositions", () => { |
| 150 | + const view = mount(Panel, "img", "compositions/scene.html"); |
| 151 | + view.choose("assets/new.png"); |
| 152 | + expect(view.selected.getAttribute("src")).toBe("../assets/new.png"); |
| 153 | + act(() => view.root.unmount()); |
| 154 | + }); |
| 155 | + it("keeps background-image replacement and clearing for ordinary elements", () => { |
| 156 | + const view = mount(Panel, "div"); |
| 157 | + view.choose("assets/new.png"); |
| 158 | + expect(view.selected.style.backgroundImage).toBe('url("assets/new.png")'); |
| 159 | + view.choose(""); |
| 160 | + expect(view.selected.style.backgroundImage).toBe("none"); |
| 161 | + expect(view.onSetHtmlAttribute).not.toHaveBeenCalled(); |
| 162 | + act(() => view.root.unmount()); |
| 163 | + }); |
| 164 | +}); |
0 commit comments