Skip to content

Commit

Permalink
expects, locators
Browse files Browse the repository at this point in the history
  • Loading branch information
satelllte committed Oct 15, 2023
1 parent 74bf38c commit 51be7cc
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 169 deletions.
97 changes: 97 additions & 0 deletions apps/docs/e2e/expects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {expect, type Page, type Locator} from '@playwright/test';

const _dragSteps = 10;
const _dragAmplitude = 20.0;

const _calculateElementCenter = async (element: Locator) => {
const bounds = await element.evaluate((el) => el.getBoundingClientRect());
const x = bounds.x + bounds.width / 2;
const y = bounds.y + bounds.height / 2;
return {x, y};
};

export const knobValueTextIs = async (
knob: Locator,
{knobOutput, valueText}: {knobOutput?: Locator; valueText: string},
) => {
expect(await knob.getAttribute('aria-valuetext')).toBe(valueText);
if (knobOutput) {
await expect(knobOutput).toHaveText(valueText);
}
};

export const knobValueIsEqualTo = async (
knob: Locator,
{valueNow}: {valueNow: number},
) => {
expect(await knob.getAttribute('aria-valuenow')).toBe(`${valueNow}`);
};

export const knobValueIsLessThan = async (
knob: Locator,
{value}: {value: number},
) => {
expect(Number(await knob.getAttribute('aria-valuenow'))).toBeLessThan(value);
};

export const knobValueIsMoreThan = async (
knob: Locator,
{value}: {value: number},
) => {
expect(Number(await knob.getAttribute('aria-valuenow'))).toBeGreaterThan(
value,
);
};

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 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();

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

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

export const expects = {
knobValueTextIs,
knobValueIsEqualTo,
knobValueIsLessThan,
knobValueIsMoreThan,
knobDragsUpCorrectly,
knobDragsDownCorrectly,
} as const;
Loading

0 comments on commit 51be7cc

Please sign in to comment.