import { describe, expect, it, vi } from 'vitest'; import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { createRef } from '@wordpress/element'; import { speak } from '@wordpress/a11y'; import * as Notice from '../index'; vi.mock( import( '@wordpress/a11y' ), () => ( { speak: vi.fn(), } ) ); describe( 'Notice', () => { describe( 'basic behaviour', () => { it( 'forwards ref', () => { const rootRef = createRef< HTMLDivElement >(); const titleRef = createRef< HTMLSpanElement >(); const descriptionRef = createRef< HTMLSpanElement >(); const actionsRef = createRef< HTMLDivElement >(); const actionButtonRef = createRef< HTMLButtonElement >(); const actionLinkRef = createRef< HTMLAnchorElement >(); const closeIconButtonRef = createRef< HTMLButtonElement >(); render( Title Test Action Button Action Link ); expect( rootRef.current ).toBeInstanceOf( HTMLDivElement ); expect( titleRef.current ).toBeInstanceOf( HTMLSpanElement ); expect( descriptionRef.current ).toBeInstanceOf( HTMLSpanElement ); expect( actionsRef.current ).toBeInstanceOf( HTMLDivElement ); expect( actionButtonRef.current ).toBeInstanceOf( HTMLButtonElement ); expect( actionLinkRef.current ).toBeInstanceOf( HTMLAnchorElement ); expect( closeIconButtonRef.current ).toBeInstanceOf( HTMLButtonElement ); } ); it( 'renders content', () => { render( Update Available A new version is ready to install. ); expect( screen.getByText( 'Update Available' ) ).toBeVisible(); expect( screen.getByText( 'A new version is ready to install.' ) ).toBeVisible(); } ); } ); describe( 'dismissing via CloseIconButton', () => { it( 'renders dismiss button when CloseIconButton included', async () => { const user = userEvent.setup(); const handleDismiss = vi.fn(); render( Dismissible ); await user.click( screen.getByRole( 'button', { name: 'Dismiss' } ) ); expect( handleDismiss ).toHaveBeenCalledTimes( 1 ); } ); it( 'does not render dismiss button when CloseIconButton omitted', () => { render( Non-dismissible ); expect( screen.queryByRole( 'button', { name: 'Dismiss' } ) ).not.toBeInTheDocument(); } ); it( 'supports custom CloseIconButton label', () => { render( Test ); expect( screen.getByRole( 'button', { name: 'Close notification' } ) ).toBeVisible(); } ); } ); describe( 'decorative icon', () => { it( 'renders icon for non-neutral intents', () => { const { container } = render( Info message ); // Disable reason: The decorative SVG has aria-hidden and no role, // so there is no Testing Library query that can target it. // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access const icon = container.querySelector( 'svg' ); expect( icon ).toBeVisible(); expect( icon ).toHaveAttribute( 'aria-hidden', 'true' ); } ); it( 'does not render icon for neutral intent', () => { const { container } = render( Neutral message ); // Disable reason: The decorative SVG has aria-hidden and no role, // so there is no Testing Library query that can target it. // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access expect( container.querySelector( 'svg' ) ).not.toBeInTheDocument(); } ); it( 'hides icon when icon prop is null', () => { const { container } = render( No icon ); // Disable reason: The decorative SVG has aria-hidden and no role, // so there is no Testing Library query that can target it. // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access expect( container.querySelector( 'svg' ) ).not.toBeInTheDocument(); } ); } ); describe( 'actions', () => { it( 'renders ActionButton, ActionButtonLink and ActionLink', async () => { const user = userEvent.setup(); const handleClick = vi.fn(); render( Notice with actions Retry View status Learn more ); await user.click( screen.getByRole( 'button', { name: 'Retry' } ) ); expect( handleClick ).toHaveBeenCalledTimes( 1 ); expect( screen.getByRole( 'link', { name: 'View status' } ) ).toHaveAttribute( 'href', '/status' ); expect( screen.getByRole( 'link', { name: 'Learn more' } ) ).toHaveAttribute( 'href', '/docs' ); } ); } ); describe( 'screen reader announcements', () => { it( 'renders without announcing its content', () => { const { container } = render( Something failed. ); expect( speak ).not.toHaveBeenCalled(); // Disable reason: no accessible query can find an element by an // `aria-live` attribute when that element has no live-region role. expect( // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access container.querySelector( '[aria-live], [role="alert"], [role="status"]' ) ).not.toBeInTheDocument(); } ); } ); } );