Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hirasso committed Sep 26, 2024
1 parent f01e586 commit 90e88a9
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 11 deletions.
25 changes: 18 additions & 7 deletions docs/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Layout from "../layouts/Layout.astro";
import VerticalExample from "../components/examples/Vertical.astro";
import HorizontalExample from "../components/examples/Horizontal.astro";
import BothDirectionsExample from "../components/examples/BothDirections.astro";
import WindowExample from "../components/examples/WindowExample.astro";
import { ArrowRightIcon } from "astro-feather";
---

Expand Down Expand Up @@ -46,13 +45,25 @@ import { ArrowRightIcon } from "astro-feather";

<script>
import ScrollMirror from "../../../src/index.js";
import Horizontal from "../components/examples/Horizontal.astro";

import { $$ } from "../js/helpers";
/** Vertical mirroring */
new ScrollMirror($$(".scroller.--vertical"));
/** Horizontal mirroring */
new ScrollMirror($$(".scroller.--horizontal"));
/** Mirroring in both directions */
new ScrollMirror($$(".scroller.--both"));

declare global {
interface Window {
mirrors: {
vertical: ScrollMirror,
horizontal: ScrollMirror,
both: ScrollMirror
};
}
}

/** Setup mirrors */
window.mirrors = {
vertical: new ScrollMirror($$(".scroller.--vertical")),
horizontal: new ScrollMirror($$(".scroller.--horizontal")),
both: new ScrollMirror($$(".scroller.--both"))
}

</script>
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"docs:build": "astro build --root docs",
"docs:serve": "astro build --root docs && astro preview --root docs",
"test": "pnpm run test:e2e",
"test:e2e": "pnpm playwright test --config ./tests/e2e/config.playwright.ts",
"test:e2e:dev": "PLAYWRIGHT_ENV=dev pnpm run test:e2e -- --ui",
"test:e2e": "pnpm exec playwright test --config ./tests/e2e/config.playwright.ts",
"test:e2e:dev": "PLAYWRIGHT_ENV=dev pnpm run test:e2e --ui",
"test:e2e:install": "pnpm exec playwright install --with-deps"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type Options = {
horizontal: boolean;
};

type Progress = {
export type Progress = {
x: number;
y: number;
};
Expand Down
41 changes: 41 additions & 0 deletions tests/e2e/tests/api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { test, expect } from "@playwright/test";
import {
scrollTo,
scrollToEnd,
expectScrollPosition,
sleep,
expectProgress,
setProgress,
} from "./support";

test.describe("API", () => {
test.beforeEach(async ({ page }) => {
await page.goto("/");
});

test("gets the current progress after mirroring", async ({ page }) => {
await scrollToEnd(page, "first-vertical");
await sleep(1000);
await expectProgress(page, "vertical", { x: 0, y: 1 });
});

test("sets the progress vertically", async ({ page }) => {
await setProgress(page, "vertical", { y: 0.5 });
await expectProgress(page, "vertical", { x: 0, y: 0.5 });
});

test("sets the progress horizontally", async ({ page }) => {
await setProgress(page, "horizontal", { x: 0.5 });
await expectProgress(page, "horizontal", { x: 0.5, y: 0 });
});

test("sets the progress in both directions", async ({ page }) => {
// two values (x, y)
await setProgress(page, "both", { x: 0.25, y: 0.75 });
await expectProgress(page, "both", { x: 0.25, y: 0.75 });

// And one value for both directions
await setProgress(page, "both", 0.5);
await expectProgress(page, "both", { x: 0.5, y: 0.5 });
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, expect } from "@playwright/test";
import { scrollTo, scrollToEnd, expectScrollPosition, sleep } from "./support";

test.describe("Features", () => {
test.describe("Mirroring", () => {
test.beforeEach(async ({ page }) => {
await page.goto("/");
});
Expand Down
43 changes: 43 additions & 0 deletions tests/e2e/tests/support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ type ScrollPosition = {
y: number;
};

import ScrollMirror from "../../../src/index.js";
import type { Progress } from "../../../src/index.js";

declare global {
interface Window {
mirrors: {
vertical: ScrollMirror,
horizontal: ScrollMirror,
both: ScrollMirror
};
}
}

export function scrollTo(
page: Page,
position: Partial<ScrollPosition>,
Expand Down Expand Up @@ -71,3 +84,33 @@ export async function expectScrollPosition(
}, testId);
expect(scrollY).toEqual(expected);
}

export async function setProgress(
page: Page,
instance: string,
progress: Partial<Progress> | number
) {
await page.evaluate(
({ instance, progress }): Progress => {
return (window.mirrors[instance].progress = progress);
},
{ instance, progress }
);
}

function roundTwoDecimals(value: number) {
return Math.round(value * 100) / 100;
}

export async function expectProgress(
page: Page,
instance: string,
expected: Progress
) {
const progress = await page.evaluate((instance): Progress => {
return window.mirrors[instance].progress;
}, instance);

expect(roundTwoDecimals(progress.x)).toEqual(expected.x);
expect(roundTwoDecimals(progress.y)).toEqual(expected.y);
}

0 comments on commit 90e88a9

Please sign in to comment.