|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +import {useMemo} from 'react'; |
| 3 | +import { |
| 4 | + render, |
| 5 | + screen, |
| 6 | + fireEvent, |
| 7 | + waitFor, |
| 8 | + cleanup, |
| 9 | +} from '@testing-library/react'; |
| 10 | +import {afterEach, describe, it, expect, vi} from 'vitest'; |
| 11 | +import {warmMenuBottomSheet} from '../DropdownMenu/__tests__/warmMenuBottomSheet'; |
| 12 | +import {beforeAll} from 'vitest'; |
| 13 | +beforeAll(warmMenuBottomSheet, 20000); |
| 14 | +import {Table} from './Table'; |
| 15 | +import type {BodyCellRenderProps, BodyRowRenderProps} from './types'; |
| 16 | +import {useTableRowsVirtualization} from './plugins/virtualization/useTableRowsVirtualization'; |
| 17 | +const data = Array.from({length: 100}, (_, i) => ({ |
| 18 | + id: String(i), |
| 19 | + name: `Row ${i}`, |
| 20 | +})); |
| 21 | +const columns = [{key: 'name', header: 'Name'}]; |
| 22 | +afterEach(() => { |
| 23 | + cleanup(); |
| 24 | + vi.restoreAllMocks(); |
| 25 | +}); |
| 26 | +function Fixture({items = data}: {items?: typeof data}) { |
| 27 | + const virtualization = useTableRowsVirtualization({ |
| 28 | + rowHeight: 56, |
| 29 | + viewportHeight: 240, |
| 30 | + }); |
| 31 | + const menu = useMemo( |
| 32 | + () => ({ |
| 33 | + rowModel: 'flat' as const, |
| 34 | + transformBodyRow(props: BodyRowRenderProps) { |
| 35 | + return {...props, htmlProps: {...props.htmlProps, tabIndex: 0}}; |
| 36 | + }, |
| 37 | + transformBodyCell(props: BodyCellRenderProps) { |
| 38 | + return { |
| 39 | + ...props, |
| 40 | + contextMenuActions: [ |
| 41 | + {id: 'inspect', label: 'Inspect row', onSelect: () => {}}, |
| 42 | + ], |
| 43 | + }; |
| 44 | + }, |
| 45 | + }), |
| 46 | + [], |
| 47 | + ); |
| 48 | + return ( |
| 49 | + <Table |
| 50 | + data={items} |
| 51 | + columns={columns} |
| 52 | + idKey="id" |
| 53 | + virtualization={virtualization} |
| 54 | + plugins={{menu}} |
| 55 | + /> |
| 56 | + ); |
| 57 | +} |
| 58 | +describe('Table recognized menu focus under windowing', () => { |
| 59 | + it('returns removed owned menu focus even when its row was not focused before opening', async () => { |
| 60 | + const {container, rerender} = render(<Fixture />); |
| 61 | + const viewport = container.querySelector( |
| 62 | + '.astryx-table-scroll-wrapper', |
| 63 | + ) as HTMLDivElement; |
| 64 | + const body = container.querySelector('tbody') as HTMLTableSectionElement; |
| 65 | + let count = data.length; |
| 66 | + Object.defineProperties(viewport, { |
| 67 | + clientHeight: {configurable: true, value: 240}, |
| 68 | + clientWidth: {configurable: true, value: 400}, |
| 69 | + scrollHeight: {configurable: true, get: () => count * 56 + 36}, |
| 70 | + scrollWidth: {configurable: true, value: 400}, |
| 71 | + }); |
| 72 | + vi.spyOn(viewport, 'getBoundingClientRect').mockReturnValue( |
| 73 | + new DOMRect(0, 0, 400, 240), |
| 74 | + ); |
| 75 | + vi.spyOn(body, 'getBoundingClientRect').mockImplementation( |
| 76 | + () => new DOMRect(0, 36 - viewport.scrollTop, 400, count * 56), |
| 77 | + ); |
| 78 | + fireEvent.scroll(viewport); |
| 79 | + const row = container.querySelector( |
| 80 | + 'tbody tr[data-table-logical-index="0"]', |
| 81 | + ) as HTMLTableRowElement; |
| 82 | + expect(document.activeElement).not.toBe(row); |
| 83 | + fireEvent.contextMenu(row.cells[0].firstElementChild as HTMLElement, { |
| 84 | + clientX: 50, |
| 85 | + clientY: 50, |
| 86 | + }); |
| 87 | + const item = ( |
| 88 | + await screen.findAllByRole('menuitem', { |
| 89 | + name: 'Inspect row', |
| 90 | + hidden: true, |
| 91 | + }) |
| 92 | + )[0]; |
| 93 | + item.focus(); |
| 94 | + expect(document.activeElement).toBe(item); |
| 95 | + viewport.scrollTop = 4000; |
| 96 | + fireEvent.scroll(viewport); |
| 97 | + await waitFor(() => |
| 98 | + expect( |
| 99 | + container.querySelector('tbody tr[data-table-logical-index="70"]'), |
| 100 | + ).not.toBeNull(), |
| 101 | + ); |
| 102 | + expect(row.isConnected).toBe(true); |
| 103 | + count -= 1; |
| 104 | + rerender(<Fixture items={data.slice(1)} />); |
| 105 | + await waitFor(() => expect(viewport).toHaveFocus()); |
| 106 | + expect(row.isConnected).toBe(false); |
| 107 | + }); |
| 108 | + it.each([false, true])( |
| 109 | + 'releases a null-target blur only when the document is focused (focused=%s)', |
| 110 | + async isDocumentFocused => { |
| 111 | + const {container} = render(<Fixture />); |
| 112 | + const viewport = container.querySelector( |
| 113 | + '.astryx-table-scroll-wrapper', |
| 114 | + ) as HTMLDivElement; |
| 115 | + const body = container.querySelector('tbody')!; |
| 116 | + Object.defineProperties(viewport, { |
| 117 | + clientHeight: {configurable: true, value: 240}, |
| 118 | + clientWidth: {configurable: true, value: 400}, |
| 119 | + scrollHeight: {configurable: true, value: 5636}, |
| 120 | + scrollWidth: {configurable: true, value: 400}, |
| 121 | + }); |
| 122 | + vi.spyOn(viewport, 'getBoundingClientRect').mockReturnValue({ |
| 123 | + x: 0, |
| 124 | + y: 0, |
| 125 | + top: 0, |
| 126 | + left: 0, |
| 127 | + right: 400, |
| 128 | + bottom: 240, |
| 129 | + width: 400, |
| 130 | + height: 240, |
| 131 | + toJSON: () => ({}), |
| 132 | + }); |
| 133 | + vi.spyOn(body, 'getBoundingClientRect').mockImplementation(() => ({ |
| 134 | + x: 0, |
| 135 | + y: 36 - viewport.scrollTop, |
| 136 | + top: 36 - viewport.scrollTop, |
| 137 | + left: 0, |
| 138 | + right: 400, |
| 139 | + bottom: 5636 - viewport.scrollTop, |
| 140 | + width: 400, |
| 141 | + height: 5600, |
| 142 | + toJSON: () => ({}), |
| 143 | + })); |
| 144 | + fireEvent.scroll(viewport); |
| 145 | + const row = container.querySelector( |
| 146 | + 'tbody tr[data-table-logical-index="0"]', |
| 147 | + ) as HTMLTableRowElement; |
| 148 | + row.focus(); |
| 149 | + viewport.scrollTop = 4000; |
| 150 | + fireEvent.scroll(viewport); |
| 151 | + await waitFor(() => |
| 152 | + expect( |
| 153 | + container.querySelector('tbody tr[data-table-logical-index="70"]'), |
| 154 | + ).not.toBeNull(), |
| 155 | + ); |
| 156 | + expect(row.isConnected).toBe(true); |
| 157 | + vi.spyOn(document, 'hasFocus').mockReturnValue(isDocumentFocused); |
| 158 | + fireEvent.focusOut(row, {relatedTarget: null}); |
| 159 | + await waitFor(() => expect(row.isConnected).toBe(!isDocumentFocused)); |
| 160 | + if (!isDocumentFocused) { |
| 161 | + expect(document.activeElement).toBe(row); |
| 162 | + const outside = document.createElement('button'); |
| 163 | + document.body.append(outside); |
| 164 | + outside.focus(); |
| 165 | + await waitFor(() => expect(row.isConnected).toBe(false)); |
| 166 | + expect(document.activeElement).toBe(outside); |
| 167 | + outside.remove(); |
| 168 | + } |
| 169 | + }, |
| 170 | + ); |
| 171 | + it('retains the same logical row while its menu owns focus and returns to its trigger', async () => { |
| 172 | + const {container} = render(<Fixture />); |
| 173 | + const viewport = container.querySelector( |
| 174 | + '.astryx-table-scroll-wrapper', |
| 175 | + ) as HTMLDivElement; |
| 176 | + const body = container.querySelector('tbody')!; |
| 177 | + Object.defineProperties(viewport, { |
| 178 | + clientHeight: {configurable: true, value: 240}, |
| 179 | + clientWidth: {configurable: true, value: 400}, |
| 180 | + scrollHeight: {configurable: true, value: 5636}, |
| 181 | + scrollWidth: {configurable: true, value: 400}, |
| 182 | + }); |
| 183 | + vi.spyOn(viewport, 'getBoundingClientRect').mockReturnValue({ |
| 184 | + x: 0, |
| 185 | + y: 0, |
| 186 | + top: 0, |
| 187 | + left: 0, |
| 188 | + right: 400, |
| 189 | + bottom: 240, |
| 190 | + width: 400, |
| 191 | + height: 240, |
| 192 | + toJSON: () => ({}), |
| 193 | + }); |
| 194 | + vi.spyOn(body, 'getBoundingClientRect').mockImplementation(() => ({ |
| 195 | + x: 0, |
| 196 | + y: 36 - viewport.scrollTop, |
| 197 | + top: 36 - viewport.scrollTop, |
| 198 | + left: 0, |
| 199 | + right: 400, |
| 200 | + bottom: 5636 - viewport.scrollTop, |
| 201 | + width: 400, |
| 202 | + height: 5600, |
| 203 | + toJSON: () => ({}), |
| 204 | + })); |
| 205 | + fireEvent.scroll(viewport); |
| 206 | + const row = container.querySelector( |
| 207 | + 'tbody tr[data-table-logical-index="0"]', |
| 208 | + ) as HTMLTableRowElement; |
| 209 | + row.focus(); |
| 210 | + fireEvent.contextMenu(row.cells[0].firstElementChild!, { |
| 211 | + clientX: 50, |
| 212 | + clientY: 50, |
| 213 | + }); |
| 214 | + const item = ( |
| 215 | + await screen.findAllByRole('menuitem', { |
| 216 | + name: 'Inspect row', |
| 217 | + hidden: true, |
| 218 | + }) |
| 219 | + )[0]; |
| 220 | + item.focus(); |
| 221 | + viewport.scrollTop = 4000; |
| 222 | + fireEvent.scroll(viewport); |
| 223 | + await waitFor(() => |
| 224 | + expect( |
| 225 | + container.querySelector('tbody tr[data-table-logical-index="70"]'), |
| 226 | + ).not.toBeNull(), |
| 227 | + ); |
| 228 | + expect(row.isConnected).toBe(true); |
| 229 | + expect(document.activeElement).toBe(item); |
| 230 | + fireEvent.keyDown(item, {key: 'Escape'}); |
| 231 | + await waitFor(() => expect(document.activeElement).toBe(row)); |
| 232 | + expect(row.isConnected).toBe(true); |
| 233 | + }); |
| 234 | +}); |
0 commit comments