import * as React from 'react'; import { render, screen, within } from '@testing-library/react'; import { Icon } from '../Icon'; import { ComponentClassNames } from '../../shared'; describe('Icon component', () => { const iconTestId = 'iconSearch'; const pathData = `M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z`; it('should render with default attributes', async () => { render(); const icon = await screen.findByTestId(iconTestId); expect(icon.id).toBe(iconTestId); expect(icon.nodeName).toBe('svg'); expect(icon.dataset['size']).toBeUndefined(); expect(icon.getAttribute('viewBox')).toBe('0 0 24 24'); expect(icon.classList[0]).toContain(ComponentClassNames.Icon); }); it('should render with provided path data', async () => { render(); const icon = await screen.findByTestId(iconTestId); expect(icon.childNodes.length).toBe(1); const path = icon.childNodes[0] as SVGElement; expect(path.getAttribute('d')).toBe(pathData); expect(path.getAttribute('fill')).toBe('currentColor'); }); it('should render a classname for Icon', async () => { render( ); const icon = await screen.findByTestId(iconTestId); expect(icon.classList.length).toBe(2); expect(icon.classList[0]).toContain(ComponentClassNames.Icon); expect(icon.classList[1]).toContain('my-icon-component'); }); it('should render an ariaLabel for Icon', async () => { render(); const icon = await screen.findByTestId(iconTestId); expect(icon.getAttribute('aria-label')).toBe('Search'); }); it('should forward ref to DOM element', async () => { const ref = React.createRef(); render(); await screen.findByTestId(iconTestId); expect(ref.current?.nodeName).toBe('svg'); }); it('can set viewBox attribute', async () => { render( ); const icon = await screen.findByTestId(iconTestId); expect(icon.getAttribute('viewBox')).toBe('0 0 100 100'); }); it('can accept SVG children', async () => { render( ); const icon = await screen.findByTestId(iconTestId); const paths = await within(icon).findAllByRole('path'); expect(paths.length).toBe(2); expect(paths[0].getAttribute('opacity')).toBe('0.5'); }); it('can accept an array of path-like objects', async () => { render( ); const icon = await screen.findByTestId(iconTestId); const paths = await within(icon).findAllByRole('path'); expect(paths.length).toBe(1); expect(paths[0].getAttribute('stroke-linejoin')).toBe('bevel'); }); });