Skip to content
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
68 changes: 28 additions & 40 deletions packages/components/src/color-picker/test/index.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,23 @@ describe( 'ColorPicker', () => {
const bounds = element.getBoundingClientRect();
expect( bounds.width ).toBeGreaterThan( 0 );
expect( bounds.height ).toBeGreaterThan( 0 );
const clientX = bounds.left + bounds.width * x;
const clientY = bounds.top + bounds.height * y;
return {
clientX,
clientY,
pageX: clientX + window.scrollX,
pageY: clientY + window.scrollY,
};
return { x: bounds.width * x, y: bounds.height * y };
};

const dragToBlack = async ( element: HTMLElement ) => {
// Black is at the bottom boundary, just outside the slider's hit area.
// Release over the picker below it so native mouse movement reaches v = 0.
// eslint-disable-next-line testing-library/no-node-access -- The picker background has no accessible role.
const picker = element.closest< HTMLElement >( '.react-colorful' )!;
const bounds = element.getBoundingClientRect();
const pickerBounds = picker.getBoundingClientRect();
await userEvent.dragAndDrop( element, picker, {
sourcePosition: pointAt( element, 0.8, 0.5 ),
targetPosition: {
x: bounds.left - pickerBounds.left + bounds.width * 0.8,
y: bounds.bottom - pickerBounds.top + 1,
},
} );
};

const getPointerPosition = ( pointer: HTMLElement ) => {
Expand Down Expand Up @@ -686,11 +695,15 @@ describe( 'ColorPicker', () => {
) as HTMLElement;
expect( parseFloat( pointer.style.left ) ).toBeGreaterThan( 50 );

// Click the bottom of the saturation surface (v = 0) at ~80% saturation.
// Exact black is on the lower boundary, outside the native hit area.
// Supply that coordinate without movement to retain click-only coverage.
const bounds = interactive.getBoundingClientRect();
fireEvent.mouseDown( interactive, {
clientX: bounds.left + bounds.width * 0.8,
clientY: bounds.bottom,
buttons: 1,
...pointAt( interactive, 0.8, 1 ),
} );
fireEvent.mouseUp( interactive );

expectPointerPosition( pointer, { top: 100 } );
// Lossy HSVA→HSLA→HSVA sync would snap left to 0%.
Expand All @@ -716,30 +729,7 @@ describe( 'ColorPicker', () => {
'.react-colorful__saturation-pointer'
) as HTMLElement;

// Full pointer lifecycle so isPointerInteractingRef is exercised
// and prevHslaRef stays in sync across mid-gesture parent updates.
fireEvent.pointerDown( interactive, {
pointerId: 1,
buttons: 1,
...pointAt( interactive, 0.8, 0.5 ),
} );
fireEvent.mouseDown( interactive, {
buttons: 1,
...pointAt( interactive, 0.8, 0.5 ),
} );
fireEvent.mouseMove( interactive, {
buttons: 1,
...pointAt( interactive, 0.8, 1 ),
} );
fireEvent.pointerUp( interactive, {
pointerId: 1,
buttons: 0,
...pointAt( interactive, 0.8, 1 ),
} );
fireEvent.mouseUp( interactive, {
buttons: 0,
...pointAt( interactive, 0.8, 1 ),
} );
await dragToBlack( interactive );

expectPointerPosition( pointer, { top: 100 } );
expect( getPointerPosition( pointer ).left ).toBeGreaterThan( 50 );
Expand Down Expand Up @@ -904,9 +894,8 @@ describe( 'ColorPicker', () => {

const colorSlider = screen.getByRole( 'slider', { name: 'Color' } );
onChange.mockClear();
fireEvent.mouseDown( colorSlider, {
buttons: 1,
...pointAt( colorSlider, 0.5, 0 ),
await userEvent.click( colorSlider, {
position: pointAt( colorSlider, 0.5, 0 ),
} );

expect( onChange ).toHaveBeenLastCalledWith( '#80d4ff' );
Expand All @@ -933,9 +922,8 @@ describe( 'ColorPicker', () => {

const colorSlider = screen.getByRole( 'slider', { name: 'Color' } );
onChange.mockClear();
fireEvent.mouseDown( colorSlider, {
buttons: 1,
...pointAt( colorSlider, 0.5, 0.5 ),
await userEvent.click( colorSlider, {
position: pointAt( colorSlider, 0.5, 0.5 ),
} );

expect( onChange ).toHaveBeenLastCalledWith( '#416c81' );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { describe, expect, it, vi } from 'vitest';
import { userEvent } from 'vitest/browser';
import { act, fireEvent, screen, waitFor } from '@testing-library/react';
import { screen, waitFor } from '@testing-library/react';
import { render } from 'vitest-browser-react';
import { useState } from '@wordpress/element';
// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports
import '../style.scss';
// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports
import '../../button/style.scss';
// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports
import '../../dropdown/style.scss';
// eslint-disable-next-line @wordpress/no-non-module-stylesheet-imports
import '../../popover/style.scss';
import CustomGradientPicker from '../';
import CustomGradientBar from '../gradient-bar';
import { KEYBOARD_CONTROL_POINT_VARIATION } from '../gradient-bar/constants';
Expand Down Expand Up @@ -30,16 +38,16 @@ describe( 'CustomGradientPicker', () => {
const barBounds = bar.getBoundingClientRect();
expect( barBounds.width ).toBeGreaterThan( 0 );
// Hover mid-bar so the insert-point control appears (away from 0%/100%).
fireEvent.mouseMove( bar, {
clientX: barBounds.left + barBounds.width / 2,
await userEvent.hover( bar, {
position: { x: barBounds.width / 2, y: barBounds.height / 2 },
} );

// eslint-disable-next-line testing-library/no-node-access
const insertButton = container.querySelector(
'.components-custom-gradient-picker__insert-point-dropdown'
) as HTMLElement;
expect( insertButton ).toBeTruthy();
await act( async () => insertButton.click() );
await userEvent.click( insertButton );

const colorSlider = screen.getByRole( 'slider', { name: 'Color' } );
const sliderBounds = colorSlider.getBoundingClientRect();
Expand All @@ -48,14 +56,11 @@ describe( 'CustomGradientPicker', () => {

// Choose a saturated mid-brightness color — creates the new stop and
// exercises parent gradient updates while picking.
const clientX = sliderBounds.left + sliderBounds.width * 0.8;
const clientY = sliderBounds.top + sliderBounds.height * 0.2;
fireEvent.mouseDown( colorSlider, {
buttons: 1,
clientX,
clientY,
pageX: clientX + window.scrollX,
pageY: clientY + window.scrollY,
await userEvent.click( colorSlider, {
position: {
x: sliderBounds.width * 0.8,
y: sliderBounds.height * 0.2,
},
} );

// ColorPicker content is portaled; pointer has no accessible role.
Expand Down Expand Up @@ -85,7 +90,7 @@ describe( 'CustomGradientPicker', () => {

// Close the portaled popover so later tests are not affected by
// asynchronous Popover position updates.
await act( async () => insertButton.click() );
await userEvent.click( insertButton );
await waitFor( () => {
expect(
screen.queryByRole( 'slider', { name: 'Color' } )
Expand Down Expand Up @@ -204,9 +209,6 @@ describe( 'CustomGradientBar', () => {
] );
} );

// Dragging is driven by window-level listeners attached on mousedown, and
// the position comes from the markers container's box, so that has to be
// given one in jsdom.
it( 'moves a control point when dragged', async () => {
const onChange = vi.fn();

Expand All @@ -223,18 +225,22 @@ describe( 'CustomGradientBar', () => {
const markers = container.querySelector(
'.components-custom-gradient-picker__markers-container'
) as HTMLElement;
markers.getBoundingClientRect = () =>
( { x: 0, width: 200 } ) as DOMRect;
expect( markers.getBoundingClientRect().width ).toBeGreaterThan( 0 );
// eslint-disable-next-line testing-library/no-node-access -- The gradient background has no accessible role.
const bar = markers.closest< HTMLElement >(
'.components-custom-gradient-picker__gradient-bar'
)!;
const bounds = bar.getBoundingClientRect();

const [ firstPoint ] = screen.getAllByRole( 'button', {
name: /Gradient control point/,
} );

fireEvent.mouseDown( firstPoint );
fireEvent.mouseMove( window, { clientX: 100 } );
fireEvent.mouseUp( window );
await userEvent.dragAndDrop( firstPoint, bar, {
targetPosition: { x: bounds.width / 2, y: bounds.height / 2 },
} );

// 100px into a 200px container is 50%.
// The midpoint of the rendered container is 50%.
expect( onChange ).toHaveBeenCalledWith( [
{ position: 50, color: 'rgb(0,0,0)' },
POINTS[ 1 ],
Expand All @@ -258,16 +264,20 @@ describe( 'CustomGradientBar', () => {
const markers = container.querySelector(
'.components-custom-gradient-picker__markers-container'
) as HTMLElement;
markers.getBoundingClientRect = () =>
( { x: 0, width: 200 } ) as DOMRect;
expect( markers.getBoundingClientRect().width ).toBeGreaterThan( 0 );
// eslint-disable-next-line testing-library/no-node-access -- The gradient background has no accessible role.
const bar = markers.closest< HTMLElement >(
'.components-custom-gradient-picker__gradient-bar'
)!;
const bounds = bar.getBoundingClientRect();

const [ firstPoint ] = screen.getAllByRole( 'button', {
name: /Gradient control point/,
} );

fireEvent.mouseDown( firstPoint );
fireEvent.mouseMove( window, { clientX: 100 } );
fireEvent.mouseUp( window );
await userEvent.dragAndDrop( firstPoint, bar, {
targetPosition: { x: bounds.width / 2, y: bounds.height / 2 },
} );

expect( onChange ).not.toHaveBeenCalled();
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ describe( 'FocalPointPicker', () => {

const draggableArea = screen.getByRole( 'button' );

// `user-event` is not capable of testing drag interactions properly.
// we could consider using playwright instead.
// Start a gesture without releasing it so Tab can interrupt it.
fireEvent.mouseDown( draggableArea );

expect( mockOnDrag ).not.toHaveBeenCalled();
expect( mockOnDragEnd ).not.toHaveBeenCalled();

fireEvent.blur( draggableArea );
fireEvent.mouseMove( draggableArea );
expect( draggableArea ).toHaveFocus();
await userEvent.tab();
expect( draggableArea ).not.toHaveFocus();
await userEvent.hover( draggableArea );

expect( mockOnDrag ).not.toHaveBeenCalled();
expect( mockOnDragEnd ).toHaveBeenCalledTimes( 1 );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { describe, expect, it } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { screen, waitFor } from '@testing-library/react';
import { render } from 'vitest-browser-react';
import { userEvent } from 'vitest/browser';
import { useState, useRef } from '@wordpress/element';
import { ValidatedRangeControl } from '../components';

globalThis.wpVitest.mockMatchMedia();

describe( 'ValidatedRangeControl', () => {
it( 'should preserve the help description', () => {
render(
it( 'should preserve the help description', async () => {
await render(
<ValidatedRangeControl label="Opacity" help="Set the opacity." />
);

Expand Down Expand Up @@ -57,7 +56,7 @@ describe( 'ValidatedRangeControl', () => {
);
}

render( <TestComponent /> );
await render( <TestComponent /> );

const slider = screen.getByRole( 'slider', { name: 'Opacity' } );
expect( slider ).toHaveAccessibleDescription( 'Set the opacity.' );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fireEvent, waitFor } from '@testing-library/react';
import { waitFor } from '@testing-library/react';
import { render } from 'vitest-browser-react';
import { describe, expect, it, vi } from 'vitest';
import DataViews from '../index';
Expand Down Expand Up @@ -66,7 +66,6 @@ describe( 'DataViews browser scrolling', () => {
} );

layoutContainer!.scrollTop = layoutContainer!.scrollHeight;
fireEvent.scroll( layoutContainer! );

await waitFor( () => {
expect( onChangeView ).toHaveBeenCalledWith(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
getCursorPosition,
getNearestVisibleBlockAncestor,
Expand All @@ -16,7 +17,7 @@ const OVERLAY_RECT = {
} as DOMRect;

function mockRect( element: HTMLElement, rect: Partial< DOMRect > ) {
element.getBoundingClientRect = jest.fn().mockReturnValue( {
vi.spyOn( element, 'getBoundingClientRect' ).mockReturnValue( {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Non-blocking: this suite supplies rectangles on every element through mockRect, but has no jsdomBrowserApis entry. The rule only tracks DOM receivers from local assignments, not helper parameters, so vi.spyOn( element, 'getBoundingClientRect' ) here passes (so would a direct element.getBoundingClientRect = ... in a helper, so the gap predates this PR).

Since the suite is newly migrated and its rationale at line 33 matches the grid item-limits entries, adding an entry would keep the exception list complete.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Added the missing jsdom exception and taught the policy rule to track DOM values through local helper parameters in 2c83303010e.

left: 0,
top: 0,
right: 0,
Expand All @@ -26,23 +27,23 @@ function mockRect( element: HTMLElement, rect: Partial< DOMRect > ) {
x: 0,
y: 0,
...rect,
} );
} as DOMRect );
}

// jsdom performs no layout and doesn't implement Range.getBoundingClientRect
// / getClientRects at all (not even as a zero-returning stub), so any test
// that lets getOffsetPositionInBlock create and measure a real Range needs
// this polyfilled.
beforeAll( () => {
Range.prototype.getBoundingClientRect = jest.fn().mockReturnValue( {
left: 0,
top: 0,
width: 0,
height: 0,
x: 0,
y: 0,
} );
Range.prototype.getClientRects = jest.fn().mockReturnValue( [] );
// Supplied rectangles exercise the fallback and ancestor-selection algorithms.
// Keep native Range construction and DOM traversal, without measuring layout.
beforeEach( () => {
const createRange = document.createRange.bind( document );
vi.spyOn( document, 'createRange' ).mockImplementation( () =>
Object.assign( createRange(), {
getBoundingClientRect: () => new DOMRect(),
getClientRects: () => [],
} )
);
} );

afterEach( () => {
vi.restoreAllMocks();
} );

describe( 'cursor-dom-utils', () => {
Expand Down Expand Up @@ -80,6 +81,7 @@ describe( 'cursor-dom-utils', () => {
);

expect( result ).toBeNull();
expect( document.createRange ).not.toHaveBeenCalled();
} );
} );

Expand Down
Loading
Loading