import * as React from 'react'; import Label from './Label'; import {fireEvent, render} from '@testing-library/react'; import {testA11y} from '../../axe'; describe('Label', () => { it('render', () => { const label = render( ); expect(label.getByText('example label')).toBeTruthy(); }); it('render with icon', () => { const label = render( ); expect(label.getByRole('img')).toBeTruthy(); }); it('when onClose is defined, has close button', () => { const mockCallback = jest.fn(); const label = render( ); expect(label.getByRole('button', {name: 'close'})).toBeTruthy(); }); it('clicking on close button calls onClose', () => { const mockCallback = jest.fn(); const label = render( ); const closeButton = label.getByRole('button', {name: 'close'}); fireEvent.click(closeButton); expect(mockCallback).toHaveBeenCalled(); }); it('should have an accessible name provided by aria-label', async () => { const name = 'accessible name'; const label = render(); expect(label.getByLabelText(name)).toBeTruthy(); }); it('should have an accessible name for the close button', async () => { const buttonName = 'remove attachment'; const handleOnClose = jest.fn(); const label = render( ); expect( label.getByRole('button', { name: buttonName, }) ).toBeTruthy(); }); it('should have an accessible name for an icon', async () => { const iconTitle = 'your attachment'; const label = render( ); expect( label.getByRole('img', { name: iconTitle, }) ).toBeTruthy(); }); it('should have an icon which is hidden from accessibility tree', async () => { const label = render( ); expect(label.queryByRole('img')).toBeFalsy(); }); describe('a11y', () => { it('should have no a11y violations', async () => { await testA11y(); }); it('should have no a11y violations when a named icon is provided', async () => { await testA11y( ); }); it('should have no a11y violations when icon is hidden from accessibility tree', async () => { await testA11y( ); }); it('should have no a11y violations when onClose is provided', async () => { const handleOnClose = jest.fn(); await testA11y(); }); }); });