import { Plus, Settings } from '@transferwise/icons'; import { mockMatchMedia, render, screen, userEvent } from '../test-utils'; import CardWidget from './CardWidget'; import type { CardWidgetHeader } from './CardWidget.types'; mockMatchMedia(); describe('CardWidget', () => { const content = 'Widget content'; const renderWidget = (header: Partial = {}) => render( {content} , ); describe('rendering', () => { it('renders children without a header', () => { render({content}); expect(screen.getByText(content)).toBeInTheDocument(); expect(screen.queryByRole('heading')).not.toBeInTheDocument(); expect(screen.getByTestId('widget')).not.toHaveClass('wds-card-widget--with-header'); }); it('renders on a CardContainer', () => { render( {content} , ); const root = screen.getByTestId('widget'); expect(root.tagName).toBe('ARTICLE'); expect(root).toHaveClass( 'wds-card-container', 'wds-card-container--active', 'wds-card-widget', 'custom', ); }); it('always keeps the default padding', () => { // `padding` is not a CardWidget prop, but untyped consumers can still pass it. const untypedProps = { padding: 'none' } as object; render( {content} , ); expect(screen.getByTestId('widget')).not.toHaveClass('wds-card-container--padding-none'); }); it('renders the title as an h3 by default', () => { renderWidget(); expect(screen.getByRole('heading', { level: 3, name: 'Cards' })).toBeInTheDocument(); expect(screen.getByTestId('widget')).toHaveClass('wds-card-widget--with-header'); }); it('renders the title as the given heading level', () => { renderWidget({ as: 'h2' }); expect(screen.getByRole('heading', { level: 2, name: 'Cards' })).toBeInTheDocument(); }); it('renders the header without content', () => { render(); expect(screen.getByRole('heading', { name: 'Cards' })).toBeInTheDocument(); expect(document.querySelector('.wds-card-widget__content')).not.toBeInTheDocument(); }); }); describe('titleAction', () => { it('renders the title as a link inside the heading', () => { renderWidget({ titleAction: { href: '/cards' } }); const link = screen.getByRole('link', { name: 'Cards' }); expect(link).toHaveAttribute('href', '/cards'); expect(screen.getByRole('heading', { level: 3 })).toContainElement(link); }); it('calls the titleAction onClick handler', async () => { const onClick = jest.fn(); renderWidget({ titleAction: { href: '#cards', onClick } }); await userEvent.click(screen.getByRole('link', { name: 'Cards' })); expect(onClick).toHaveBeenCalledTimes(1); }); it('renders the title as a button when titleAction has only onClick', async () => { const onClick = jest.fn(); renderWidget({ titleAction: { onClick } }); await userEvent.click(screen.getByRole('button', { name: 'Cards' })); expect(onClick).toHaveBeenCalledTimes(1); }); it('marks a title link that opens in a new tab', () => { renderWidget({ titleAction: { href: '/cards', target: '_blank' } }); expect(screen.getByRole('link', { name: /Cards/u })).toHaveAttribute('rel', 'noreferrer'); }); it('keeps the title link alongside actions', () => { renderWidget({ titleAction: { href: '/cards' }, action: { label: 'Order card' } }); expect(screen.getByRole('link', { name: 'Cards' })).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Order card' })).toBeInTheDocument(); }); }); describe('states', () => { it("always follows the title link, instead of the CardContainer's own states", () => { // `interactive` is not a CardWidget prop, but untyped consumers can still pass it. const untypedProps = { interactive: true } as object; render( {content} , ); const root = screen.getByTestId('widget'); expect(root).not.toHaveClass('wds-card-container--interactive'); expect(root).not.toHaveAttribute('interactive'); }); }); describe('actions', () => { it('renders a labelled action as a button', () => { renderWidget({ action: { label: 'Order card' } }); expect(screen.getByRole('button', { name: 'Order card' })).toBeInTheDocument(); }); it('renders a labelled action with an href as a link', () => { renderWidget({ action: { label: 'Order card', href: '/cards/order' } }); expect(screen.getByRole('link', { name: 'Order card' })).toHaveAttribute( 'href', '/cards/order', ); }); it('renders a labelled action icon after the label by default', () => { renderWidget({ action: { label: 'Order card', icon: } }); expect(screen.getByTestId('action-icon').parentElement).toHaveClass('wds-Button-icon--end'); }); it('renders a labelled action icon before the label with iconPosition start', () => { renderWidget({ action: { label: 'Order card', icon: , iconPosition: 'start', }, }); expect(screen.getByTestId('action-icon').parentElement).toHaveClass('wds-Button-icon--start'); }); it('names an unlabelled action from its aria-label', () => { renderWidget({ action: { 'aria-label': 'Card settings', icon: } }); expect(screen.getByRole('button', { name: 'Card settings' })).toBeInTheDocument(); }); it('calls the action onClick handler', async () => { const onClick = jest.fn(); renderWidget({ action: { label: 'Order card', onClick } }); await userEvent.click(screen.getByRole('button', { name: 'Order card' })); expect(onClick).toHaveBeenCalledTimes(1); }); it('does not submit a surrounding form', async () => { const onSubmit = jest.fn((event: React.FormEvent) => event.preventDefault()); render(
, ); const button = screen.getByRole('button', { name: 'Order card' }); expect(button).toHaveAttribute('type', 'button'); await userEvent.click(button); expect(onSubmit).not.toHaveBeenCalled(); }); it('does not call onClick when disabled', async () => { const onClick = jest.fn(); renderWidget({ action: { label: 'Order card', disabled: true, onClick } }); await userEvent.click(screen.getByRole('button', { name: 'Order card' })); expect(onClick).not.toHaveBeenCalled(); }); it('renders actionSecondary before action in the DOM', () => { renderWidget({ action: { label: 'Order card' }, actionSecondary: { label: 'Manage' } }); const [first, second] = screen.getAllByRole('button'); expect(first).toHaveAccessibleName('Manage'); expect(second).toHaveAccessibleName('Order card'); }); it('takes button priority from the slot the action is passed to', () => { renderWidget({ action: { label: 'Order card' }, actionSecondary: { label: 'Manage' } }); expect(screen.getByRole('button', { name: 'Order card' })).toHaveClass( 'wds-Button--secondary', ); expect(screen.getByRole('button', { name: 'Manage' })).toHaveClass( 'wds-Button--secondary-neutral', ); }); it('takes icon button priority from the slot the action is passed to', () => { renderWidget({ action: { 'aria-label': 'Add', icon: }, actionSecondary: { 'aria-label': 'Settings', icon: }, }); expect(screen.getByRole('button', { name: 'Add' })).toHaveClass( 'np-icon-button-secondary-default', ); expect(screen.getByRole('button', { name: 'Settings' })).toHaveClass( 'np-icon-button-tertiary-default', ); }); it('keeps the lower emphasis when actionSecondary is used on its own', () => { renderWidget({ actionSecondary: { label: 'Manage' } }); expect(screen.getByRole('button', { name: 'Manage' })).toHaveClass( 'wds-Button--secondary-neutral', ); }); it('renders an arbitrary node in place of the action buttons', () => { renderWidget({ action: Custom node }); expect(screen.getByText('Custom node')).toBeInTheDocument(); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); it('renders an element as given, even when it has a label prop', () => { const CustomAction = ({ label }: { label: string }) => {label}; renderWidget({ action: }); expect(screen.getByText('Custom action')).toBeInTheDocument(); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); }); });