import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useState } from '@wordpress/element';
import type { WidgetType } from '@wordpress/widget-primitives';
import { WidgetDashboard } from '../widget-dashboard';
import type { CanPerformDashboardOperation, DashboardWidget } from '../types';
vi.hoisted( () => {
globalThis.wpVitest.mockMatchMedia();
globalThis.wpVitest.mockCSSSupports();
} );
const widgetTypes: WidgetType[] = [];
// Use a non-empty layout so the provider's auto-edit-when-empty effect
// does not flip editMode on as a side effect of mounting.
const layout: DashboardWidget[] = [
{ uuid: 'a', type: 'core/test', placement: { width: 1, height: 1 } },
];
interface HarnessProps {
initialEditMode?: boolean;
onEditChange?: ( next: boolean ) => void;
onLayoutChange?: ( next: DashboardWidget[] ) => void;
canPerform?: CanPerformDashboardOperation;
layout?: DashboardWidget[];
onLayoutReset?: () => Promise< void >;
}
function Harness( {
initialEditMode = false,
onEditChange,
onLayoutChange = () => {},
canPerform,
layout: initialLayout = layout,
onLayoutReset,
}: HarnessProps ) {
const [ editMode, setEditMode ] = useState( initialEditMode );
const dashboard = (
{
setEditMode( next );
onEditChange?.( next );
} }
onLayoutReset={ onLayoutReset }
>
);
return canPerform ? (
{ dashboard }
) : (
dashboard
);
}
const denyCustomize: CanPerformDashboardOperation = ( request ) =>
request.operation !== 'customize';
const denyReset: CanPerformDashboardOperation = ( request ) =>
request.operation !== 'reset';
describe( 'WidgetDashboard.Actions', () => {
let user: ReturnType< typeof userEvent.setup >;
beforeEach( () => {
user = userEvent.setup();
} );
it( 'renders the Customize button when editMode is false', () => {
render( );
expect(
screen.getByRole( 'button', { name: 'Customize' } )
).toBeInTheDocument();
expect(
screen.queryByRole( 'button', { name: 'Done' } )
).not.toBeInTheDocument();
} );
it( 'renders the Done button when editMode is true', () => {
render( );
expect(
screen.getByRole( 'button', { name: 'Done' } )
).toBeInTheDocument();
expect(
screen.queryByRole( 'button', { name: 'Customize' } )
).not.toBeInTheDocument();
} );
it( 'fires onEditChange with true when Customize is clicked', async () => {
const onEditChange = vi.fn();
render( );
await user.click( screen.getByRole( 'button', { name: 'Customize' } ) );
expect( onEditChange ).toHaveBeenLastCalledWith( true );
} );
it( 'disables Done when there are no staging changes', () => {
render( );
expect(
screen.getByRole( 'button', { name: 'Done' } )
).toHaveAttribute( 'aria-disabled', 'true' );
} );
it( 'fires onEditChange with false when Cancel is clicked', async () => {
const onEditChange = vi.fn();
const onLayoutChange = vi.fn();
render(
);
await user.click( screen.getByRole( 'button', { name: 'Cancel' } ) );
expect( onEditChange ).toHaveBeenLastCalledWith( false );
expect( onLayoutChange ).not.toHaveBeenCalled();
} );
it( 'renders nothing when onEditChange is not provided', () => {
render(
{} }
widgetTypes={ widgetTypes }
>
);
expect(
screen.queryByRole( 'button', { name: 'Customize' } )
).not.toBeInTheDocument();
expect(
screen.queryByRole( 'button', { name: 'Done' } )
).not.toBeInTheDocument();
} );
it( 'does not render a Layout settings button', () => {
render( );
expect(
screen.queryByRole( 'button', { name: 'Layout settings' } )
).not.toBeInTheDocument();
} );
it( 'hides Customize when the policy denies it', () => {
render( );
expect(
screen.queryByRole( 'button', { name: 'Customize' } )
).not.toBeInTheDocument();
expect(
screen.getByRole( 'button', { name: 'More options' } )
).toBeInTheDocument();
} );
it( 'keeps Done and Cancel while in edit mode when customize is denied', () => {
render( );
expect(
screen.getByRole( 'button', { name: 'Done' } )
).toBeInTheDocument();
expect(
screen.getByRole( 'button', { name: 'Cancel' } )
).toBeInTheDocument();
} );
it( 'enters edit mode on an empty layout only when customize is allowed', () => {
const onEditChange = vi.fn();
const { unmount } = render(
);
expect( onEditChange ).toHaveBeenCalledWith( true );
unmount();
onEditChange.mockClear();
render(
);
expect( onEditChange ).not.toHaveBeenCalled();
} );
it( 'offers Reset to default when the policy allows it', async () => {
render( {} } /> );
await user.click(
screen.getByRole( 'button', { name: 'More options' } )
);
expect(
await screen.findByRole( 'menuitem', { name: 'Reset to default…' } )
).toBeInTheDocument();
} );
it( 'hides Reset to default and its menu when the policy denies reset', () => {
render(
{} }
canPerform={ denyReset }
/>
);
expect(
screen.getByRole( 'button', { name: 'Customize' } )
).toBeInTheDocument();
expect(
screen.queryByRole( 'button', { name: 'More options' } )
).not.toBeInTheDocument();
} );
it( 'throws when used outside a WidgetDashboard subtree', () => {
const preventJSDOMError = ( event: ErrorEvent ) => {
event.preventDefault();
};
window.addEventListener( 'error', preventJSDOMError );
try {
expect( () => render( ) ).toThrow(
/Dashboard compound used outside a WidgetDashboard subtree/
);
} finally {
window.removeEventListener( 'error', preventJSDOMError );
}
} );
} );