Skip to content
Merged
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
71 changes: 42 additions & 29 deletions packages/components/src/tooltip/test/index.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ const hoverOutside = async () => {
await userEvent.hover( document.body );
};

const sleep = ( milliseconds: number ) =>
new Promise( ( resolve ) => setTimeout( resolve, milliseconds ) );

describe( 'Tooltip', () => {
describe( 'basic behavior', () => {
it( 'should not render the tooltip if multiple children are passed', async () => {
Expand Down Expand Up @@ -284,41 +281,57 @@ describe( 'Tooltip', () => {
const ADDITIONAL_DELAY = 500;

await render(
<Tooltip
{ ...props }
delay={ TOOLTIP_DELAY + ADDITIONAL_DELAY }
/>
<>
<button
style={ {
position: 'fixed',
right: 0,
bottom: 0,
} }
>
Pointer parking target
</button>
<Tooltip
{ ...props }
delay={ TOOLTIP_DELAY + ADDITIONAL_DELAY }
/>
</>
);

const anchor = screen.getByRole( 'button', {
name: 'Tooltip anchor',
} );
expect( anchor ).toBeVisible();

// Hover over the anchor
await userEvent.hover( anchor );
expectTooltipToBeHidden();

// Advance time by default delay, which is still well before the
// custom delay even when CI is slow to resolve the hover event.
await sleep( TOOLTIP_DELAY );

// Tooltip hasn't appeared yet
expectTooltipToBeHidden();

// Wait for additional delay for tooltip to appear
await sleep( ADDITIONAL_DELAY );
await waitExpectTooltipToShow();
await userEvent.hover(
screen.getByRole( 'button', {
name: 'Pointer parking target',
} )
);
// Keep browser command latency from consuming the hover delay.
vi.useFakeTimers( { toFake: [ 'setTimeout', 'clearTimeout' ] } );
try {
await userEvent.hover( anchor );
expectTooltipToBeHidden();

// Hover outside of the anchor, tooltip should hide
await hoverOutside();
await waitExpectTooltipToHide();
// One millisecond short of the custom delay, which is past the
// default one: the tooltip hasn't appeared yet.
await act( async () => {
vi.advanceTimersByTime(
TOOLTIP_DELAY + ADDITIONAL_DELAY - 1
);
} );
expectTooltipToBeHidden();

// Prevent this test from interfering with the next one.
// "Tooltips appear instantly if another tooltip has just been hidden."
// See: https://github.com/WordPress/gutenberg/pull/57345#discussion_r1435495655
await sleep( 3000 );
}, 10_000 );
// The custom delay has elapsed: the tooltip appears.
await act( async () => {
vi.advanceTimersByTime( 1 );
} );
expectTooltipToBeVisible();
} finally {
vi.useRealTimers();
}
} );

it( 'should not show tooltip if the mouse leaves the tooltip anchor before set delay', async () => {
const onMouseEnterMock = vi.fn();
Expand Down
Loading