import React from 'react'; import { cleanup, render } from '@testing-library/react'; import { axe } from 'jest-axe'; import { Variant } from '../Toast.types'; import { InternalToast } from './InternalToast'; import { InternalToastProps } from './InternalToast.types'; /** * This suite checks the rendering of a toast. * * Does not test interactivity * (see `ToastContext.spec.tsx`) * * Does not check controlled rendering * (see `ControlledToast.spec`) */ describe('packages/toast/internal-toast', () => { afterEach(cleanup); describe('a11y', () => { test('does not have basic accessibility issues', async () => { const { container } = render( , ); const results = await axe(container); expect(results).toHaveNoViolations(); }); }); describe('`variant` prop', () => { describe(`when 'variant' is 'progress', the progress bar renders`, () => { test(`when 'progress' is undefined`, () => { const { queryByRole } = render( , ); const progressBar = queryByRole('progressbar'); expect(progressBar).toBeVisible(); }); test(`when 'progress' is '0'`, () => { const { queryByRole } = render( , ); const progressBar = queryByRole('progressbar'); expect(progressBar).toBeVisible(); }); test(`when 'progress' is '0.5'`, () => { const { queryByRole } = render( , ); const progressBar = queryByRole('progressbar'); expect(progressBar).toBeVisible(); }); }); describe(`when 'variant' is not 'progress', the progress bar does not render`, () => { test(`when 'progress' is undefined`, () => { const { queryByRole } = render( , ); const progressBar = queryByRole('progressbar'); expect(progressBar).not.toBeInTheDocument(); }); test(`when 'progress' is set`, () => { const { queryByRole } = render( , ); const progressBar = queryByRole('progressbar'); expect(progressBar).not.toBeInTheDocument(); }); }); describe('the correct icon is rendered', () => { const expectedVariantIcons: Record = { [Variant.Success]: 'success notification', [Variant.Note]: 'note notification', [Variant.Warning]: 'warning notification', [Variant.Important]: 'important notification', [Variant.Progress]: 'progress notification', }; test.each(Object.values(Variant) as Array)( `when 'variant' is '%s'`, variant => { const { getByLabelText } = render( , ); expect(getByLabelText(expectedVariantIcons[variant])).toBeVisible(); }, ); }); }); describe('`title` prop', () => { test('a string, the title element renders', () => { const titleText = 'this is the title'; const { queryByText } = render( , ); const body = queryByText(titleText); expect(body).toBeVisible(); }); test('a JSX element, the title element renders', () => { const titleText = 'this is the title'; const titleElement = {titleText}; const { queryByTestId } = render( , ); const titleSpan = queryByTestId('test-span'); expect(titleSpan).toBeVisible(); }); }); describe('`description` prop', () => { test('a string, the title element renders', () => { const bodyText = 'this is the title'; const { queryByText } = render( , ); const body = queryByText(bodyText); expect(body).toBeVisible(); }); test('a JSX element, the body element renders', () => { const bodyText = 'this is the body'; const bodyElement = {bodyText}; const { queryByTestId } = render( , ); const bodySpan = queryByTestId('test-span'); expect(bodySpan).toBeVisible(); }); }); describe('`actionElement` prop', () => { test('Renders `actionElement` when `variant === progress`', () => { const { queryByTestId } = render( action} />, ); const actionButton = queryByTestId('action'); expect(actionButton).toBeInTheDocument(); }); test('`actionElement` not rendered when `variant !== progress`', () => { const { queryByTestId } = render( action} />, ); const actionButton = queryByTestId('action'); expect(actionButton).not.toBeInTheDocument(); }); }); }); // eslint-disable-next-line jest/no-disabled-tests describe.skip('TS types behave as expected', () => { // eslint-disable-next-line jest/no-disabled-tests test.skip('Requires props', () => { <> {/* @ts-expect-error - title, open & onClose={are required */} {/* @ts-expect-error - title is required */} {}} /> {/* @ts-expect-error - `open` is not a valid prop on internal toast */} {}} /> ; }); const requiredProps = { title: 'string', } as InternalToastProps; // eslint-disable-next-line jest/no-disabled-tests test.skip('has correct props', () => { <> ; }); });