only emit sl-change when you stop dragging

This commit is contained in:
konnorrogers
2023-10-26 16:44:27 -04:00
parent 0281d0b0b6
commit 1c8a73fc0d
2 changed files with 51 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
import '../../../dist/shoelace.js';
import { aTimeout, expect, fixture, html, oneEvent } from '@open-wc/testing';
import { clickOnElement } from '../../internal/test.js';
import { clickOnElement, dragElement } from '../../internal/test.js';
import { runFormControlBaseTests } from '../../internal/test/form-control-base-tests.js';
import { sendKeys } from '@web/test-runner-commands';
import { serialize } from '../../utilities/form.js';
@@ -31,11 +31,22 @@ describe('<sl-color-picker>', () => {
await clickOnElement(trigger); // open the dropdown
await aTimeout(200); // wait for the dropdown to open
await clickOnElement(grid); // click on the grid
// Simulate a drag event. "sl-change" should not fire until we stop dragging.
await dragElement(grid, 2, 0, {
afterMouseDown: async () => {
expect(changeHandler).to.have.not.been.called;
expect(inputHandler).to.have.been.calledOnce;
},
afterMouseMove: async () => {
expect(inputHandler).to.have.been.calledTwice;
}
});
await el.updateComplete;
expect(changeHandler).to.have.been.calledOnce;
expect(inputHandler).to.have.been.calledOnce;
expect(inputHandler).to.have.been.calledTwice;
});
it('should emit sl-change and sl-input when the hue slider is moved', async () => {
@@ -50,10 +61,22 @@ describe('<sl-color-picker>', () => {
await clickOnElement(trigger); // open the dropdown
await aTimeout(200); // wait for the dropdown to open
await clickOnElement(slider); // click on the hue slider
// Simulate a drag event. "sl-change" should not fire until we stop dragging.
await dragElement(slider, 20, 0, {
afterMouseDown: async () => {
expect(changeHandler).to.have.not.been.called;
expect(inputHandler).to.have.been.calledOnce;
},
afterMouseMove: async () => {
// It's not twice because you can't change the hue of white!
expect(inputHandler).to.have.been.calledOnce;
}
});
await el.updateComplete;
expect(changeHandler).to.have.been.calledOnce;
// It's not twice because you can't change the hue of white!
expect(inputHandler).to.have.been.calledOnce;
});
@@ -69,11 +92,22 @@ describe('<sl-color-picker>', () => {
await clickOnElement(trigger); // open the dropdown
await aTimeout(200); // wait for the dropdown to open
await clickOnElement(slider); // click on the opacity slider
// Simulate a drag event. "sl-change" should not fire until we stop dragging.
await dragElement(slider, 2, 0, {
afterMouseDown: async () => {
expect(changeHandler).to.have.not.been.called;
expect(inputHandler).to.have.been.calledOnce;
},
afterMouseMove: async () => {
expect(inputHandler).to.have.been.calledTwice;
}
});
await el.updateComplete;
expect(changeHandler).to.have.been.calledOnce;
expect(inputHandler).to.have.been.calledOnce;
expect(inputHandler).to.have.been.calledTwice;
});
it('should emit sl-change and sl-input when toggling the format', async () => {

View File

@@ -73,11 +73,21 @@ export async function dragElement(
/** The horizontal distance to drag in pixels */
deltaX = 0,
/** The vertical distance to drag in pixels */
deltaY = 0
deltaY = 0,
callbacks: {
afterMouseDown?: () => Promise<void>
afterMouseMove?: () => Promise<void>
} = {}
): Promise<void> {
await moveMouseOnElement(el);
await sendMouse({ type: 'down' });
await callbacks.afterMouseDown?.()
const { clickX, clickY } = determineMousePosition(el, 'center', deltaX, deltaY);
await sendMouse({ type: 'move', position: [clickX, clickY] });
await callbacks.afterMouseMove?.()
await sendMouse({ type: 'up' });
}