import React from 'react'; import { render, within } from '@testing-library/react'; import { axe } from 'jest-axe'; import { TableSkeleton } from './TableSkeleton'; describe('packages/skeleton-loader/TableSkeleton', () => { describe('a11y', () => { test('does not have basic accessibility issues', async () => { const { container } = render(); const results = await axe(container); expect(results).toHaveNoViolations(); }); }); describe('numCols prop', () => { test('defaults to 4', async () => { const { getAllByRole } = render(); expect(getAllByRole('columnheader').length).toEqual(4); }); test('renders correct number of column headers', async () => { const { getAllByRole } = render(); const thead = getAllByRole('rowgroup')[0]; expect(within(thead).getAllByRole('columnheader').length).toEqual(6); }); }); describe('numRows prop', () => { test('defaults to 5', async () => { const { getAllByRole } = render(); const tbody = getAllByRole('rowgroup')[1]; expect(within(tbody).getAllByRole('row').length).toEqual(5); }); test('renders correct number of rows', async () => { const { getAllByRole } = render(); const tbody = getAllByRole('rowgroup')[1]; expect(within(tbody).getAllByRole('row').length).toEqual(6); }); }); describe('columnLabels prop', () => { test('renders when all text is provided', async () => { const { getByText } = render( , ); expect(getByText('col1')).toBeInTheDocument(); expect(getByText('col2')).toBeInTheDocument(); expect(getByText('col3')).toBeInTheDocument(); expect(getByText('col4')).toBeInTheDocument(); }); test('renders as many headers as numCols specifies', async () => { const { getByText, queryByText } = render( , ); expect(getByText('col1')).toBeInTheDocument(); expect(getByText('col2')).toBeInTheDocument(); expect(getByText('col3')).toBeInTheDocument(); expect(queryByText('col4')).not.toBeInTheDocument(); }); test('empty strings render skeletons', async () => { const { getAllByRole } = render( , ); const thead = getAllByRole('rowgroup')[0]; const secondTh = within(thead).getAllByRole('columnheader')[1]; expect(secondTh.querySelector('div')).toBeInTheDocument(); // VisuallyHidden text content expect(secondTh).toHaveTextContent('Loading'); }); test('undefined renders skeletons', async () => { const { getAllByRole } = render( , ); const thead = getAllByRole('rowgroup')[0]; const secondTh = within(thead).getAllByRole('columnheader')[1]; expect(secondTh.querySelector('div')).toBeInTheDocument(); // VisuallyHidden text content expect(secondTh).toHaveTextContent('Loading'); }); }); });