import * as React from 'react'; import { render } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { buttonAccessibilityBehaviorDefinition, validateBehavior, ComponentTestFacade } from '@fluentui/a11y-testing'; import { isConformant } from '../../testing/isConformant'; import { Button } from './Button'; import type { ButtonProps } from './Button.types'; describe('Button', () => { isConformant({ Component: Button as React.FunctionComponent, displayName: 'Button', testOptions: { 'has-static-classnames': [ { props: { icon: 'Test Icon', }, }, ], }, }); describe('meets accessibility requirements', () => { const testFacade = new ComponentTestFacade(Button, {}); const errors = validateBehavior(buttonAccessibilityBehaviorDefinition, testFacade); expect(errors).toEqual([]); afterAll(() => { // Reset body after behavioral checks are done document.body.innerHTML = ''; }); }); describe('when rendered as a button', () => { it('renders correctly', () => { const { getByRole } = render(); const button = getByRole('button'); expect(button.tagName).toBe('BUTTON'); }); it('can be focused', () => { const { getByRole } = render(); const button = getByRole('button'); expect(document.activeElement).not.toEqual(button); userEvent.tab(); expect(document.activeElement).toEqual(button); }); it('cannot be focused when disabled has been passed to the component', () => { const { getByRole } = render(); const button = getByRole('button'); expect(document.activeElement).not.toEqual(button); userEvent.tab(); expect(document.activeElement).not.toEqual(button); }); it('can be focused when disabledFocusable has been passed to the component', () => { const { getByRole } = render(); const button = getByRole('button'); expect(document.activeElement).not.toEqual(button); userEvent.tab(); expect(document.activeElement).toEqual(button); }); it('can trigger a function by being clicked', () => { const onClick = jest.fn(); const { getByRole } = render(); userEvent.click(getByRole('button')); expect(onClick).toHaveBeenCalled(); }); it('does not trigger a function by being clicked when disabled has been passed to the component', () => { const onClick = jest.fn(); const { getByRole } = render( , ); userEvent.click(getByRole('button')); expect(onClick).not.toHaveBeenCalled(); }); it('does not trigger a function by being clicked when disabledFocusable has been passed to the component', () => { const onClick = jest.fn(); const { getByRole } = render( , ); userEvent.click(getByRole('button')); expect(onClick).not.toHaveBeenCalled(); }); }); describe('when rendered as an anchor', () => { it('renders correctly', () => { const { getByRole } = render( , ); const anchor = getByRole('link'); expect(anchor.tagName).toBe('A'); expect(anchor.getAttribute('role')).toBeFalsy(); }); it('applies role and tabindex with no href', () => { const { getByRole } = render(); const anchor = getByRole('button'); expect(anchor.tagName).toBe('A'); expect(anchor.getAttribute('role')).toEqual('button'); expect(anchor.tabIndex).toEqual(0); }); it('can be focused', () => { const { getByRole } = render( , ); const anchor = getByRole('link'); expect(document.activeElement).not.toEqual(anchor); userEvent.tab(); expect(document.activeElement).toEqual(anchor); }); it('cannot be focused when disabled has been passed to the component', () => { const { getByRole } = render( , ); const anchor = getByRole('link'); expect(document.activeElement).not.toEqual(anchor); userEvent.tab(); expect(document.activeElement).not.toEqual(anchor); }); it('can be focused when disabledFocusable has been passed to the component', () => { const { getByRole } = render( , ); const anchor = getByRole('link'); expect(document.activeElement).not.toEqual(anchor); userEvent.tab(); expect(document.activeElement).toEqual(anchor); }); }); });