|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import '@testing-library/jest-dom'; |
| 5 | +import { render, screen } from '@testing-library/react'; |
| 6 | +import userEvent from '@testing-library/user-event'; |
| 7 | + |
| 8 | +/** |
| 9 | + * WordPress dependencies |
| 10 | + */ |
| 11 | +import { useState } from '@wordpress/element'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Internal dependencies |
| 15 | + */ |
| 16 | +import { WidgetDashboard } from '../widget-dashboard'; |
| 17 | +import type { DashboardWidget, WidgetType } from '../types'; |
| 18 | + |
| 19 | +const widgetTypes: WidgetType[] = []; |
| 20 | +const layout: DashboardWidget[] = []; |
| 21 | + |
| 22 | +interface HarnessProps { |
| 23 | + initialEditMode?: boolean; |
| 24 | + onEditChange?: ( next: boolean ) => void; |
| 25 | +} |
| 26 | + |
| 27 | +function Harness( { initialEditMode = false, onEditChange }: HarnessProps ) { |
| 28 | + const [ editMode, setEditMode ] = useState( initialEditMode ); |
| 29 | + |
| 30 | + return ( |
| 31 | + <WidgetDashboard |
| 32 | + layout={ layout } |
| 33 | + onLayoutChange={ () => {} } |
| 34 | + widgetTypes={ widgetTypes } |
| 35 | + editMode={ editMode } |
| 36 | + onEditChange={ ( next ) => { |
| 37 | + setEditMode( next ); |
| 38 | + onEditChange?.( next ); |
| 39 | + } } |
| 40 | + > |
| 41 | + <WidgetDashboard.Actions /> |
| 42 | + </WidgetDashboard> |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +describe( 'WidgetDashboard.Actions', () => { |
| 47 | + it( 'renders the Customize button when editMode is false', () => { |
| 48 | + render( <Harness /> ); |
| 49 | + |
| 50 | + expect( |
| 51 | + screen.getByRole( 'button', { name: 'Customize' } ) |
| 52 | + ).toBeInTheDocument(); |
| 53 | + expect( |
| 54 | + screen.queryByRole( 'button', { name: 'Done' } ) |
| 55 | + ).not.toBeInTheDocument(); |
| 56 | + } ); |
| 57 | + |
| 58 | + it( 'renders the Done button when editMode is true', () => { |
| 59 | + render( <Harness initialEditMode /> ); |
| 60 | + |
| 61 | + expect( |
| 62 | + screen.getByRole( 'button', { name: 'Done' } ) |
| 63 | + ).toBeInTheDocument(); |
| 64 | + expect( |
| 65 | + screen.queryByRole( 'button', { name: 'Customize' } ) |
| 66 | + ).not.toBeInTheDocument(); |
| 67 | + } ); |
| 68 | + |
| 69 | + it( 'fires onEditChange with the toggled value on click', async () => { |
| 70 | + const onEditChange = jest.fn(); |
| 71 | + render( <Harness onEditChange={ onEditChange } /> ); |
| 72 | + |
| 73 | + await userEvent.click( |
| 74 | + screen.getByRole( 'button', { name: 'Customize' } ) |
| 75 | + ); |
| 76 | + expect( onEditChange ).toHaveBeenLastCalledWith( true ); |
| 77 | + |
| 78 | + await userEvent.click( screen.getByRole( 'button', { name: 'Done' } ) ); |
| 79 | + expect( onEditChange ).toHaveBeenLastCalledWith( false ); |
| 80 | + expect( onEditChange ).toHaveBeenCalledTimes( 2 ); |
| 81 | + |
| 82 | + // TODO: drop once Done has its own committed behavior; today it logs. |
| 83 | + // eslint-disable-next-line jest/no-standalone-expect |
| 84 | + expect( console ).toHaveLogged( 'done' ); |
| 85 | + } ); |
| 86 | + |
| 87 | + it( 'renders nothing when onEditChange is not provided', () => { |
| 88 | + render( |
| 89 | + <WidgetDashboard |
| 90 | + layout={ layout } |
| 91 | + onLayoutChange={ () => {} } |
| 92 | + widgetTypes={ widgetTypes } |
| 93 | + > |
| 94 | + <WidgetDashboard.Actions /> |
| 95 | + </WidgetDashboard> |
| 96 | + ); |
| 97 | + |
| 98 | + expect( |
| 99 | + screen.queryByRole( 'button', { name: 'Customize' } ) |
| 100 | + ).not.toBeInTheDocument(); |
| 101 | + expect( |
| 102 | + screen.queryByRole( 'button', { name: 'Done' } ) |
| 103 | + ).not.toBeInTheDocument(); |
| 104 | + } ); |
| 105 | + |
| 106 | + it( 'throws when used outside a WidgetDashboard subtree', () => { |
| 107 | + const spy = jest |
| 108 | + .spyOn( console, 'error' ) |
| 109 | + .mockImplementation( () => {} ); |
| 110 | + |
| 111 | + expect( () => render( <WidgetDashboard.Actions /> ) ).toThrow( |
| 112 | + /Dashboard compound used outside a WidgetDashboard subtree/ |
| 113 | + ); |
| 114 | + |
| 115 | + spy.mockRestore(); |
| 116 | + } ); |
| 117 | +} ); |
0 commit comments