import * as React from 'react'; import ButtonLite from './ButtonLite'; import {render, within} from '@testing-library/react'; import {testA11y} from '../../axe'; import userEvent from '@testing-library/user-event'; describe('ButtonLite', () => { it('render', () => { const button = render(Some text); expect(button.getByRole('button', {name: 'Some text'})).toBeInTheDocument(); }); it('disabled', () => { const button = render(Some text); expect(button.getByRole('button')).toHaveProperty('disabled', true); }); describe('without `href`', () => { it('has a button role and an accessible label', () => { const label = 'Load more Mathematic questions'; const button = render( Load more ); expect( button.getByRole('button', { name: label, }) ).toBeInTheDocument(); }); it('is focusable', () => { const button = render(Read more); button.getByRole('button').focus(); expect(button.getByRole('button')).toBe(document.activeElement); }); it('is has reset type', () => { const button = render(Reset form); expect(button.getByRole('button').getAttribute('type')).toEqual('reset'); }); it('is not focusable and clickable when disabled', () => { const handleOnClick = jest.fn(); const label = 'Load more'; const button = render( {label} ); button.getByText(label).focus(); expect(button.getByText(label)).not.toBe(document.activeElement); userEvent.click(button.getByText(label)); expect(handleOnClick).not.toHaveBeenCalled(); }); it('fires onClick on click, space and enter', () => { const handleOnClick = jest.fn(); const label = 'Load more'; const button = render( {label} ); userEvent.click( button.getByRole('button', { name: label, }) ); expect(handleOnClick).toHaveBeenCalledTimes(1); button.getByText(label).focus(); userEvent.keyboard('{space}'); expect(handleOnClick).toHaveBeenCalledTimes(2); userEvent.keyboard('{enter}'); expect(handleOnClick).toHaveBeenCalledTimes(3); }); it('informs about the loading state and is then disabled', () => { const label = 'Load more'; const loadingAriaLabel = 'loading more'; const button = render( {label} ); const status = button.getByRole('status'); expect(status.getAttribute('aria-live')).toBe('assertive'); expect(within(status).getByText(loadingAriaLabel)).toBeInTheDocument(); expect(button.getByRole('button')).toHaveProperty('disabled', true); }); }); describe('with `href`', () => { it('has a link role and an accessible label', () => { const label = 'read more about products'; const button = render( Read more ); expect( button.getByRole('link', { name: label, }) ).toBeInTheDocument(); expect(button.getByRole('link').getAttribute('href')).toBe( 'https://example.com/' ); }); it('is focusable', () => { const button = render( Read more ); button.getByRole('link').focus(); expect(button.getByRole('link')).toBe(document.activeElement); }); it('is not focusable and clickable when disabled', () => { const label = 'read more'; const onClick = jest.fn(); const button = render( {label} ); button.getByText(label).focus(); expect(button.getByText(label)).not.toBe(document.activeElement); userEvent.click(button.getByText(label)); expect(onClick).not.toHaveBeenCalled(); }); it('informs about the opening in a new tab', () => { const label = 'read more'; const newTabLabel = 'in new tab'; const button = render( {label} ); expect(button.getByText(newTabLabel)).toBeInTheDocument(); }); }); describe('a11y', () => { describe('without `href`', () => { it('should have no a11y violations', async () => { await testA11y(Read more); }); it('should have no a11y violations when aria-label is provided', async () => { await testA11y( Read more ); }); it('should have no a11y violations when disabled', async () => { await testA11y(Read more); }); it('should have no a11y violations in loading state', async () => { await testA11y(Read more); }); }); describe('with `href`', () => { it('should have no a11y violations', async () => { await testA11y( Read more ); }); it('should have no a11y violations when aria-label is provided', async () => { await testA11y( Read more ); }); it('should have no a11y violations when disabled', async () => { await testA11y( Read more ); }); it('should have no a11y violations when opens in a new tab', async () => { await testA11y( Read more ); }); }); }); });