|
| 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 lookup popover's quick-create and "show all results" stay whole and |
| 11 | + * clickable whatever room the popover gets (objectstack#36825). |
| 12 | + * |
| 13 | + * ## The defect |
| 14 | + * |
| 15 | + * Both rows were the last children of the option list's scroll container. The |
| 16 | + * popover is held to the room Radix reports on the side it opens to, and when |
| 17 | + * that room is short of the list's natural height the list is what shrinks, so |
| 18 | + * the rows at its end were the first thing cut: measured in a dialog on a |
| 19 | + * 450px-high window, the list ended 3px into the create row. Inside a modal |
| 20 | + * form the list could not be scrolled to it either, because the popover is |
| 21 | + * portalled out of the dialog's DOM and the dialog's scroll lock cancels every |
| 22 | + * wheel and touch move outside it. |
| 23 | + * |
| 24 | + * ## What holds it now |
| 25 | + * |
| 26 | + * - the two rows sit in a footer pinned under the list, outside its scroll |
| 27 | + * container, so the list gives way and they do not; |
| 28 | + * - the popover is `modal`, its own scroll-lock layer, so its list scrolls |
| 29 | + * inside a modal form. |
| 30 | + * |
| 31 | + * jsdom and happy-dom do no layout, so these pins hold the structure that |
| 32 | + * makes the geometry true; the geometry itself was measured in a browser |
| 33 | + * (the readings are on the pull request). |
| 34 | + * |
| 35 | + * ## The rows |
| 36 | + * |
| 37 | + * - a long list (the clipped case): create and "show all" are in the footer, |
| 38 | + * not in the listbox; |
| 39 | + * - CONTROL, a short list with room to spare: the footer is drawn the same |
| 40 | + * way, so the placement does not depend on the list's length; |
| 41 | + * - no create grant and nothing more to show: no footer at all; |
| 42 | + * - no matching option: the empty state keeps its centred create button and |
| 43 | + * draws no footer; |
| 44 | + * - ↑/↓ still reach the last option and `aria-activedescendant` names an |
| 45 | + * option inside the listbox; |
| 46 | + * - while the popover is open the page behind it is hidden from assistive |
| 47 | + * technology, the observable half of `modal`. |
| 48 | + */ |
| 49 | +import { render, screen, fireEvent, waitFor, act, cleanup, within } from '@testing-library/react'; |
| 50 | +import '@testing-library/jest-dom'; |
| 51 | +import React from 'react'; |
| 52 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 53 | +import { MePermissionsProvider, type MePermissionsAuthenticatedResponse } from '@object-ui/permissions'; |
| 54 | +import { LookupField } from './LookupField'; |
| 55 | + |
| 56 | +afterEach(cleanup); |
| 57 | + |
| 58 | +const TARGET = 'crm_account'; |
| 59 | + |
| 60 | +const field = { |
| 61 | + name: 'account', |
| 62 | + label: 'Account', |
| 63 | + reference: TARGET, |
| 64 | + reference_field: 'name', |
| 65 | +} as any; |
| 66 | + |
| 67 | +function makeDataSource(shown: number, total: number) { |
| 68 | + const rows = Array.from({ length: total }, (_, i) => ({ id: `acc_${i + 1}`, name: `Account ${String(i + 1).padStart(2, '0')}` })); |
| 69 | + return { |
| 70 | + find: vi.fn().mockImplementation(async (_object: string, params: any) => { |
| 71 | + const q = String(params?.$search ?? '').trim(); |
| 72 | + const hit = q ? rows.filter((r) => r.name.includes(q)) : rows; |
| 73 | + return { data: hit.slice(0, shown), total: hit.length }; |
| 74 | + }), |
| 75 | + findOne: vi.fn(), |
| 76 | + create: vi.fn(), |
| 77 | + update: vi.fn(), |
| 78 | + delete: vi.fn(), |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +const deniedCreate: MePermissionsAuthenticatedResponse = { |
| 83 | + authenticated: true, |
| 84 | + userId: 'u-reader', |
| 85 | + tenantId: null, |
| 86 | + hasAnyQueue: false, |
| 87 | + objectsUsingQueues: [], |
| 88 | + positions: ['reader'], |
| 89 | + permissionSets: ['reader'], |
| 90 | + objects: { [TARGET]: { allowRead: true, allowCreate: false, allowEdit: false, allowDelete: false } }, |
| 91 | + fields: {}, |
| 92 | +}; |
| 93 | + |
| 94 | +async function openPicker(shown: number, total: number, wrap?: (node: React.ReactElement) => React.ReactElement) { |
| 95 | + const ds = makeDataSource(shown, total); |
| 96 | + const widget = <LookupField field={field} value={undefined} onChange={vi.fn()} readonly={false} dataSource={ds as any} />; |
| 97 | + const utils = render( |
| 98 | + <div> |
| 99 | + <button type="button">Outside the field</button> |
| 100 | + {wrap ? wrap(widget) : widget} |
| 101 | + </div>, |
| 102 | + ); |
| 103 | + await act(async () => { |
| 104 | + fireEvent.click(screen.getByTestId('lookup-trigger-account')); |
| 105 | + }); |
| 106 | + const popover = await screen.findByTestId('lookup-popover'); |
| 107 | + if (shown > 0) await waitFor(() => expect(within(popover).getAllByRole('option')).toHaveLength(shown)); |
| 108 | + return { ...utils, popover, listbox: within(popover).getByRole('listbox') }; |
| 109 | +} |
| 110 | + |
| 111 | +describe('LookupField popover pins quick-create and "show all" under the list (objectstack#36825)', () => { |
| 112 | + it('a long list (the clipped case): create and "show all" sit in the footer, outside the listbox', async () => { |
| 113 | + const { popover, listbox } = await openPicker(25, 60); |
| 114 | + const footer = within(popover).getByTestId('lookup-panel-footer'); |
| 115 | + const create = within(footer).getByTestId('lookup-create-new'); |
| 116 | + const showAll = within(footer).getByTestId('show-all-results'); |
| 117 | + expect(create).toHaveTextContent(/Create new/); |
| 118 | + expect(showAll).toHaveTextContent(/60/); |
| 119 | + expect(listbox).not.toContainElement(create); |
| 120 | + expect(listbox).not.toContainElement(showAll); |
| 121 | + // The footer follows the list, a sibling of its scroll container's box. |
| 122 | + expect(listbox.compareDocumentPosition(footer) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy(); |
| 123 | + expect(footer).not.toContainElement(listbox); |
| 124 | + }); |
| 125 | + |
| 126 | + it('CONTROL — a short list with room to spare draws the same footer', async () => { |
| 127 | + const { popover, listbox } = await openPicker(3, 3); |
| 128 | + const footer = within(popover).getByTestId('lookup-panel-footer'); |
| 129 | + const create = within(footer).getByTestId('lookup-create-new'); |
| 130 | + expect(listbox).not.toContainElement(create); |
| 131 | + // Nothing more to show: no "show all", anywhere. |
| 132 | + expect(within(popover).queryByTestId('show-all-results')).toBeNull(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('no create grant and nothing more to show: no footer', async () => { |
| 136 | + const { popover } = await openPicker(3, 3, (node) => ( |
| 137 | + <MePermissionsProvider initialPermissions={deniedCreate}>{node}</MePermissionsProvider> |
| 138 | + )); |
| 139 | + expect(within(popover).queryByTestId('lookup-panel-footer')).toBeNull(); |
| 140 | + expect(within(popover).queryByText(/Create new/)).toBeNull(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('no create grant, more to show: the footer holds "show all" alone', async () => { |
| 144 | + const { popover } = await openPicker(25, 60, (node) => ( |
| 145 | + <MePermissionsProvider initialPermissions={deniedCreate}>{node}</MePermissionsProvider> |
| 146 | + )); |
| 147 | + const footer = within(popover).getByTestId('lookup-panel-footer'); |
| 148 | + expect(within(footer).getByTestId('show-all-results')).toBeInTheDocument(); |
| 149 | + expect(within(popover).queryByTestId('lookup-create-new')).toBeNull(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('no matching option: the empty state keeps its centred create button and draws no footer', async () => { |
| 153 | + const { popover, listbox } = await openPicker(25, 60); |
| 154 | + const input = within(popover).getByRole('combobox'); |
| 155 | + await act(async () => { |
| 156 | + fireEvent.change(input, { target: { value: 'nothing matches this' } }); |
| 157 | + }); |
| 158 | + await waitFor(() => expect(within(popover).getByText('No options found')).toBeInTheDocument()); |
| 159 | + expect(within(listbox).getByRole('button', { name: /Create/ })).toBeInTheDocument(); |
| 160 | + expect(within(popover).queryByTestId('lookup-panel-footer')).toBeNull(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('↑/↓ still reach the last option and aria-activedescendant names an option in the listbox', async () => { |
| 164 | + const { popover, listbox } = await openPicker(25, 60); |
| 165 | + const input = within(popover).getByRole('combobox'); |
| 166 | + for (let i = 0; i < 30; i++) fireEvent.keyDown(input, { key: 'ArrowDown' }); |
| 167 | + const active = input.getAttribute('aria-activedescendant'); |
| 168 | + expect(active).toBeTruthy(); |
| 169 | + const option = document.getElementById(active!); |
| 170 | + expect(listbox).toContainElement(option); |
| 171 | + expect(option).toHaveTextContent('Account 25'); |
| 172 | + fireEvent.keyDown(input, { key: 'ArrowUp' }); |
| 173 | + expect(document.getElementById(input.getAttribute('aria-activedescendant')!)).toHaveTextContent('Account 24'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('while open, the page behind the popover is hidden from assistive technology (the popover is modal)', async () => { |
| 177 | + const { container, popover } = await openPicker(3, 3); |
| 178 | + expect(container).toHaveAttribute('aria-hidden', 'true'); |
| 179 | + expect(popover.closest('[aria-hidden="true"]')).toBeNull(); |
| 180 | + }); |
| 181 | +}); |
0 commit comments