import { render, screen } from '@testing-library/react'; import { JumpLinks } from '../JumpLinks'; import { JumpLinksItem } from '../JumpLinksItem'; import { JumpLinksList } from '../JumpLinksList'; test('simple jumplinks', () => { const { asFragment } = render( Inactive section Active section Inactive section ); expect(asFragment()).toMatchSnapshot(); }); test('jumplinks centered', () => { const { asFragment } = render( Inactive section Active section Inactive section ); expect(asFragment()).toMatchSnapshot(); }); test('jumplinks with label', () => { const { asFragment } = render( Inactive section Active section Inactive section ); expect(asFragment()).toMatchSnapshot(); }); test('vertical with label', () => { const { asFragment } = render( Inactive section Active section Inactive section ); expect(asFragment()).toMatchSnapshot(); }); test('expandable vertical with subsection', () => { const { asFragment } = render( Inactive section Section with active subsection Active subsection Inactive subsection Inactive subsection Inactive section Inactive section ); expect(asFragment()).toMatchSnapshot(); }); // Revamped tests begin here const jumpLinksItems = ( <> Inactive section Active section Inactive section ); const expandableJumpLinksItems = ( <> Inactive section Section with active subsection Active subsection Inactive subsection Inactive subsection Inactive section Inactive section ); test('renders label be default', () => { render({jumpLinksItems}); expect(screen.getByText(/Jump to section/i)).toBeTruthy(); }); test('does not render label when alwaysShowLabel is false', () => { render( {jumpLinksItems} ); expect(screen.queryByText(/Jump to section/i)).toBeFalsy(); }); test('uses aria-labelledby over aria-label when label and alwaysShowLabel are passed in', () => { render({expandableJumpLinksItems}); const navigation = screen.getByRole('navigation', { name: /Jump to section/i }); expect(navigation).toHaveAttribute('aria-labelledby'); expect(navigation).not.toHaveAttribute('aria-label'); }); test('uses aria-labelledby over aria-label when expandable is passed in', () => { render({expandableJumpLinksItems}); const navigation = screen.getByRole('navigation', { name: /Toggle jump links/i }); expect(navigation).toHaveAttribute('aria-labelledby'); expect(navigation).not.toHaveAttribute('aria-label'); }); test('random id is used with aria-labelledby by default in expandable case', () => { render({expandableJumpLinksItems}); const navigation = screen.getByRole('navigation', { name: /Toggle jump links/i }); expect(navigation).toHaveAttribute('aria-labelledby'); expect(navigation.getAttribute('aria-labelledby')).toBeTruthy(); }); test('random id is used with aria-labelledby by default in label case', () => { render( {expandableJumpLinksItems} ); const navigation = screen.getByRole('navigation', { name: /Jump to section/i }); expect(navigation).toHaveAttribute('aria-labelledby'); expect(navigation.getAttribute('aria-labelledby')).toBeTruthy(); }); test('custom labelId is used with aria-labelledby when it is passed in in expandable case', () => { render( {expandableJumpLinksItems} ); const navigation = screen.getByRole('navigation', { name: /Toggle jump links/i }); expect(navigation).toHaveAttribute('aria-labelledby', 'custom-id'); }); test('custom labelId is used with aria-labelledby when it is passed in in label case', () => { render( {expandableJumpLinksItems} ); const navigation = screen.getByRole('navigation', { name: /Jump to section/i }); expect(navigation).toHaveAttribute('aria-labelledby', 'custom-id'); }); test('uses aria-label instead of aria-labelledby by default', () => { render({jumpLinksItems}); const navigation = screen.getByRole('navigation', { name: /Custom aria label/i }); expect(navigation).toHaveAttribute('aria-label', 'Custom aria label'); expect(navigation).not.toHaveAttribute('aria-labelledby'); }); test('uses aria-label instead of aria-labelledby when label is provided but alwaysShowLabel is false', () => { render( {jumpLinksItems} ); const navigation = screen.getByRole('navigation', { name: /Custom aria label/i }); expect(navigation).toHaveAttribute('aria-label', 'Custom aria label'); expect(navigation).not.toHaveAttribute('aria-labelledby'); }); test('aria-labelledby is used with provided labelId even when aria-label is also provided in expandable case', () => { render( {expandableJumpLinksItems} ); const navigation = screen.getByRole('navigation'); expect(navigation).toHaveAttribute('aria-labelledby', 'custom-id'); expect(navigation).not.toHaveAttribute('aria-label'); }); test('aria-labelledby is used with provided labelId even when aria-label is also provided in label case', () => { render( {jumpLinksItems} ); const navigation = screen.getByRole('navigation'); expect(navigation).toHaveAttribute('aria-labelledby', 'custom-id'); expect(navigation).not.toHaveAttribute('aria-label'); }); test('does not throw error when there is a label passed in"', () => { const warnMock = jest.fn() as any; global.console = { warn: warnMock } as any; render( {jumpLinksItems} ); expect(warnMock).not.toHaveBeenCalled(); }); test('does not throw error when there is an aria-label passed in"', () => { const warnMock = jest.fn() as any; global.console = { warn: warnMock } as any; render( {jumpLinksItems} ); expect(warnMock).not.toHaveBeenCalled(); }); test('throws error when no label or ariaLabel are passed in', () => { const warnMock = jest.fn() as any; global.console = { warn: warnMock } as any; render({jumpLinksItems}); expect(warnMock).toHaveBeenCalled(); }); test('does not throw error when there is a toggleAriaLabel and expandable prop passed in', () => { const warnMock = jest.fn() as any; global.console = { warn: warnMock } as any; render( {expandableJumpLinksItems} ); expect(warnMock).not.toHaveBeenCalled(); }); test('does not throw error when there is an aria-label and expandable prop passed in', () => { const warnMock = jest.fn() as any; global.console = { warn: warnMock } as any; render( {expandableJumpLinksItems} ); expect(warnMock).not.toHaveBeenCalled(); }); test('throws error when there is no toggle aria-label or aria-label but expandable is passed in', () => { const warnMock = jest.fn() as any; global.console = { warn: warnMock } as any; render({expandableJumpLinksItems}); expect(warnMock).toHaveBeenCalled(); });