import { describe, expect, it, vi } from 'vitest'; import { page, userEvent } from 'vitest/browser'; import { screen } from '@testing-library/react'; import { render } from 'vitest-browser-react'; import { createRef, forwardRef } from '@wordpress/element'; import { plusCircle } from '@wordpress/icons'; import _Button from '..'; import Tooltip from '../../tooltip'; import cleanupTooltip from '../../tooltip/test/utils'; vi.mock( import( '../../icon' ), async ( importOriginal ) => ( { ...( await importOriginal() ), default: () =>
, } ) ); const Button = forwardRef( ( props: React.ComponentProps< typeof _Button >, ref: React.ForwardedRef< unknown > ) => <_Button __next40pxDefaultSize { ...props } ref={ ref } /> ); describe( 'Button', () => { describe( 'basic rendering', () => { it( 'should render a button element with only one class', async () => { await render( ); // Move focus to the button await userEvent.tab(); expect( screen.getByRole( 'button' ) ).toHaveClass( 'has-text' ); } ); it( 'should render a button element without has-text when children are not passed', async () => { await render( ); expect( screen.getByRole( 'button' ) ).not.toHaveClass( 'has-text' ); } ); it( 'should render a button element without has-text when children are empty fragment', async () => { await render( ); expect( screen.getByRole( 'button' ) ).not.toHaveClass( 'has-text' ); } ); it( 'should render a button element with has-text when the text is not the first child', async () => { const isBusy = false; await render( ); expect( screen.getByRole( 'button' ) ).toHaveClass( 'has-text' ); } ); it( 'should render a button element with has-text when children are wrapped in an element', async () => { await render( ); expect( screen.getByRole( 'button' ) ).toHaveClass( 'has-text' ); } ); it( 'should render a button element with has-text when children are wrapped in a fragment', async () => { await render( ); expect( screen.getByRole( 'button' ) ).toHaveClass( 'has-text' ); } ); it( 'should render a button element without has-text when every child is falsy', async () => { const isBusy: boolean = false; await render( ); expect( screen.getByRole( 'button' ) ).not.toHaveClass( 'has-text' ); } ); it( 'should render a button element without has-text when a button wrapped in Tooltip', async () => { await render( ); const anchor = screen.getByRole( 'button', { name: 'Tooltip anchor', } ); await userEvent.tab(); expect( anchor ).toHaveFocus(); const tooltip = await screen.findByRole( 'tooltip', { name: 'Tooltip text', } ); expect( tooltip ).toBeVisible(); await userEvent.tab(); expect( screen.getByRole( 'button', { name: 'Focus me' } ) ).toHaveFocus(); await expect .element( page.getByRole( 'tooltip', { name: 'Tooltip text', } ) ) .not.toBeInTheDocument(); } ); it( 'should render correctly as a tooltip anchor, ignoring its internal tooltip in favour of the external tooltip', async () => { await render( <> ); const anchor = screen.getByRole( 'button', { name: 'Button label', } ); await userEvent.tab(); expect( anchor ).toHaveFocus(); const tooltip = await screen.findByRole( 'tooltip', { name: 'Tooltip text', } ); expect( tooltip ).toBeVisible(); // Check that the tooltip that would be normally rendered internally by // the `Button` component is ignored, because of an outer tooltip. expect( screen.queryByRole( 'tooltip', { name: 'Button label', } ) ).not.toBeInTheDocument(); await userEvent.tab(); expect( screen.getByRole( 'button', { name: 'Focus me' } ) ).toHaveFocus(); await expect .element( page.getByRole( 'tooltip', { name: 'Tooltip text', } ) ) .not.toBeInTheDocument(); } ); it( 'should not trash the rendered HTML elements when toggling between showing and not showing a tooltip', async () => { const { rerender } = await render( ); const button = screen.getByRole( 'button', { name: 'Button label', } ); expect( button ).toBeVisible(); await userEvent.tab(); expect( button ).toHaveFocus(); // Re-render the button, but this time change the settings so that it // shows a tooltip. await rerender( ); // The same button element that we referenced before should still be // in the document and have focus. expect( button ).toHaveFocus(); // Re-render the button, but stop showing a tooltip. await rerender( ); // The same button element that we referenced before should still be // in the document and have focus. expect( button ).toHaveFocus(); } ); it( 'should add a disabled prop to the button', async () => { await render( ); expect( screen.getByRole( 'button', { description: 'Description text', } ) ).toBeVisible(); await userEvent.tab(); expect( screen.getByRole( 'button' ) ).toHaveFocus(); await expect .element( page.getByRole( 'tooltip', { name: 'Description text' } ) ) .toBeVisible(); await cleanupTooltip(); } ); it( 'should allow tooltip disable', async () => { await render( ); expect( screen.queryByText( 'WordPress' ) ).not.toBeInTheDocument(); // Move focus to the button await userEvent.tab(); expect( screen.queryByText( 'WordPress' ) ).not.toBeInTheDocument(); } ); it( 'should force showing the tooltip even if icon and children defined', async () => { await render( ); expect( screen.queryByText( 'WordPress' ) ).not.toBeInTheDocument(); // Move focus to the button await userEvent.tab(); expect( screen.getByRole( 'button' ) ).toHaveFocus(); await expect .element( page.getByRole( 'tooltip', { name: 'WordPress' } ) ) .toBeVisible(); await cleanupTooltip(); } ); describe( 'using `aria-pressed` prop', () => { it( 'should render a button element with is-pressed when `true`', async () => { await render(