import { createRef } from 'react'; import { render, screen } from '@testing-library/react'; import ButtonLink from './ButtonLink'; describe('ButtonLink', () => { it('handles click', () => { const mockOnClick = jest.fn< [ { dangerouslyDisableOnNavigation: () => void; event: React.MouseEvent | React.KeyboardEvent; }, ], // @ts-expect-error - TS2344 - Type 'undefined' does not satisfy the constraint 'any[]'. undefined >(); render(); screen.getByText('ButtonText').click(); expect(mockOnClick).toHaveBeenCalled(); }); it('renders with correct new tab announcement with and without accessibilityLabel', () => { render( , ); expect( screen.getByText('Visit Pinterest', { exact: true, }), ).toBeVisible(); expect( screen.getByText('; Opens a new tab', { exact: true, }), ).toBeVisible(); render( , ); expect(screen.getByLabelText('Visit Pinterest; Opens a new tab')).toBeVisible(); }); it('renders with sequential keyboard navigation and forwards a ref to the innermost element', () => { const ref = createRef(); render(); 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( , ); 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( , ); expect(ref.current instanceof HTMLAnchorElement).toEqual(true); expect(ref.current instanceof HTMLAnchorElement && ref.current?.tabIndex).toEqual(-1); }); it('renders with data-test-id', () => { const TEST_ID = 'button-test-123'; render( , ); expect( screen.getByTestId(TEST_ID, { exact: true, }), ).toBeVisible(); }); });