|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectui#8797 — every row on the swimlane axis has the SAME scroll RANGE. |
| 11 | + * |
| 12 | + * ## What broke |
| 13 | + * |
| 14 | + * objectui#8448 gave the board one horizontal axis: a scroll handler copies |
| 15 | + * `scrollLeft` from whichever row the user drove onto every other row. Equal |
| 16 | + * `scrollLeft` is equal ALIGNMENT only while the rows have the same scrollable |
| 17 | + * RANGE, and they did not. The lane content rows carried `px-2`; the header row |
| 18 | + * did not. `scrollLeft`'s maximum is `scrollWidth - clientWidth`, so the lane |
| 19 | + * rows' maximum was larger. Driven to a lane's maximum, the header row CLAMPED |
| 20 | + * at its own smaller one and every column title sat off its column — and stayed |
| 21 | + * there, because nothing moves again until the user scrolls back. |
| 22 | + * |
| 23 | + * Measured in Chromium 1194 at 1600x1000, five columns and six swimlanes, real |
| 24 | + * Tailwind CSS, the axis driven through the real input pipeline (`mouse.wheel`, |
| 25 | + * not an assignment — see the warning below): |
| 26 | + * |
| 27 | + * before header max scrollLeft 288 | lane max 298 | worst title/cell dx 9px |
| 28 | + * after header max scrollLeft 296 | lane max 298 | worst title/cell dx 1px |
| 29 | + * |
| 30 | + * 1px is the baseline objectui#7303 and objectui#8448 already record: the lane |
| 31 | + * content row sits inside the lane's `border rounded-lg` wrapper, so it starts |
| 32 | + * 1px right of the header row. That same border is why the two maxima are still |
| 33 | + * 2px apart after the fix — `clientWidth` 1550 against 1552. That 2px is NOT |
| 34 | + * padding and is not closable from the class lists this file guards; it is |
| 35 | + * reported on objectui#8797 rather than papered over with a magic offset. |
| 36 | + * |
| 37 | + * ## ⚠️ What this file can and cannot measure — read before extending it |
| 38 | + * |
| 39 | + * Vitest runs here in happy-dom, which performs NO layout and applies NO CSS. |
| 40 | + * `scrollWidth`, `clientWidth` and every `scrollLeft` MAXIMUM are therefore |
| 41 | + * indistinguishable between the broken build and the fixed one — the numbers |
| 42 | + * above cannot be reproduced here, and asserting them would be a pin that |
| 43 | + * cannot fail. That half is out-of-band browser work and is reported in the PR, |
| 44 | + * exactly as objectui#7303, objectui#8448 and objectui#8449 all document for |
| 45 | + * this surface. |
| 46 | + * |
| 47 | + * What IS mechanically true here is the CLASS CONTRACT that decides the range. |
| 48 | + * This file pins it as an INVARIANT rather than a spelling: it never asserts |
| 49 | + * that a row carries `px-2`, only that every row on the axis carries the SAME |
| 50 | + * horizontal padding as every other. A future re-spelling of the padding stays |
| 51 | + * green; a future DIVERGENCE between the two kinds of row goes red, which is |
| 52 | + * the whole defect class. |
| 53 | + * |
| 54 | + * ## ⚠️ Why this file does NOT re-assert propagation |
| 55 | + * |
| 56 | + * objectui#8448's pin drives a `fireEvent.scroll` and asserts the rows end up |
| 57 | + * with equal `scrollLeft`. That is exactly the assertion that could not see |
| 58 | + * this bug: a clamp only happens PAST the header row's own maximum, and |
| 59 | + * happy-dom has no maxima, so propagation is satisfied at every value. Pinning |
| 60 | + * propagation again here would reproduce the blind spot that let objectui#8797 |
| 61 | + * through. Propagation stays objectui#8448's; range is this file's. |
| 62 | + */ |
| 63 | + |
| 64 | +import React from 'react'; |
| 65 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 66 | +import { render, waitFor, cleanup, within } from '@testing-library/react'; |
| 67 | +import { SchemaRenderer, SchemaRendererProvider } from '@object-ui/react'; |
| 68 | +// Registers `object-kanban`. Module scope, not a hook: the import IS the |
| 69 | +// registration (AGENTS.md's test-discipline section). |
| 70 | +import '../index'; |
| 71 | +// Same specifier as `index.tsx`'s lazy factory, so ESM's module cache resolves |
| 72 | +// the chunk immediately instead of racing a `waitFor` budget (objectui#3010). |
| 73 | +import '../KanbanImpl'; |
| 74 | + |
| 75 | +afterEach(cleanup); |
| 76 | + |
| 77 | +/** The viewport the browser numbers in the docblock were read at. */ |
| 78 | +const VIEWPORT = { width: 1600, height: 1000 }; |
| 79 | + |
| 80 | +beforeEach(() => { |
| 81 | + Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: VIEWPORT.width }); |
| 82 | + Object.defineProperty(window, 'innerHeight', { configurable: true, writable: true, value: VIEWPORT.height }); |
| 83 | + // Collapsed lanes persist per swimlane field; a leaked entry would silently |
| 84 | + // change how many rows the next case renders. |
| 85 | + try { window.localStorage.clear(); } catch { /* ignore */ } |
| 86 | +}); |
| 87 | + |
| 88 | +const OBJECT = 'deal'; |
| 89 | + |
| 90 | +const DEAL_SCHEMA = { |
| 91 | + name: OBJECT, |
| 92 | + label: 'Deal', |
| 93 | + fields: { |
| 94 | + name: { type: 'text', label: 'Name' }, |
| 95 | + status: { type: 'text', label: 'Status' }, |
| 96 | + owner: { type: 'text', label: 'Owner' }, |
| 97 | + }, |
| 98 | +}; |
| 99 | + |
| 100 | +function makeAdapter(): Record<string, any> { |
| 101 | + return { |
| 102 | + find: vi.fn(async () => ({ data: [] })), |
| 103 | + findOne: vi.fn(), |
| 104 | + create: vi.fn(), |
| 105 | + update: vi.fn(), |
| 106 | + delete: vi.fn(), |
| 107 | + getObjectSchema: vi.fn(async () => DEAL_SCHEMA), |
| 108 | + }; |
| 109 | +} |
| 110 | + |
| 111 | +const ROWS = [ |
| 112 | + { id: 'a', name: 'Alpha deal', status: 'open', owner: 'ann' }, |
| 113 | + { id: 'b', name: 'Beta deal', status: 'won', owner: 'bob' }, |
| 114 | +]; |
| 115 | + |
| 116 | +const BOARD = { |
| 117 | + type: 'object-kanban', |
| 118 | + objectName: OBJECT, |
| 119 | + groupBy: 'status', |
| 120 | + columns: [ |
| 121 | + { id: 'open', title: 'Open' }, |
| 122 | + { id: 'qualified', title: 'Qualified' }, |
| 123 | + { id: 'proposal', title: 'Proposal' }, |
| 124 | + { id: 'won', title: 'Won' }, |
| 125 | + { id: 'lost', title: 'Lost' }, |
| 126 | + ], |
| 127 | +}; |
| 128 | + |
| 129 | +const SWIMLANES = { grouping: { fields: [{ field: 'owner' }] } }; |
| 130 | + |
| 131 | +/** Every row that declares itself part of the board's shared horizontal axis. */ |
| 132 | +const axisRows = (root: ParentNode): HTMLElement[] => |
| 133 | + [...root.querySelectorAll<HTMLElement>('[data-swimlane-scroll-row]')]; |
| 134 | + |
| 135 | +/** `sm:pl-44` -> `pl-44`; a bare utility is returned unchanged. */ |
| 136 | +const utility = (token: string): string => token.slice(token.lastIndexOf(':') + 1); |
| 137 | + |
| 138 | +/** |
| 139 | + * The classes that move a box's HORIZONTAL padding, and therefore its |
| 140 | + * `scrollWidth`: `p-*`, `px-*`, `pl-*`, `pr-*`, `ps-*`, `pe-*`. Deliberately |
| 141 | + * NOT `py-*` / `pt-*` / `pb-*` — the header row's `pt-3 sm:pt-4` and the lane |
| 142 | + * rows' `pb-3` are vertical, legitimately differ, and are objectui#8449's. |
| 143 | + */ |
| 144 | +const isHorizontalPadding = (token: string): boolean => /^p(?:[xlrse])?-/.test(utility(token)); |
| 145 | + |
| 146 | +/** Whatever decides the gap between columns, which also moves `scrollWidth`. */ |
| 147 | +const isGap = (token: string): boolean => /^gap(?:-x)?-/.test(utility(token)); |
| 148 | + |
| 149 | +const classesMatching = (el: HTMLElement, pred: (t: string) => boolean): string[] => |
| 150 | + el.className.split(/\s+/).filter(Boolean).filter(pred).sort(); |
| 151 | + |
| 152 | +/** |
| 153 | + * Render a swimlane board and prove it is a real one before anything is read |
| 154 | + * off it: the region, both lanes, and a card INSIDE a lane. |
| 155 | + */ |
| 156 | +async function renderSwimlaneBoard() { |
| 157 | + const { container } = render( |
| 158 | + <SchemaRendererProvider dataSource={makeAdapter() as any}> |
| 159 | + <SchemaRenderer schema={{ ...BOARD, data: ROWS, ...SWIMLANES } as never} /> |
| 160 | + </SchemaRendererProvider>, |
| 161 | + ); |
| 162 | + |
| 163 | + await waitFor(() => expect(container.textContent).toContain('Alpha deal')); |
| 164 | + const region = container.querySelector('[role="region"]') as HTMLElement; |
| 165 | + expect(region?.getAttribute('aria-label')).toBe('Kanban board with swimlanes'); |
| 166 | + |
| 167 | + const laneButtons = [...region.querySelectorAll('button[aria-expanded]')]; |
| 168 | + expect(laneButtons.map((b) => b.textContent)).toEqual(['▶ann(1)', '▶bob(1)']); |
| 169 | + |
| 170 | + const laneList = region.querySelector('[role="list"][aria-label="Open - ann cards"]'); |
| 171 | + expect( |
| 172 | + within(laneList as HTMLElement).queryByText('Alpha deal'), |
| 173 | + 'the swimlane cell should hold its card', |
| 174 | + ).not.toBeNull(); |
| 175 | + |
| 176 | + return { container, region }; |
| 177 | +} |
| 178 | + |
| 179 | +describe('objectui#8797 — every row on the swimlane axis has the same scroll range', () => { |
| 180 | + it('RANGE — the header row and every lane row carry the SAME horizontal padding', async () => { |
| 181 | + const { region } = await renderSwimlaneBoard(); |
| 182 | + |
| 183 | + const rows = axisRows(region); |
| 184 | + // CONTROL — one header row + one row per lane, header row first. |
| 185 | + expect(rows.length, 'header row + one row per lane').toBe(3); |
| 186 | + expect(rows[0]).toBe(region.firstElementChild); |
| 187 | + |
| 188 | + const [headerRow, ...laneRows] = rows; |
| 189 | + const headerPadding = classesMatching(headerRow, isHorizontalPadding); |
| 190 | + |
| 191 | + // NON-VACUITY — "the sets agree" is trivially true of two empty sets. The |
| 192 | + // rows are indented off the lane labels, so this can never legitimately be |
| 193 | + // empty; if it is, the pin below is comparing nothing. |
| 194 | + expect( |
| 195 | + headerPadding.length, |
| 196 | + 'the axis rows must carry horizontal padding at all, or this pin compares two empty sets', |
| 197 | + ).toBeGreaterThan(0); |
| 198 | + |
| 199 | + // THE INVARIANT. Not "the header row carries px-2" — that would pin a |
| 200 | + // spelling. Every row on one axis must agree with every other, whatever |
| 201 | + // the spelling, because that agreement IS the equal range. |
| 202 | + for (const [i, laneRow] of laneRows.entries()) { |
| 203 | + expect( |
| 204 | + classesMatching(laneRow, isHorizontalPadding), |
| 205 | + `lane row ${i} must carry the same horizontal padding as the column-header row, ` + |
| 206 | + 'or its scrollable range differs and the shared scrollLeft clamps on one of them (objectui#8797)', |
| 207 | + ).toEqual(headerPadding); |
| 208 | + } |
| 209 | + }); |
| 210 | + |
| 211 | + it('RANGE — the axis rows also agree on the gap, the other class-level input to scrollWidth', async () => { |
| 212 | + const { region } = await renderSwimlaneBoard(); |
| 213 | + |
| 214 | + const [headerRow, ...laneRows] = axisRows(region); |
| 215 | + const headerGap = classesMatching(headerRow, isGap); |
| 216 | + |
| 217 | + expect(headerGap.length, 'the columns are laid out with a gap utility').toBeGreaterThan(0); |
| 218 | + |
| 219 | + // Padding is what objectui#8797 actually caught, but it is not the only |
| 220 | + // class that moves `scrollWidth`. A divergent gap would reintroduce the |
| 221 | + // same clamp by a different route. (Column WIDTH is objectui#8508's, which |
| 222 | + // pins both rows onto one `columnWidthClasses` source.) |
| 223 | + for (const [i, laneRow] of laneRows.entries()) { |
| 224 | + expect( |
| 225 | + classesMatching(laneRow, isGap), |
| 226 | + `lane row ${i} must space its columns exactly as the header row does (objectui#8797)`, |
| 227 | + ).toEqual(headerGap); |
| 228 | + } |
| 229 | + }); |
| 230 | + |
| 231 | + it('NON-VACUITY — the rows are still scroll containers, so they still HAVE a range', async () => { |
| 232 | + const { region } = await renderSwimlaneBoard(); |
| 233 | + |
| 234 | + // A row that cannot scroll has no range, and two rows that cannot scroll |
| 235 | + // agree about their padding for free. objectui#8448 refused "make the |
| 236 | + // header row unscrollable" as arm C and objectui#7303's shrink arithmetic |
| 237 | + // depends on these rows being scroll containers; this re-reads that so the |
| 238 | + // equality above cannot be satisfied by deleting the axis. |
| 239 | + for (const row of axisRows(region)) { |
| 240 | + expect( |
| 241 | + row.className.split(/\s+/).some((t) => /^(?:[a-z]+:)*overflow(?:-x)?-(?:auto|scroll)$/.test(t)), |
| 242 | + `a row on the shared axis must still be a scroll container; class was ${JSON.stringify(row.className)}`, |
| 243 | + ).toBe(true); |
| 244 | + } |
| 245 | + }); |
| 246 | +}); |
0 commit comments