import { describe, expect, it } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Tabs, TabsContent, TabsList, TabsTrigger } from './tabs';
const Example = () => (
Overview
Activity
Billing
Overview panel
Activity panel
Billing panel
);
describe('Tabs', () => {
/*
* The list carries the shape and the trigger reads it back through
* `group-data-*`, so these attributes are the contract between the two —
* dropping one silently falls the triggers back to the default look.
*/
describe('shape is published on the list', () => {
it('exposes the variant and size the triggers style themselves from', () => {
render(
A
Panel
);
const list = screen.getByRole('tablist');
expect(list).toHaveAttribute('data-variant', 'outline');
expect(list).toHaveAttribute('data-size', 'lg');
expect(list).toHaveClass('palette-red');
});
it('defaults to CBAR’s line strip on the secondary palette', () => {
render();
const list = screen.getByRole('tablist');
expect(list).toHaveAttribute('data-variant', 'line');
expect(list).toHaveAttribute('data-size', 'md');
expect(list).toHaveClass('palette-secondary');
});
});
it('renders only the selected panel', () => {
render();
expect(screen.getByText('Overview panel')).toBeInTheDocument();
expect(screen.queryByText('Activity panel')).not.toBeInTheDocument();
});
it('switches panels on click', async () => {
render();
await userEvent.click(screen.getByRole('tab', { name: 'Activity' }));
expect(screen.getByText('Activity panel')).toBeInTheDocument();
expect(screen.queryByText('Overview panel')).not.toBeInTheDocument();
});
it('moves between tabs with the arrow keys', async () => {
render();
await userEvent.click(screen.getByRole('tab', { name: 'Overview' }));
await userEvent.keyboard('{ArrowRight}');
expect(screen.getByRole('tab', { name: 'Activity' })).toHaveAttribute(
'aria-selected',
'true'
);
});
it('skips a disabled tab', async () => {
render();
await userEvent.click(screen.getByRole('tab', { name: 'Billing' }));
expect(screen.getByText('Overview panel')).toBeInTheDocument();
});
});