import { createRef } from 'react'; import { fireEvent, render, screen } from '@testing-library/react'; import TapArea from './TapArea'; describe('TapArea', () => { it('TapArea handles onTap callback', () => { const mockOnTap = jest.fn< [ { event: React.MouseEvent | React.KeyboardEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render(TapArea); screen.getByText('TapArea').click(); expect(mockOnTap).toHaveBeenCalled(); }); it('TapArea handles onBlur callback', () => { const mockOnBlur = jest.fn< [ { event: React.FocusEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render(TapArea); fireEvent.focus(screen.getByText('TapArea')); fireEvent.blur(screen.getByText('TapArea')); expect(mockOnBlur).toHaveBeenCalled(); }); it('TapArea handles onFocus callback', () => { const mockOnFocus = jest.fn< [ { event: React.FocusEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render(TapArea); fireEvent.focus(screen.getByText('TapArea')); expect(mockOnFocus).toHaveBeenCalled(); }); it('TapArea handles onMouseEnter callback', () => { const mockOnMouseEnter = jest.fn< [ { event: React.MouseEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render(TapArea); fireEvent.mouseEnter(screen.getByText('TapArea')); expect(mockOnMouseEnter).toHaveBeenCalled(); }); it('TapArea handles onMouseLeave callback', () => { const mockOnMouseLeave = jest.fn< [ { event: React.MouseEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render(TapArea); fireEvent.mouseLeave(screen.getByText('TapArea')); expect(mockOnMouseLeave).toHaveBeenCalled(); }); it('TapArea handles key press event', () => { const mockOnTap = jest.fn< [ { event: React.MouseEvent | React.KeyboardEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render(TapArea); const mockEvent = { charCode: 32, // @ts-expect-error - TS2344 - Type 'unknown' does not satisfy the constraint 'any[]'. preventDefault: jest.fn, unknown>(), } as const; fireEvent.keyPress(screen.getByText('TapArea'), mockEvent); expect(mockOnTap).toHaveBeenCalled(); }); it('renders a TapArea with sequential keyboard navigation and forwards a ref to the innermost element', () => { const ref = createRef(); render(Text); expect(ref.current instanceof HTMLDivElement).toEqual(true); expect(ref.current instanceof HTMLDivElement && ref.current?.tabIndex).toEqual(0); }); it('renders a disabled TapArea', () => { const ref = createRef(); render( Text , ); expect(ref.current instanceof HTMLDivElement).toEqual(true); expect(ref.current instanceof HTMLDivElement && ref.current?.tabIndex).toEqual(-1); }); it('renders a TapArea removed from sequential keyboard navigation via tabIndex', () => { const ref = createRef(); render( Text , ); expect(ref.current instanceof HTMLDivElement).toEqual(true); expect(ref.current instanceof HTMLDivElement && ref.current?.tabIndex).toEqual(-1); }); });