import { render, screen } from '@testing-library/react';
import { Table } from '../Table';
import { Td } from '../Td';
import { Th } from '../Th';
import { AnimationsProvider, useAnimationsConfig, useHasAnimations } from '@patternfly/react-core/dist/esm/helpers';
import styles from '@patternfly/react-styles/css/components/Table/table';
test('Renders without children', () => {
render(
);
expect(screen.getByRole('grid', { name: 'Test table' })).toBeInTheDocument();
});
test('Renders with children', () => {
render(
);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveTextContent('Table caption');
});
test('Renders with role="treegrid" when isTreeTable is true', () => {
render();
expect(screen.getByRole('treegrid', { name: 'Test table' })).toBeInTheDocument();
});
test(`Renders with class ${styles.table} by default`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.table);
});
test(`Renders with a pf-m-grid class by default`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(/^pf-m-grid/);
});
test(`Renders with a pf-m-tree-view-grid class when isTreeTable is true`, () => {
render();
expect(screen.getByRole('treegrid', { name: 'Test table' })).not.toHaveClass(/^pf-m-grid/);
expect(screen.getByRole('treegrid', { name: 'Test table' })).toHaveClass(/^pf-m-tree-view-grid/);
});
test(`Does not render with class ${styles.modifiers.animateExpand} by default`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.animateExpand);
});
test(`Renders with class ${styles.modifiers.animateExpand} hasAnimations is true`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.animateExpand);
});
// Animation context tests for expandable tables
test('respects AnimationsProvider context when no local hasAnimations prop for expandable table', () => {
render(
);
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.animateExpand);
});
test('local hasAnimations prop takes precedence over context for expandable table', () => {
render(
);
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.animateExpand);
});
test('works without AnimationsProvider (backward compatibility) for expandable table', () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.animateExpand);
});
test('Matches snapshot without children', () => {
const { asFragment } = render();
expect(asFragment()).toMatchSnapshot();
});
test('Renders expandable toggle button with pf-m-small class when variant is compact', () => {
render(
);
const toggleButton = screen.getByRole('button', { name: 'Details' });
expect(toggleButton).toHaveClass('pf-m-small');
});
test('Renders expandable toggle button in Th with pf-m-small class when variant is compact', () => {
render(
| {}
}}
aria-label="Row expansion"
/>
| Name |
| {}
}}
/>
| Test content |
);
const toggleButtons = screen.getAllByRole('button');
expect(toggleButtons).toHaveLength(2); // One in Th, one in Td
toggleButtons.forEach((button) => {
expect(button).toHaveClass('pf-m-small');
});
});
test(`Renders with class ${styles.modifiers.plain} when isPlain is true`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.plain);
});
test(`Does not render with class ${styles.modifiers.plain} when isPlain is not defined`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.plain);
});
test(`Does not render with class ${styles.modifiers.plain} when isPlain is false`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.plain);
});
test(`Renders with class ${styles.modifiers.noPlainOnGlass} when isNoPlainOnGlass is true`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.noPlainOnGlass);
});
test(`Does not render with class ${styles.modifiers.noPlainOnGlass} when isNoPlainOnGlass is undefined`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.noPlainOnGlass);
});
test(`Does not render with class ${styles.modifiers.noPlainOnGlass} when isNoPlainOnGlass is false`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.noPlainOnGlass);
});
test(`Renders with class ${styles.modifiers.stickyHeaderBase} when isStickyHeaderBase is true`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.stickyHeaderBase);
});
test(`Does not render with class ${styles.modifiers.stickyHeaderBase} when isStickyHeaderBase is false`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderBase);
});
test(`Does not render with class ${styles.modifiers.stickyHeaderBase} when isStickyHeaderBase is undefined`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderBase);
});
test(`Renders with class ${styles.modifiers.stickyHeaderStuck} when isStickyHeaderStuck is true`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).toHaveClass(styles.modifiers.stickyHeaderStuck);
});
test(`Does not render with class ${styles.modifiers.stickyHeaderStuck} when isStickyHeaderStuck is false`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderStuck);
});
test(`Does not render with class ${styles.modifiers.stickyHeaderStuck} when isStickyHeaderStuck is undefined`, () => {
render();
expect(screen.getByRole('grid', { name: 'Test table' })).not.toHaveClass(styles.modifiers.stickyHeaderStuck);
});