import { createRef } from 'react'; import { fireEvent, render, screen } from '@testing-library/react'; import TapAreaLink from './TapAreaLink'; describe('TapAreaLink', () => { it('TapAreaLink handles onTap', () => { const mockOnTap = jest.fn< [ { dangerouslyDisableOnNavigation: () => void; event: React.MouseEvent | React.KeyboardEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render( TapAreaLink , ); screen.getByText('TapAreaLink').click(); expect(mockOnTap).toHaveBeenCalled(); }); it('TapAreaLink handles onBlur callback', () => { const mockOnBlur = jest.fn< [ { event: React.FocusEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render( TapAreaLink , ); fireEvent.focus(screen.getByText('TapAreaLink')); fireEvent.blur(screen.getByText('TapAreaLink')); expect(mockOnBlur).toHaveBeenCalled(); }); it('TapAreaLink handles onFocus callback', () => { const mockOnFocus = jest.fn< [ { event: React.FocusEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render( TapAreaLink , ); fireEvent.focus(screen.getByText('TapAreaLink')); expect(mockOnFocus).toHaveBeenCalled(); }); it('TapAreaLink handles onMouseEnter callback', () => { const mockOnMouseEnter = jest.fn< [ { event: React.MouseEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render( TapAreaLink , ); fireEvent.mouseEnter(screen.getByText('TapAreaLink')); expect(mockOnMouseEnter).toHaveBeenCalled(); }); it('TapAreaLink handles onMouseLeave callback', () => { const mockOnMouseLeave = jest.fn< [ { event: React.MouseEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render( TapAreaLink , ); fireEvent.mouseLeave(screen.getByText('TapAreaLink')); expect(mockOnMouseLeave).toHaveBeenCalled(); }); it('TapAreaLink handles key press event', () => { const mockOnTap = jest.fn< [ { dangerouslyDisableOnNavigation: () => void; event: React.MouseEvent | React.KeyboardEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render( TapAreaLink , ); 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('TapAreaLink'), mockEvent); expect(mockOnTap).toHaveBeenCalled(); }); it('renders with sequential keyboard navigation and forwards a ref to the innermost element', () => { const ref = createRef(); render( TapAreaLink , ); expect(ref.current instanceof HTMLAnchorElement).toEqual(true); expect(ref.current instanceof HTMLAnchorElement && ref.current?.href).toEqual( 'http://www.pinterest.com/', ); expect(ref.current instanceof HTMLAnchorElement && ref.current?.tabIndex).toEqual(0); }); it('renders disabled', () => { const ref = createRef(); render( TapAreaLink , ); expect(ref.current instanceof HTMLAnchorElement).toEqual(true); expect(ref.current instanceof HTMLAnchorElement && ref.current?.href).toEqual(''); expect(ref.current instanceof HTMLAnchorElement && ref.current?.tabIndex).toEqual(0); }); it('renders removed from sequential keyboard navigation via tabIndex', () => { const ref = createRef(); render( TapAreaLink , ); expect(ref.current instanceof HTMLAnchorElement).toEqual(true); expect(ref.current instanceof HTMLAnchorElement && ref.current?.tabIndex).toEqual(-1); }); it('renders with correct new tab announcement with and without accessibilityLabel', () => { render( Visit Pinterest , ); expect( screen.getByText('Visit Pinterest', { exact: true, }), ).toBeVisible(); expect( screen.getByText('; Opens a new tab', { exact: true, }), ).toBeVisible(); render( Visit Pinterest , ); expect(screen.getByLabelText('Visit Pinterest; Opens a new tab')).toBeVisible(); }); it('renders with data-test-id', () => { const TEST_ID = 'button-test-123'; render( Visit Pinterest , ); expect( screen.getByTestId(TEST_ID, { exact: true, }), ).toBeVisible(); }); it('renders with a provided title', () => { const TEST_TITLE = 'test title'; render( Visit Pinterest , ); const anchorTagElement = screen.getByRole('link'); expect(anchorTagElement.getAttribute('title')).toBe(TEST_TITLE); }); });