Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS unit tests: update popover matcher #54168

Merged
merged 3 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ function TestWrapper() {
);
}

/**
* Returns the first found popover element up the DOM tree.
*
* @param {HTMLElement} element Element to start with.
* @return {HTMLElement|null} Popover element, or `null` if not found.
*/
function getWrappingPopoverElement( element ) {
return element.closest( '.components-popover' );
}

describe( 'General media replace flow', () => {
it( 'renders successfully', () => {
render( <TestWrapper /> );
Expand All @@ -67,11 +57,7 @@ describe( 'General media replace flow', () => {
);
const uploadMenu = screen.getByRole( 'menu' );

await waitFor( () =>
expect(
getWrappingPopoverElement( uploadMenu )
).toBePositionedPopover()
);
await waitFor( () => expect( uploadMenu ).toBePositionedPopover() );

await waitFor( () => expect( uploadMenu ).toBeVisible() );
} );
Expand All @@ -92,9 +78,7 @@ describe( 'General media replace flow', () => {
name: 'example.media (opens in a new tab)',
} );

await waitFor( () =>
expect( getWrappingPopoverElement( link ) ).toBePositionedPopover()
);
await waitFor( () => expect( link ).toBePositionedPopover() );

expect( link ).toHaveAttribute( 'href', 'https://example.media' );
} );
Expand All @@ -113,11 +97,9 @@ describe( 'General media replace flow', () => {

await waitFor( () =>
expect(
getWrappingPopoverElement(
screen.getByRole( 'link', {
name: 'example.media (opens in a new tab)',
} )
)
screen.getByRole( 'link', {
name: 'example.media (opens in a new tab)',
} )
).toBePositionedPopover()
);

Expand Down
10 changes: 1 addition & 9 deletions packages/components/src/border-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ function createProps( customProps ) {

const toggleLabelRegex = /Border color( and style)* picker/;

function getWrappingPopoverElement( element ) {
return element.closest( '.components-popover' );
}

const openPopover = async ( user ) => {
const toggleButton = screen.getByLabelText( toggleLabelRegex );
await user.click( toggleButton );
Expand All @@ -51,11 +47,7 @@ const openPopover = async ( user ) => {
name: /^Custom color picker/,
} );

await waitFor( () =>
expect(
getWrappingPopoverElement( pickerButton )
).toBePositionedPopover()
);
await waitFor( () => expect( pickerButton ).toBePositionedPopover() );
};

const getButton = ( name ) => {
Expand Down
8 changes: 1 addition & 7 deletions packages/components/src/color-palette/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ const EXAMPLE_COLORS = [
];
const INITIAL_COLOR = EXAMPLE_COLORS[ 0 ].color;

function getWrappingPopoverElement( element: HTMLElement ) {
return element.closest( '.components-popover' );
}

const ControlledColorPalette = ( {
onChange,
}: {
Expand Down Expand Up @@ -192,9 +188,7 @@ describe( 'ColorPalette', () => {
const dropdownColorInput = screen.getByLabelText( 'Hex color' );

await waitFor( () =>
expect(
getWrappingPopoverElement( dropdownColorInput )
).toBePositionedPopover()
expect( dropdownColorInput ).toBePositionedPopover()
);
} );

Expand Down
10 changes: 1 addition & 9 deletions packages/components/src/toggle-group-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import {
} from '../index';
import type { ToggleGroupControlProps } from '../types';

function getWrappingPopoverElement( element: HTMLElement ) {
return element.closest( '.components-popover' );
}

const ControlledToggleGroupControl = ( {
value: valueProp,
onChange,
Expand Down Expand Up @@ -141,11 +137,7 @@ describe.each( [
'Click for Delicious Gnocchi'
);

await waitFor( () =>
expect(
getWrappingPopoverElement( tooltip )
).toBePositionedPopover()
);
await waitFor( () => expect( tooltip ).toBePositionedPopover() );

expect( tooltip ).toBeVisible();
} );
Expand Down
26 changes: 6 additions & 20 deletions packages/components/src/tooltip/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ const props = {
delay: TOOLTIP_DELAY,
};

function getWrappingPopoverElement( element ) {
return element.closest( '.components-popover' );
}

describe( 'Tooltip', () => {
it( 'should not render the tooltip if multiple children are passed', async () => {
const user = userEvent.setup();
Expand Down Expand Up @@ -75,9 +71,7 @@ describe( 'Tooltip', () => {

// Wait for the tooltip element to be positioned (aligned with the button)
await waitFor( () =>
expect(
getWrappingPopoverElement( screen.getByText( 'tooltip text' ) )
).toBePositionedPopover()
expect( screen.getByText( 'tooltip text' ) ).toBePositionedPopover()
);
} );

Expand All @@ -100,9 +94,7 @@ describe( 'Tooltip', () => {

// Wait for the tooltip element to be positioned (aligned with the button)
await waitFor( () =>
expect(
getWrappingPopoverElement( screen.getByText( 'tooltip text' ) )
).toBePositionedPopover()
expect( screen.getByText( 'tooltip text' ) ).toBePositionedPopover()
);

await user.unhover( button );
Expand Down Expand Up @@ -146,9 +138,7 @@ describe( 'Tooltip', () => {

// Wait for the tooltip element to be positioned (aligned with the button)
await waitFor( () =>
expect(
getWrappingPopoverElement( screen.getByText( 'tooltip text' ) )
).toBePositionedPopover()
expect( screen.getByText( 'tooltip text' ) ).toBePositionedPopover()
);
} );

Expand All @@ -174,9 +164,7 @@ describe( 'Tooltip', () => {

// Wait for the tooltip element to be positioned (aligned with the button)
await waitFor( () =>
expect(
getWrappingPopoverElement( screen.getByText( 'tooltip text' ) )
).toBePositionedPopover()
expect( screen.getByText( 'tooltip text' ) ).toBePositionedPopover()
);
} );

Expand Down Expand Up @@ -282,7 +270,7 @@ describe( 'Tooltip', () => {
// Wait for the tooltip element to be positioned (aligned with the button)
await waitFor( () =>
expect(
getWrappingPopoverElement( screen.getByText( 'shortcut text' ) )
screen.getByText( 'shortcut text' )
).toBePositionedPopover()
);
} );
Expand Down Expand Up @@ -315,9 +303,7 @@ describe( 'Tooltip', () => {

// Wait for the tooltip element to be positioned (aligned with the button)
await waitFor( () =>
expect(
getWrappingPopoverElement( screen.getByText( '⇧⌘,' ) )
).toBePositionedPopover()
expect( screen.getByText( '⇧⌘,' ) ).toBePositionedPopover()
);
} );
} );
1 change: 1 addition & 0 deletions packages/nux/src/components/dot-tip/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe( 'DotTip', () => {
</DotTip>
);

// Wait for the dialog element to be positioned (aligned with the button)
await waitFor( () =>
expect( screen.getByRole( 'dialog' ) ).toBePositionedPopover()
);
Expand Down
12 changes: 10 additions & 2 deletions test/unit/config/matchers/to-be-positioned-popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
* @param {HTMLElement} element Popover element.
*/
function toBePositionedPopover( element ) {
const pass = element.classList.contains( 'is-positioned' );
const popover = element?.closest( '.components-popover' );
const isPopoverPositioned = popover?.classList.contains( 'is-positioned' );
const pass = !! isPopoverPositioned;

return {
pass,
message: () => `Received element is ${ pass ? '' : 'not ' }positioned`,
message: () => {
const is = pass ? 'is' : 'is not';
return ! popover
? `Received element ${ is } a popover element or its descendant.`
: `Received element ${ is } positioned`;
},
};
}

Expand Down