import { mockMatchMedia, render, screen, within } from '../../test-utils';
import { PrimitivePrompt, PrimitivePromptProps } from './PrimitivePrompt';
import { userEvent } from '@testing-library/user-event';
mockMatchMedia();
describe('PrimitivePrompt', () => {
const defaultProps: PrimitivePromptProps = {
media: Media,
children:
Test content
,
};
beforeEach(() => {
jest.clearAllMocks();
});
describe('Basic rendering', () => {
it('renders children content', () => {
render();
expect(screen.getByText('Test content')).toBeInTheDocument();
});
it('renders media', () => {
render();
expect(screen.getByTestId('test-media')).toBeInTheDocument();
});
it('renders actions when provided', () => {
render(
Action Button}
/>,
);
expect(screen.getByText('Action Button')).toBeInTheDocument();
});
it('does not render actions wrapper when actions not provided', () => {
render();
const prompt = screen.getByTestId('prompt');
expect(within(prompt).queryByRole('button')).not.toBeInTheDocument();
});
});
describe('Sentiments', () => {
[
{ sentiment: 'negative' as const, label: 'negative' },
{ sentiment: 'warning' as const, label: 'warning' },
{ sentiment: 'neutral' as const, label: 'neutral' },
{ sentiment: 'success' as const, label: 'success' },
].forEach(({ sentiment, label }) => {
it(`applies correct CSS class for ${label} sentiment`, () => {
render();
const promptElement = screen.getByTestId('prompt');
expect(promptElement).toHaveClass(`wds-prompt--${label}`);
});
});
it('defaults to success sentiment', () => {
render();
expect(screen.getByTestId('prompt')).toHaveClass('wds-prompt--success');
});
it('applies base wds-prompt class', () => {
render();
expect(screen.getByTestId('prompt')).toHaveClass('wds-prompt');
});
});
describe('Dismiss functionality', () => {
it('renders dismiss button when onDismiss is provided', () => {
const onDismiss = jest.fn();
render();
expect(screen.getByLabelText('Close')).toBeInTheDocument();
});
it('calls onDismiss when dismiss button is clicked', async () => {
const onDismiss = jest.fn();
const user = userEvent.setup();
render();
const dismissButton = screen.getByLabelText('Close');
await user.click(dismissButton);
expect(onDismiss).toHaveBeenCalledTimes(1);
});
it('does not render dismiss button when onDismiss is not provided', () => {
render();
expect(screen.queryByLabelText('Close')).not.toBeInTheDocument();
});
it('applies with-dismiss class when onDismiss is provided', () => {
render( {}} />);
const prompt = screen.getByTestId('prompt');
expect(screen.getByLabelText('Close')).toBeInTheDocument();
expect(prompt).toBeInTheDocument();
});
it('does not apply with-dismiss class when onDismiss is not provided', () => {
render();
expect(screen.queryByLabelText('Close')).not.toBeInTheDocument();
});
});
describe('Custom attributes', () => {
it('applies custom className', () => {
render();
expect(screen.getByTestId('prompt')).toHaveClass('custom-class');
});
it('applies custom id', () => {
render();
expect(screen.getByTestId('prompt')).toHaveAttribute('id', 'custom-id');
});
it('applies custom data-testid', () => {
render();
expect(screen.getByTestId('custom-test-id')).toBeInTheDocument();
});
it('combines custom className with base classes', () => {
render(
,
);
const prompt = screen.getByTestId('prompt');
expect(prompt).toHaveClass('wds-prompt');
expect(prompt).toHaveClass('wds-prompt--warning');
expect(prompt).toHaveClass('custom-class');
});
});
describe('SentimentSurface integration', () => {
it('uses SentimentSurface for sentiment styling', () => {
render();
const prompt = screen.getByTestId('prompt');
expect(prompt).toHaveClass('wds-sentiment-surface');
});
it('passes sentiment to SentimentSurface', () => {
render();
const prompt = screen.getByTestId('prompt');
expect(prompt).toHaveClass('wds-prompt--warning');
});
});
});