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( );
const button = screen.getByRole( 'button' );
expect( button ).toHaveClass( 'components-button' );
expect( button ).not.toHaveClass( 'is-large' );
expect( button ).not.toHaveClass( 'is-primary' );
expect( button ).not.toHaveClass( 'is-pressed' );
expect( button ).toBeEnabled();
expect( button ).not.toHaveAttribute( 'aria-disabled' );
expect( button ).toHaveAttribute( 'type', 'button' );
} );
it( 'should render a button element with is-primary class', async () => {
await render( );
const button = screen.getByRole( 'button' );
expect( button ).not.toHaveClass( 'is-large' );
expect( button ).toHaveClass( 'is-primary' );
} );
it( 'should render a button element with is-secondary and is-small class', async () => {
await render( );
const button = screen.getByRole( 'button' );
expect( button ).toHaveClass( 'is-secondary' );
expect( button ).not.toHaveClass( 'is-large' );
expect( button ).toHaveClass( 'is-small' );
expect( button ).not.toHaveClass( 'is-primary' );
} );
it( 'should render a button element with is-tertiary class', async () => {
await render( );
const button = screen.getByRole( 'button' );
expect( button ).not.toHaveClass( 'is-large' );
expect( button ).not.toHaveClass( 'is-primary' );
expect( button ).not.toHaveClass( 'is-secondary' );
expect( button ).toHaveClass( 'is-tertiary' );
} );
it( 'should render a button element with is-link class', async () => {
await render( );
const button = screen.getByRole( 'button' );
expect( button ).not.toHaveClass( 'is-primary' );
expect( button ).not.toHaveClass( 'is-secondary' );
expect( button ).not.toHaveClass( 'is-tertiary' );
expect( button ).toHaveClass( 'is-link' );
} );
it( 'should render a button element with has-text when children are passed', 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(
);
expect( screen.getByRole( 'button' ) ).not.toHaveClass(
'has-text'
);
} );
it( 'should render correctly as a tooltip anchor', 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' ) ).toBeDisabled();
} );
it( 'should add only aria-disabled attribute when disabled and isFocusable are true', async () => {
await render( );
const button = screen.getByRole( 'button' );
expect( button ).toBeEnabled();
expect( button ).toHaveAttribute( 'aria-disabled' );
} );
it( 'should not pass the prop target into the element', async () => {
// @ts-expect-error - `target` requires `href`
await render( );
expect( screen.getByRole( 'button' ) ).not.toHaveAttribute(
'target'
);
} );
it( 'should render with an additional className', async () => {
await render( );
expect( screen.getByRole( 'button' ) ).toHaveClass( 'gutenberg' );
} );
it( 'should pass additional props to the element', async () => {
await render( );
expect( screen.getByRole( 'button' ) ).toHaveAttribute(
'type',
'submit'
);
} );
it( 'should render an icon button', async () => {
await render( );
const button = screen.getByRole( 'button' );
expect( button ).toHaveClass( 'has-icon' );
expect( button ).not.toHaveAttribute( 'aria-label' );
} );
it( 'should render a Dashicon component matching the wordpress icon', async () => {
await render( );
expect( screen.getByRole( 'button' ) ).toContainElement(
screen.getByTestId( 'test-icon' )
);
} );
it( 'should render child elements and icon', async () => {
await render(