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

Refactor: E2E - reusable fn for knob drags assertions #44

Merged
merged 4 commits into from
Oct 22, 2023
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
136 changes: 52 additions & 84 deletions apps/docs/e2e/expects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,50 @@ const _calculateElementCenter = async (element: Locator) => {
return {x, y};
};

type KnobDragAssertionArgs = [Locator, {valueNow: number; page: Page}];

const _knobDragsCorrectly =
(direction: 'up' | 'down' | 'left' | 'right') =>
async (
knob: Locator,
{
valueNow,
page,
}: {
valueNow: number;
page: Page;
},
) => {
// It's necessary to hover over the knob and scroll it into view,
// so then we can calculate its bounds in the viewport properly
await knob.hover();

let {x, y} = await _calculateElementCenter(knob);

x =
direction === 'right'
? x + _dragAmplitude
: direction === 'left'
? x - _dragAmplitude
: x;
y =
direction === 'down'
? y + _dragAmplitude
: direction === 'up'
? y - _dragAmplitude
: y;

await page.mouse.down();
await page.mouse.move(x, y, {steps: _dragSteps});
await page.mouse.up();

if (direction === 'up' || direction === 'right') {
await knobValueIsMoreThan(knob, {value: valueNow});
} else {
await knobValueIsLessThan(knob, {value: valueNow});
}
};

export const knobValueTextIs = async (
knob: Locator,
{knobOutput, valueText}: {knobOutput?: Locator; valueText: string},
Expand Down Expand Up @@ -43,93 +87,17 @@ export const knobValueIsMoreThan = async (
);
};

export const knobDragsDownCorrectly = async (
knob: Locator,
{
valueNow,
page,
}: {
valueNow: number;
page: Page;
},
) => {
// It's necessary to hover over the knob and scroll it into view,
// so then we can calculate its bounds in the viewport properly
await knob.hover();
export const knobDragsDownCorrectly = async (...args: KnobDragAssertionArgs) =>
_knobDragsCorrectly('down')(...args);

const {x, y} = await _calculateElementCenter(knob);
export const knobDragsUpCorrectly = async (...args: KnobDragAssertionArgs) =>
_knobDragsCorrectly('up')(...args);

await page.mouse.down();
await page.mouse.move(x, y + _dragAmplitude, {steps: _dragSteps});
await page.mouse.up();
await knobValueIsLessThan(knob, {value: valueNow});
};
export const knobDragsLeftCorrectly = async (...args: KnobDragAssertionArgs) =>
_knobDragsCorrectly('left')(...args);

export const knobDragsUpCorrectly = async (
knob: Locator,
{
valueNow,
page,
}: {
valueNow: number;
page: Page;
},
) => {
// It's necessary to hover over the knob and scroll it into view,
// so then we can calculate its bounds in the viewport properly
await knob.hover();

const {x, y} = await _calculateElementCenter(knob);

await page.mouse.down();
await page.mouse.move(x, y - _dragAmplitude, {steps: _dragSteps});
await page.mouse.up();
await knobValueIsMoreThan(knob, {value: valueNow});
};

export const knobDragsLeftCorrectly = async (
knob: Locator,
{
valueNow,
page,
}: {
valueNow: number;
page: Page;
},
) => {
// It's necessary to hover over the knob and scroll it into view,
// so then we can calculate its bounds in the viewport properly
await knob.hover();

const {x, y} = await _calculateElementCenter(knob);

await page.mouse.down();
await page.mouse.move(x - _dragAmplitude, y, {steps: _dragSteps});
await page.mouse.up();
await knobValueIsLessThan(knob, {value: valueNow});
};

export const knobDragsRightCorrectly = async (
knob: Locator,
{
valueNow,
page,
}: {
valueNow: number;
page: Page;
},
) => {
// It's necessary to hover over the knob and scroll it into view,
// so then we can calculate its bounds in the viewport properly
await knob.hover();

const {x, y} = await _calculateElementCenter(knob);

await page.mouse.down();
await page.mouse.move(x + _dragAmplitude, y, {steps: _dragSteps});
await page.mouse.up();
await knobValueIsMoreThan(knob, {value: valueNow});
};
export const knobDragsRightCorrectly = async (...args: KnobDragAssertionArgs) =>
_knobDragsCorrectly('right')(...args);

export const sourceCodeLinkIsValid = async ({
link,
Expand Down