import { test, expect } from 'vitest'; import { render, screen, within } from '@testing-library/react'; import WarningNotification from './WarningNotification'; const NOTIFICATION_TEXT = 'Further details of the notification message'; const TITLE_TEXT = 'Important information'; test('warning notification message renders correctly', () => { render( {NOTIFICATION_TEXT} ); const container = document.querySelector('.ds_notification-message'); const heading = screen.getByRole('heading'); const content = heading.nextElementSibling; expect(container?.ariaLive).toEqual('polite'); expect(heading.tagName).toEqual('H3'); expect(heading.textContent).toEqual(TITLE_TEXT) expect(content?.textContent).toEqual(NOTIFICATION_TEXT) }); test('warning notification message with close button', () => { render( {NOTIFICATION_TEXT} ); const closeButton = screen.getByRole('button'); const closeButtonLabel = within(closeButton).getByText('Close this notification'); const closeButtonIcon = within(closeButton).getByRole('img', { hidden: true }); expect(closeButton).toHaveClass('ds_notification-message__close', 'js-close-notification-message'); expect(closeButton).toHaveAttribute('type', 'button'); expect(closeButtonLabel).toBeInTheDocument(); expect(closeButtonLabel).toHaveClass('visually-hidden'); expect(closeButtonIcon).toHaveClass('ds_icon', 'ds_icon--fill'); }); test('warning notification message with icon', () => { render( {NOTIFICATION_TEXT} ); const notificationIcon = screen.getByRole('img', { hidden: true }); expect(notificationIcon).toHaveClass('ds_icon', 'ds_notification-message__icon', 'ds_icon--24'); expect(notificationIcon).toHaveAttribute('aria-hidden'); }); test("does not render body when no children specified", () => { const { container } = render(); expect( container.querySelector(".ds_notification-message__body"), ).not.toBeInTheDocument(); }); test('warning notification message with custom aria live and custom header level', () => { render( ); const container = document.querySelector('.ds_notification-message'); const heading = screen.getByRole('heading'); expect(container?.ariaLive).toEqual('assertive'); expect(heading.tagName).toEqual('H2'); }); test('passing additional props', () => { render( {NOTIFICATION_TEXT} ) const container = document.querySelector('.ds_notification-message') as HTMLElement; expect(container?.dataset.test).toEqual('foo'); }); test('passing additional CSS classes', () => { render( {NOTIFICATION_TEXT} ) const container = document.querySelector('.ds_notification-message') as HTMLElement; expect(container).toHaveClass('foo', 'ds_notification-message'); });