import React from 'react' import { render } from '../utils/test-utils' import type { GridCellFocusHealProps } from './grid-cell-focus-heal' import { GridCellFocusHeal } from './grid-cell-focus-heal' import { GridCellDefault } from '../components' import type { CellParams } from '../types' import userEvent from '@testing-library/user-event' const ParentWrapper = (props: Omit) => { const ref = React.useRef(null) return (
) } const generateCellRenderer = () => function Renderer(props: CellParams) { return } const cellRendererProps: CellParams = { columnId: 'name', rowId: '1', label: 'Value', tabIndex: 0, value: 'value', } const originalFocus = Object.getOwnPropertyDescriptor( global.HTMLElement.prototype, 'focus' )! describe('GridCellFocusHeal', () => { let focusSpy: jest.SpyInstance beforeEach(() => { Object.defineProperty( global.HTMLElement.prototype, 'focus', originalFocus ) focusSpy = jest.spyOn(HTMLElement.prototype, 'focus') }) it('should retain focus if focused before render', async () => { const { getByText, rerender } = render( ) await userEvent.keyboard('{Tab}') expect(getByText('Value')).toHaveFocus() focusSpy.mockClear() rerender( ) expect(getByText('Value')).toHaveFocus() expect(focusSpy).toHaveBeenCalledTimes(1) expect(focusSpy).toHaveBeenCalledWith({ preventScroll: true }) }) it('should not steal focus if not focused before render even if hasFocus is true', async () => { const { getByText, rerender } = render( ) expect(getByText('Value')).not.toHaveFocus() rerender( ) expect(getByText('Value')).not.toHaveFocus() expect(focusSpy).not.toHaveBeenCalled() }) it('should not steal focus if focused before render but hasFocus is false', async () => { const { getByText, rerender } = render( ) await userEvent.keyboard('{Tab}') expect(getByText('Value')).toHaveFocus() focusSpy.mockClear() rerender( ) expect(getByText('Value')).not.toHaveFocus() expect(focusSpy).not.toHaveBeenCalled() }) })