Skip to content

Commit

Permalink
JS unit tests: update popover matcher (#54168)
Browse files Browse the repository at this point in the history
* There was no use of the `toBePositionedPopover()` matcher without a wrapping `getWrappingPopoverElement()`, so this commit just moves `getWrappingPopoverElement` logic into `toBePositionedPopover` to save everyone the trouble.

* - detect whether element.closest( '.components-popover' ) returns an element
- change messaging based on error type

* tidied up null checks and message copy
  • Loading branch information
ramonjd committed Sep 6, 2023
1 parent be2bd33 commit 89a483f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 70 deletions.
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

1 comment on commit 89a483f

@github-actions
Copy link

@github-actions github-actions bot commented on 89a483f Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 89a483f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6092150187
📝 Reported issues:

Please sign in to comment.