|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectui#11795 — the flow canvas re-fits when its own width changes, and |
| 5 | + * nothing an author does to the diagram moves the viewport. |
| 6 | + * |
| 7 | + * Measured in Chromium before the fix: Studio's Automations pillar opened at a |
| 8 | + * 390px window framed the diagram for a ~34px canvas (centred, so it hung off |
| 9 | + * both edges); widened to 1024px, the canvas grew to ~460px and the diagram |
| 10 | + * stayed where the narrow box had put it, its Start/End cards clipped at the |
| 11 | + * left edge. The mount framing ran once and nothing re-ran it. |
| 12 | + * |
| 13 | + * The canvas is the real one. happy-dom lays nothing out, so the viewport's |
| 14 | + * `clientWidth` / `clientHeight` are read from a box this file sets, and |
| 15 | + * `ResizeObserver` is a double whose notifications the tests deliver, the way |
| 16 | + * a browser delivers them: once after `observe`, then on each resize. |
| 17 | + */ |
| 18 | + |
| 19 | +import * as React from 'react'; |
| 20 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 21 | +import { render, screen, cleanup, act } from '@testing-library/react'; |
| 22 | +import { FlowCanvas } from './FlowCanvas'; |
| 23 | +import { NODE_W, PADDING, type FlowDesignerEdge, type FlowDesignerNode } from './flow-canvas-layout'; |
| 24 | + |
| 25 | +// The palette's engine overlay answers "engine absent" (its offline answer). |
| 26 | +vi.stubGlobal( |
| 27 | + 'fetch', |
| 28 | + vi.fn(async () => new Response('not found', { status: 404 })), |
| 29 | +); |
| 30 | + |
| 31 | +/** The diagram's width at 100% for a linear flow: one node column plus the layout's padding. */ |
| 32 | +const DIAGRAM_W = NODE_W + 2 * PADDING; |
| 33 | + |
| 34 | +const box = { w: 0, h: 0 }; |
| 35 | +const observers = new Set<{ cb: ResizeObserverCallback; self: ResizeObserver }>(); |
| 36 | +class ResizeObserverDouble { |
| 37 | + private entry: { cb: ResizeObserverCallback; self: ResizeObserver }; |
| 38 | + constructor(cb: ResizeObserverCallback) { |
| 39 | + this.entry = { cb, self: this as unknown as ResizeObserver }; |
| 40 | + } |
| 41 | + observe() { |
| 42 | + observers.add(this.entry); |
| 43 | + } |
| 44 | + unobserve() { |
| 45 | + observers.delete(this.entry); |
| 46 | + } |
| 47 | + disconnect() { |
| 48 | + observers.delete(this.entry); |
| 49 | + } |
| 50 | +} |
| 51 | +/** Deliver a notification to every live observer, as a browser does after a resize. */ |
| 52 | +function notify() { |
| 53 | + act(() => { |
| 54 | + for (const o of [...observers]) o.cb([], o.self); |
| 55 | + }); |
| 56 | +} |
| 57 | +function resizeTo(w: number, h: number) { |
| 58 | + box.w = w; |
| 59 | + box.h = h; |
| 60 | + notify(); |
| 61 | +} |
| 62 | + |
| 63 | +/** The prototype that owns `name` on the viewport element. */ |
| 64 | +function ownerOf(name: 'clientWidth' | 'clientHeight'): object { |
| 65 | + let p: object | null = HTMLDivElement.prototype; |
| 66 | + while (p && !Object.getOwnPropertyDescriptor(p, name)) p = Object.getPrototypeOf(p); |
| 67 | + if (!p) throw new Error(`no prototype defines ${name}`); |
| 68 | + return p; |
| 69 | +} |
| 70 | + |
| 71 | +let realResizeObserver: typeof ResizeObserver | undefined; |
| 72 | +beforeEach(() => { |
| 73 | + box.w = 0; |
| 74 | + box.h = 0; |
| 75 | + observers.clear(); |
| 76 | + realResizeObserver = globalThis.ResizeObserver; |
| 77 | + (globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = ResizeObserverDouble; |
| 78 | + const isViewport = (el: Element) => el.getAttribute('role') === 'application'; |
| 79 | + vi.spyOn(ownerOf('clientWidth') as HTMLElement, 'clientWidth', 'get').mockImplementation(function (this: Element) { |
| 80 | + return isViewport(this) ? box.w : 0; |
| 81 | + }); |
| 82 | + vi.spyOn(ownerOf('clientHeight') as HTMLElement, 'clientHeight', 'get').mockImplementation(function (this: Element) { |
| 83 | + return isViewport(this) ? box.h : 0; |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +afterEach(() => { |
| 88 | + cleanup(); |
| 89 | + vi.restoreAllMocks(); |
| 90 | + (globalThis as unknown as { ResizeObserver: unknown }).ResizeObserver = realResizeObserver; |
| 91 | + localStorage.clear(); |
| 92 | +}); |
| 93 | + |
| 94 | +type Draft = { nodes: FlowDesignerNode[]; edges: FlowDesignerEdge[] }; |
| 95 | + |
| 96 | +const flow = (): Draft => ({ |
| 97 | + nodes: [ |
| 98 | + { id: 'start', type: 'start', label: 'Start' }, |
| 99 | + { id: 'assign', type: 'assignment', label: 'Set owner' }, |
| 100 | + { id: 'end', type: 'end', label: 'End' }, |
| 101 | + ], |
| 102 | + edges: [ |
| 103 | + { id: 'e1', source: 'start', target: 'assign' }, |
| 104 | + { id: 'e2', source: 'assign', target: 'end' }, |
| 105 | + ], |
| 106 | +}); |
| 107 | + |
| 108 | +/** Mount the canvas in a host that keeps the draft, as Studio does; `edit` changes it from outside. */ |
| 109 | +function mountAt(w: number, h: number) { |
| 110 | + box.w = w; |
| 111 | + box.h = h; |
| 112 | + const host: { edit: (fn: (d: Draft) => Draft) => void } = { edit: () => {} }; |
| 113 | + function Host() { |
| 114 | + const [draft, setDraft] = React.useState<Draft>(flow); |
| 115 | + host.edit = (fn) => act(() => setDraft(fn)); |
| 116 | + return ( |
| 117 | + <FlowCanvas |
| 118 | + nodes={draft.nodes} |
| 119 | + edges={draft.edges} |
| 120 | + editable |
| 121 | + designMode |
| 122 | + selectedId={null} |
| 123 | + onSelect={() => {}} |
| 124 | + onPatch={(patch) => setDraft((d) => ({ ...d, ...(patch as Partial<Draft>) }))} |
| 125 | + /> |
| 126 | + ); |
| 127 | + } |
| 128 | + render(<Host />); |
| 129 | + // The browser's first notification after `observe`, at the width just framed. |
| 130 | + notify(); |
| 131 | + return host; |
| 132 | +} |
| 133 | + |
| 134 | +/** The pan and zoom the canvas applied to the diagram, read off its transform. */ |
| 135 | +function view(): { x: number; y: number; zoom: number } { |
| 136 | + const layer = screen.getByRole('application').firstElementChild as HTMLElement; |
| 137 | + const m = /translate\((-?[\d.]+)px, (-?[\d.]+)px\) scale\(([\d.]+)\)/.exec(layer.style.transform); |
| 138 | + expect(m, `diagram transform: ${layer.style.transform}`).not.toBeNull(); |
| 139 | + return { x: Number(m![1]), y: Number(m![2]), zoom: Number(m![3]) }; |
| 140 | +} |
| 141 | + |
| 142 | +describe('FlowCanvas — a width change re-fits the diagram (objectui#11795)', () => { |
| 143 | + it('opened narrow and then widened, the diagram is framed for the new width, not left where the narrow box put it', () => { |
| 144 | + mountAt(34, 541); |
| 145 | + // The mount framing at 34px: centred at 100%, so the diagram's left edge is off-canvas. |
| 146 | + expect(view().x).toBeLessThan(0); |
| 147 | + |
| 148 | + resizeTo(460, 570); |
| 149 | + |
| 150 | + const v = view(); |
| 151 | + expect(v.zoom).toBe(1); |
| 152 | + expect(v.x).toBe((460 - DIAGRAM_W) / 2); |
| 153 | + expect(v.x).toBeGreaterThanOrEqual(0); |
| 154 | + expect(v.x + DIAGRAM_W * v.zoom).toBeLessThanOrEqual(460); |
| 155 | + }); |
| 156 | + |
| 157 | + it('narrowed below the diagram, it zooms out until the whole flow fits, centred', () => { |
| 158 | + mountAt(800, 600); |
| 159 | + expect(view().zoom).toBe(1); |
| 160 | + |
| 161 | + resizeTo(200, 600); |
| 162 | + |
| 163 | + const v = view(); |
| 164 | + expect(v.zoom).toBeLessThan(1); |
| 165 | + expect(v.x).toBeGreaterThanOrEqual(0); |
| 166 | + expect(v.x + DIAGRAM_W * v.zoom).toBeLessThanOrEqual(200); |
| 167 | + expect(screen.getByText(`${Math.round(v.zoom * 100)}%`)).toBeTruthy(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('a wider canvas does not zoom a small flow past the 100% it opens at', () => { |
| 171 | + mountAt(460, 570); |
| 172 | + |
| 173 | + resizeTo(1400, 900); |
| 174 | + |
| 175 | + expect(view()).toMatchObject({ zoom: 1, x: (1400 - DIAGRAM_W) / 2 }); |
| 176 | + }); |
| 177 | +}); |
| 178 | + |
| 179 | +describe('FlowCanvas — what an author does to the diagram never moves the viewport (objectui#11795, the control)', () => { |
| 180 | + it('a node add re-arms the observer at the width already framed, and the view stays put', () => { |
| 181 | + const host = mountAt(800, 600); |
| 182 | + const before = view(); |
| 183 | + |
| 184 | + host.edit((d) => ({ |
| 185 | + nodes: [...d.nodes, { id: 'extra', type: 'assignment', label: 'Extra' }], |
| 186 | + edges: [...d.edges, { id: 'e3', source: 'end', target: 'extra' }], |
| 187 | + })); |
| 188 | + // The re-armed observer's first notification arrives at the same width. |
| 189 | + notify(); |
| 190 | + |
| 191 | + expect(document.querySelector('[data-node-id="extra"]')).not.toBeNull(); |
| 192 | + expect(view()).toEqual(before); |
| 193 | + }); |
| 194 | + |
| 195 | + it('a height-only change — a notice landing above the canvas after an edit — does not re-fit', () => { |
| 196 | + mountAt(800, 600); |
| 197 | + const before = view(); |
| 198 | + |
| 199 | + resizeTo(800, 560); |
| 200 | + |
| 201 | + expect(view()).toEqual(before); |
| 202 | + }); |
| 203 | + |
| 204 | + it('a hidden canvas (0 wide) is not fitted into nothing', () => { |
| 205 | + mountAt(800, 600); |
| 206 | + const before = view(); |
| 207 | + |
| 208 | + resizeTo(0, 0); |
| 209 | + |
| 210 | + expect(view()).toEqual(before); |
| 211 | + }); |
| 212 | +}); |
0 commit comments