|
| 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#11775 — the record drawer is a right-hand panel on a desktop and a |
| 11 | + * bottom sheet on a phone. |
| 12 | + * |
| 13 | + * `renderDrawerForm` used to open one vaul `Drawer` with `direction="right"` |
| 14 | + * on every viewport. vaul honours `direction` for its slide and drag gesture |
| 15 | + * only: the shipped `DrawerContent` is the upstream bottom sheet and carries |
| 16 | + * the bottom-sheet classes whatever the direction. On a 1440×900 desktop the |
| 17 | + * create form therefore drew as a 672×526 bottom sheet pinned to the |
| 18 | + * bottom-left by `sm:max-w-2xl`, with `data-vaul-drawer-direction="right"` on |
| 19 | + * it (the card's browser reading). |
| 20 | + * |
| 21 | + * The desktop panel is now the right-hand `Sheet`; the phone keeps vaul's |
| 22 | + * bottom sheet, opened in vaul's default direction. |
| 23 | + * |
| 24 | + * jsdom-class environments do not lay out, so these pins read the classes and |
| 25 | + * data attributes that decide the geometry, never a measured box: |
| 26 | + * |
| 27 | + * - THE FIX: on a desktop, create, edit and view draw the form on a |
| 28 | + * `[role="dialog"]` that is right-anchored and full height |
| 29 | + * (`inset-y-0 right-0 h-full`), is not a vaul drawer, and carries none of |
| 30 | + * the bottom sheet's classes. |
| 31 | + * - CONTROL: on a phone the same verbs draw it on vaul's bottom sheet, in the |
| 32 | + * `bottom` direction, spanning the viewport (no `sm:max-w-2xl` cap). |
| 33 | + * - CONTROL: the stock `DrawerContent` every other bottom-sheet user renders |
| 34 | + * (`useActionModal`'s `bottom` placement, the SDUI `drawer` node) still |
| 35 | + * draws the bottom sheet: the fix did not come from changing the primitive. |
| 36 | + * |
| 37 | + * `ObjectGrid` is a sink that keeps the handlers the view hands it, and |
| 38 | + * `ObjectForm` a sink that shows the mode it was built for. |
| 39 | + */ |
| 40 | + |
| 41 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 42 | +import { render, screen, waitFor, cleanup, act, fireEvent, within } from '@testing-library/react'; |
| 43 | +import type { DataSource, ObjectViewSchema } from '@object-ui/types'; |
| 44 | +import { Drawer, DrawerContent, DrawerTitle } from '@object-ui/components'; |
| 45 | +import { ObjectView } from '../ObjectView'; |
| 46 | + |
| 47 | +type GridHandlers = { onRowClick: (record: Record<string, unknown>) => void; onEdit?: (record: Record<string, unknown>) => void }; |
| 48 | +const gridProps: GridHandlers[] = []; |
| 49 | + |
| 50 | +vi.mock('@object-ui/plugin-grid', async (importOriginal) => ({ |
| 51 | + ...(await importOriginal<Record<string, unknown>>()), |
| 52 | + ObjectGrid: ({ onRowClick, onEdit }: GridHandlers) => { |
| 53 | + gridProps.push({ onRowClick, onEdit }); |
| 54 | + return <div data-testid="object-grid" />; |
| 55 | + }, |
| 56 | +})); |
| 57 | +vi.mock('@object-ui/plugin-form', async (importOriginal) => ({ |
| 58 | + ...(await importOriginal<Record<string, unknown>>()), |
| 59 | + ObjectForm: ({ schema }: { schema: { mode?: string } }) => <div data-testid="object-form" data-mode={schema.mode} />, |
| 60 | +})); |
| 61 | + |
| 62 | +/** The card's viewport, and a phone below `useIsMobile`'s 768px breakpoint. */ |
| 63 | +const DESKTOP = 1440; |
| 64 | +const MOBILE = 390; |
| 65 | +const ORIGINAL_WIDTH = window.innerWidth; |
| 66 | + |
| 67 | +function setViewport(width: number) { |
| 68 | + Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: width }); |
| 69 | +} |
| 70 | + |
| 71 | +/** The upstream bottom sheet's geometry classes, and the right panel's. */ |
| 72 | +const BOTTOM_SHEET = ['inset-x-0', 'bottom-0', 'rounded-t-[10px]']; |
| 73 | +const RIGHT_PANEL = ['inset-y-0', 'right-0', 'h-full']; |
| 74 | + |
| 75 | +const ROW = { id: 'r1', name: 'Alpha' }; |
| 76 | + |
| 77 | +function makeDataSource() { |
| 78 | + const state = { settled: false }; |
| 79 | + const ds = { |
| 80 | + find: vi.fn().mockResolvedValue({ data: [], total: 0 }), |
| 81 | + findOne: vi.fn(), |
| 82 | + create: vi.fn(), |
| 83 | + update: vi.fn(), |
| 84 | + delete: vi.fn(), |
| 85 | + getObjectSchema: vi.fn(async () => { |
| 86 | + state.settled = true; |
| 87 | + return { name: 'task', label: 'Tasks', fields: { name: { type: 'text' }, status: { type: 'text' } } }; |
| 88 | + }), |
| 89 | + }; |
| 90 | + return { ds: ds as unknown as DataSource, state }; |
| 91 | +} |
| 92 | + |
| 93 | +/** An object view whose author chose the drawer, so both viewports open it. */ |
| 94 | +async function mount() { |
| 95 | + const { ds, state } = makeDataSource(); |
| 96 | + const schema = { type: 'object-view', objectName: 'task', layout: 'drawer' } as unknown as ObjectViewSchema; |
| 97 | + render(<ObjectView schema={schema} dataSource={ds} />); |
| 98 | + await waitFor(() => expect(gridProps.length).toBeGreaterThan(0)); |
| 99 | + await waitFor(() => expect(state.settled, 'the object schema was never read').toBe(true)); |
| 100 | + await act(async () => {}); |
| 101 | +} |
| 102 | + |
| 103 | +type Verb = 'create' | 'edit' | 'view'; |
| 104 | +const VERBS: Verb[] = ['create', 'edit', 'view']; |
| 105 | +const TITLES: Record<Verb, string> = { create: 'Create Tasks', edit: 'Edit Tasks', view: 'View Tasks' }; |
| 106 | + |
| 107 | +function run(verb: Verb) { |
| 108 | + const grid = gridProps[gridProps.length - 1]; |
| 109 | + if (verb === 'create') { |
| 110 | + fireEvent.click(screen.getByRole('button', { name: 'New' })); |
| 111 | + } else if (verb === 'view') { |
| 112 | + act(() => grid.onRowClick(ROW)); |
| 113 | + } else { |
| 114 | + expect(grid.onEdit, 'the grid was handed no `onEdit`').toBeTypeOf('function'); |
| 115 | + act(() => grid.onEdit!(ROW)); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/** The dialog surface the one form in the tree, built for `verb`, is drawn on. */ |
| 120 | +async function surfaceOf(verb: Verb): Promise<HTMLElement> { |
| 121 | + const form = await screen.findByTestId('object-form'); |
| 122 | + expect(form.getAttribute('data-mode')).toBe(verb); |
| 123 | + const surface = form.closest('[role="dialog"]') as HTMLElement | null; |
| 124 | + expect(surface, `the ${verb} form is not inside a dialog surface`).not.toBeNull(); |
| 125 | + expect(within(surface!).getByText(TITLES[verb])).toBeTruthy(); |
| 126 | + return surface!; |
| 127 | +} |
| 128 | + |
| 129 | +const classesOf = (el: Element) => el.getAttribute('class')?.split(/\s+/).filter(Boolean) ?? []; |
| 130 | + |
| 131 | +beforeEach(() => { |
| 132 | + cleanup(); |
| 133 | + gridProps.length = 0; |
| 134 | +}); |
| 135 | +afterEach(() => { |
| 136 | + cleanup(); |
| 137 | + setViewport(ORIGINAL_WIDTH); |
| 138 | +}); |
| 139 | + |
| 140 | +describe('objectui#11775 — the record drawer is a right panel on a desktop, a bottom sheet on a phone', () => { |
| 141 | + for (const verb of VERBS) { |
| 142 | + it(`THE FIX: on a desktop, ${verb} draws the form in a right-anchored, full-height panel`, async () => { |
| 143 | + setViewport(DESKTOP); |
| 144 | + await mount(); |
| 145 | + run(verb); |
| 146 | + const surface = await surfaceOf(verb); |
| 147 | + const classes = classesOf(surface); |
| 148 | + |
| 149 | + expect( |
| 150 | + surface.closest('[data-vaul-drawer]'), |
| 151 | + 'the desktop form is still on vaul\'s drawer, whose content is styled as a bottom sheet', |
| 152 | + ).toBeNull(); |
| 153 | + expect(classes, 'the panel is not right-anchored and full height').toEqual(expect.arrayContaining(RIGHT_PANEL)); |
| 154 | + for (const bottom of BOTTOM_SHEET) { |
| 155 | + expect(classes, `the desktop panel still carries the bottom sheet's \`${bottom}\``).not.toContain(bottom); |
| 156 | + } |
| 157 | + // The caller's width wins over the Sheet's default `w-3/4 sm:max-w-sm`. |
| 158 | + expect(classes).toEqual(expect.arrayContaining(['w-full', 'sm:max-w-2xl'])); |
| 159 | + expect(classes).not.toContain('sm:max-w-sm'); |
| 160 | + }); |
| 161 | + |
| 162 | + it(`CONTROL: on a phone, ${verb} draws the form on vaul's bottom sheet, across the viewport`, async () => { |
| 163 | + setViewport(MOBILE); |
| 164 | + await mount(); |
| 165 | + run(verb); |
| 166 | + const surface = await surfaceOf(verb); |
| 167 | + const classes = classesOf(surface); |
| 168 | + |
| 169 | + expect(surface.hasAttribute('data-vaul-drawer'), 'the phone form is not on vaul\'s drawer').toBe(true); |
| 170 | + expect( |
| 171 | + surface.getAttribute('data-vaul-drawer-direction'), |
| 172 | + 'the bottom sheet slides and drags in a direction other than the one it is drawn in', |
| 173 | + ).toBe('bottom'); |
| 174 | + expect(classes).toEqual(expect.arrayContaining(BOTTOM_SHEET)); |
| 175 | + expect(classes).not.toContain('right-0'); |
| 176 | + expect(classes, 'the phone bottom sheet is capped, so it pins to the left between 640px and 768px').not.toContain('sm:max-w-2xl'); |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + it('CONTROL: the stock `DrawerContent` other bottom-sheet users render still draws the bottom sheet', () => { |
| 181 | + render( |
| 182 | + <Drawer open> |
| 183 | + <DrawerContent> |
| 184 | + <DrawerTitle>Stock bottom sheet</DrawerTitle> |
| 185 | + </DrawerContent> |
| 186 | + </Drawer>, |
| 187 | + ); |
| 188 | + const surface = screen.getByText('Stock bottom sheet').closest('[data-vaul-drawer]') as HTMLElement | null; |
| 189 | + expect(surface).not.toBeNull(); |
| 190 | + expect(surface!.getAttribute('data-vaul-drawer-direction')).toBe('bottom'); |
| 191 | + expect(classesOf(surface!)).toEqual(expect.arrayContaining(BOTTOM_SHEET)); |
| 192 | + }); |
| 193 | +}); |
0 commit comments