|
| 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 | + * The stacked resizable divider — `custom/resizable.tsx`. |
| 11 | + * |
| 12 | + * `ui/resizable.tsx` is the react-resizable-panels **v3** Shadcn file: its |
| 13 | + * whole stacked-group appearance hangs off `data-[panel-group-direction=vertical]`, |
| 14 | + * an attribute v4 (the installed major) never emits. So in a stacked group the |
| 15 | + * divider kept the base `w-px` and came out a 1px-wide, 16px-tall sliver — its |
| 16 | + * only height being the grip — instead of a full-width hairline, with a 4x16px |
| 17 | + * drag target pinned to the group's left edge and an unrotated grip. |
| 18 | + * `custom/resizable.tsx` re-keys those styles onto `aria-orientation`, which v4 |
| 19 | + * does emit on the separator. |
| 20 | + * |
| 21 | + * These tests pin the *class contract* and the attribute it depends on; happy-dom |
| 22 | + * has no Tailwind, so resolved geometry (478x1px rule, 478x4px hit strip, grip at |
| 23 | + * `rotate: 90deg`, and a real off-grip drag moving the split 50 → 75.21) is |
| 24 | + * verified in a browser instead — see the PR. |
| 25 | + * |
| 26 | + * The load-bearing case is the one that reads backwards: `aria-orientation` |
| 27 | + * describes the SEPARATOR, so a `orientation="vertical"` group (stacked panels) |
| 28 | + * has a `aria-orientation="horizontal"` divider. Keying the stacked styles on |
| 29 | + * `aria-[orientation=vertical]` would be the easy, silent way to reintroduce |
| 30 | + * exactly this bug — hence the first test. |
| 31 | + */ |
| 32 | + |
| 33 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 34 | +import { render, cleanup } from '@testing-library/react'; |
| 35 | + |
| 36 | +import { |
| 37 | + ResizableHandle, |
| 38 | + ResizablePanel, |
| 39 | + ResizablePanelGroup, |
| 40 | +} from '../custom/resizable'; |
| 41 | + |
| 42 | +afterEach(cleanup); |
| 43 | + |
| 44 | +function renderGroup(orientation: 'horizontal' | 'vertical') { |
| 45 | + const { container } = render( |
| 46 | + <ResizablePanelGroup orientation={orientation}> |
| 47 | + <ResizablePanel defaultSize={50}>one</ResizablePanel> |
| 48 | + <ResizableHandle withHandle /> |
| 49 | + <ResizablePanel defaultSize={50}>two</ResizablePanel> |
| 50 | + </ResizablePanelGroup>, |
| 51 | + ); |
| 52 | + const separator = container.querySelector<HTMLElement>('[role="separator"]'); |
| 53 | + if (!separator) throw new Error('no separator rendered'); |
| 54 | + return { container, separator }; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Every `aria-[name=value]:`-prefixed utility on the element, as the |
| 59 | + * [attribute, value] pair the variant actually selects on — Tailwind's `aria-[…]` |
| 60 | + * variant prepends `aria-` to the bracketed name, so `aria-[orientation=horizontal]:` |
| 61 | + * compiles to `&[aria-orientation="horizontal"]`. |
| 62 | + */ |
| 63 | +function ariaVariantsOn(el: HTMLElement): Array<[string, string]> { |
| 64 | + return [...el.classList].flatMap((cls) => { |
| 65 | + const m = /(?:^|:)aria-\[([a-z-]+)=([^\]]+)\]:/.exec(cls); |
| 66 | + return m ? [[`aria-${m[1]}`, m[2]] as [string, string]] : []; |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +describe('custom/resizable — the stacked divider (react-resizable-panels v4)', () => { |
| 71 | + it('gives the stacked divider aria-orientation=horizontal, not vertical', () => { |
| 72 | + // The inversion that makes this bug easy to reintroduce: the group stacks |
| 73 | + // vertically, so the rule between the panels runs horizontally. |
| 74 | + expect(renderGroup('vertical').separator.getAttribute('aria-orientation')).toBe('horizontal'); |
| 75 | + expect(renderGroup('horizontal').separator.getAttribute('aria-orientation')).toBe('vertical'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('keys the stacked divider on that attribute, so a hairline replaces the sliver', () => { |
| 79 | + const { separator } = renderGroup('vertical'); |
| 80 | + const classes = [...separator.classList]; |
| 81 | + |
| 82 | + // The rule: full-width, 1px tall — overriding the base `w-px` that made it a |
| 83 | + // vertical sliver in a stacked group. |
| 84 | + expect(classes).toContain('aria-[orientation=horizontal]:h-px'); |
| 85 | + expect(classes).toContain('aria-[orientation=horizontal]:w-full'); |
| 86 | + // The `::after` drag target: a strip ALONG the rule, not across it. |
| 87 | + expect(classes).toContain('aria-[orientation=horizontal]:after:h-1'); |
| 88 | + expect(classes).toContain('aria-[orientation=horizontal]:after:w-full'); |
| 89 | + expect(classes).toContain('aria-[orientation=horizontal]:after:left-0'); |
| 90 | + expect(classes).toContain('aria-[orientation=horizontal]:after:translate-x-0'); |
| 91 | + expect(classes).toContain('aria-[orientation=horizontal]:after:-translate-y-1/2'); |
| 92 | + // The grip crosses the rule only when rotated. |
| 93 | + expect(classes).toContain('[&[aria-orientation=horizontal]>div]:rotate-90'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('leaves the side-by-side divider on the base classes', () => { |
| 97 | + const { separator } = renderGroup('horizontal'); |
| 98 | + // Same class string either way — nothing above matches, so the base |
| 99 | + // `w-px` / `after:left-1/2` / `after:-translate-x-1/2` styling stands and |
| 100 | + // the grip is not rotated. This is the case that already worked; it must |
| 101 | + // keep working. |
| 102 | + expect(separator.getAttribute('aria-orientation')).toBe('vertical'); |
| 103 | + for (const [attr, value] of ariaVariantsOn(separator)) { |
| 104 | + expect(separator.matches(`[${attr}="${value}"]`)).toBe(false); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + it('only keys on attributes the installed library actually emits', () => { |
| 109 | + // The guard that would have caught the original bug, and catches the next |
| 110 | + // rename: every `aria-[…]` variant the wrapper adds must match the real DOM |
| 111 | + // in the orientation it targets. A variant naming an attribute/value the |
| 112 | + // library never writes is a dead selector — which is exactly what |
| 113 | + // `data-[panel-group-direction=vertical]` became when v4 landed. |
| 114 | + const { separator } = renderGroup('vertical'); |
| 115 | + const variants = ariaVariantsOn(separator); |
| 116 | + |
| 117 | + expect(variants.length).toBeGreaterThan(0); |
| 118 | + for (const [attr, value] of variants) { |
| 119 | + expect( |
| 120 | + separator.matches(`[${attr}="${value}"]`), |
| 121 | + `dead selector: no [${attr}="${value}"] on the rendered separator`, |
| 122 | + ).toBe(true); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + it('documents why the wrapper exists: v4 emits no panel-group-direction', () => { |
| 127 | + // If this ever fails, the library started emitting the v3 attribute again |
| 128 | + // and `ui/resizable.tsx`'s own variants would carry the stacked case — at |
| 129 | + // which point this wrapper is redundant rather than load-bearing. |
| 130 | + const { container } = renderGroup('vertical'); |
| 131 | + expect(container.querySelectorAll('[data-panel-group-direction]')).toHaveLength(0); |
| 132 | + }); |
| 133 | +}); |
0 commit comments