import { render, screen, userEvent } from '../test-utils';
import CardContainer from './CardContainer';
import type { CardContainerSpotlight } from './CardContainer.types';
describe('CardContainer', () => {
const content = 'Container content';
describe('rendering', () => {
it('renders children', () => {
render({content});
expect(screen.getByText(content)).toBeInTheDocument();
});
it('renders as a div by default', () => {
render({content});
expect(screen.getByTestId('container').tagName).toBe('DIV');
});
it.each(['section', 'article'] as const)('renders as %s when as is provided', (as) => {
render(
{content}
,
);
expect(screen.getByTestId('container').tagName).toBe(as.toUpperCase());
});
it('applies className to the root element', () => {
render(
{content}
,
);
expect(screen.getByTestId('container')).toHaveClass('wds-card-container', 'custom-class');
});
it('spreads unknown props onto the root element', () => {
render(
{content}
,
);
expect(screen.getByTestId('container')).toHaveAttribute('aria-label', 'Cards');
});
});
describe('padding', () => {
it('has no padding modifier by default', () => {
render({content});
expect(screen.getByTestId('container')).not.toHaveClass('wds-card-container--padding-none');
});
it('renders the padding-none modifier', () => {
render(
{content}
,
);
expect(screen.getByTestId('container')).toHaveClass('wds-card-container--padding-none');
});
it('keeps the dashed border for the inactive spotlight without padding', () => {
render(
{content}
,
);
expect(
screen.getByTestId('container').querySelector('.wds-card-container__border'),
).toBeInTheDocument();
});
});
describe('spotlight', () => {
it('has no spotlight modifier by default', () => {
render({content});
const root = screen.getByTestId('container');
expect(root).not.toHaveClass('wds-card-container--active');
expect(root).not.toHaveClass('wds-card-container--inactive');
});
it.each(['active', 'inactive'])(
'renders the %s spotlight modifier',
(spotlight) => {
render(
{content}
,
);
expect(screen.getByTestId('container')).toHaveClass(`wds-card-container--${spotlight}`);
},
);
it('draws a decorative dashed border for the inactive spotlight', () => {
render(
{content}
,
);
expect(
screen.getByTestId('container').querySelector('.wds-card-container__border'),
).toHaveAttribute('aria-hidden', 'true');
});
it.each(['active', undefined])(
'draws no dashed border for spotlight %s',
(spotlight) => {
render(
{content}
,
);
const root = screen.getByTestId('container');
expect(root.querySelector('.wds-card-container__border')).not.toBeInTheDocument();
},
);
});
describe('interactive', () => {
it('is not interactive by default', () => {
render({content});
expect(screen.getByTestId('container')).not.toHaveClass('wds-card-container--interactive');
});
it('adds the interactive modifier without changing the element', () => {
render(
{content}
,
);
const root = screen.getByTestId('container');
expect(root.tagName).toBe('DIV');
expect(root).toHaveClass('wds-card-container--interactive');
expect(root).not.toHaveAttribute('interactive');
});
it('spreads onClick onto the root element as a plain handler', async () => {
const onClick = jest.fn();
render(
{content}
,
);
const root = screen.getByTestId('container');
expect(root.tagName).toBe('DIV');
await userEvent.click(root);
expect(onClick).toHaveBeenCalledTimes(1);
});
});
});