Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Add more tests (#133)

Emil Kowalskia73a8c757fd6
diff --git a/.github/workflow/playwright.yml b/.github/workflows/playwright.yml
similarity index 100%
rename from .github/workflow/playwright.yml
rename to .github/workflows/playwright.yml
diff --git a/playwright.config.ts b/playwright.config.ts
index 11f30dd..43d374e 100644
--- a/playwright.config.ts
+++ b/playwright.config.ts
@@ -45,7 +45,7 @@ export default defineConfig({
projects: [
{
name: 'iPhone',
- use: { ...devices['iPhone 12'] },
+ use: { ...devices['iPhone 13 Pro'] },
},
{
name: 'Pixel',
diff --git a/test/src/app/nested-drawers/page.tsx b/test/src/app/nested-drawers/page.tsx
index 3050a47..e9c9387 100644
--- a/test/src/app/nested-drawers/page.tsx
+++ b/test/src/app/nested-drawers/page.tsx
@@ -7,11 +7,14 @@ export default function Page() {
<div className="w-screen h-screen bg-white p-8 flex justify-center items-center" vaul-drawer-wrapper="">
<Drawer.Root shouldScaleBackground>
<Drawer.Trigger asChild>
- <button>Open Drawer</button>
+ <button data-testid="trigger">Open Drawer</button>
</Drawer.Trigger>
<Drawer.Portal>
<Drawer.Overlay className="fixed inset-0 bg-black/40" />
- <Drawer.Content className="bg-gray-100 flex flex-col rounded-t-[10px] h-full mt-24 max-h-[96%] fixed bottom-0 left-0 right-0">
+ <Drawer.Content
+ data-testid="content"
+ className="bg-gray-100 flex flex-col rounded-t-[10px] h-full mt-24 max-h-[96%] fixed bottom-0 left-0 right-0"
+ >
<div className="p-4 bg-white rounded-t-[10px] flex-1">
<div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-gray-300 mb-8" />
<div className="max-w-md mx-auto">
@@ -39,12 +42,19 @@ export default function Page() {
</a>
</p>
<Drawer.NestedRoot>
- <Drawer.Trigger className="rounded-md mb-6 w-full bg-gray-900 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-gray-800 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-gray-600">
+ <Drawer.Trigger
+ data-testid="nested-trigger"
+ className="rounded-md mb-6 w-full bg-gray-900 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-gray-800 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-gray-600"
+ >
Open Second Drawer
</Drawer.Trigger>
<Drawer.Portal>
<Drawer.Overlay className="fixed inset-0 bg-black/40" />
- <Drawer.Content className="bg-gray-100 flex flex-col rounded-t-[10px] h-full mt-24 max-h-[94%] fixed bottom-0 left-0 right-0">
+ <Drawer.Content
+ data-testid="nested-content"
+ className="bg-gray-100 flex flex-col rounded-t-[10px] h-full mt-24 max-h-[94%] fixed bottom-0 left-0 right-0"
+ >
+ <Drawer.Close data-testid="nested-close">Close</Drawer.Close>
<div className="p-4 bg-white rounded-t-[10px] flex-1">
<div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-gray-300 mb-8" />
<div className="max-w-md mx-auto">
diff --git a/test/src/app/non-dismissible/page.tsx b/test/src/app/non-dismissible/page.tsx
index 1bd3399..8cc8b40 100644
--- a/test/src/app/non-dismissible/page.tsx
+++ b/test/src/app/non-dismissible/page.tsx
@@ -8,12 +8,15 @@ export default function Page() {
return (
<div className="w-screen h-screen bg-white p-8 flex justify-center items-center" vaul-drawer-wrapper="">
<Drawer.Root dismissible={false} open={open}>
- <Drawer.Trigger asChild onClick={() => setOpen(true)}>
+ <Drawer.Trigger data-testid="trigger" asChild onClick={() => setOpen(true)}>
<button>Open Drawer</button>
</Drawer.Trigger>
<Drawer.Portal>
<Drawer.Overlay className="fixed inset-0 bg-black/40" />
- <Drawer.Content className="bg-zinc-100 flex flex-col rounded-t-[10px] mt-24 fixed bottom-0 left-0 right-0">
+ <Drawer.Content
+ data-testid="content"
+ className="bg-zinc-100 flex flex-col rounded-t-[10px] mt-24 fixed bottom-0 left-0 right-0"
+ >
<div className="p-4 bg-white rounded-t-[10px] flex-1">
<div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-zinc-300 mb-8" />
<div className="max-w-md mx-auto">
@@ -42,6 +45,7 @@ export default function Page() {
<button
type="button"
+ data-testid="dismiss-button"
onClick={() => setOpen(false)}
className="rounded-md mb-6 w-full bg-gray-900 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-gray-800 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-gray-600"
>
diff --git a/test/src/app/with-scaled-background/page.tsx b/test/src/app/with-scaled-background/page.tsx
index a36d689..e9e49d7 100644
--- a/test/src/app/with-scaled-background/page.tsx
+++ b/test/src/app/with-scaled-background/page.tsx
@@ -12,7 +12,7 @@ export default function Page() {
</button>
</Drawer.Trigger>
<Drawer.Portal>
- <Drawer.Overlay data-testid="ovarlay" className="fixed inset-0 bg-black/40" />
+ <Drawer.Overlay data-testid="overlay" className="fixed inset-0 bg-black/40" />
<Drawer.Content
data-testid="content"
className="bg-zinc-100 flex flex-col rounded-t-[10px] h-[96%] mt-24 fixed bottom-0 left-0 right-0"
diff --git a/test/src/app/with-snap-points/page.tsx b/test/src/app/with-snap-points/page.tsx
index 4b7f760..52a9b77 100644
--- a/test/src/app/with-snap-points/page.tsx
+++ b/test/src/app/with-snap-points/page.tsx
@@ -4,18 +4,26 @@ import { clsx } from 'clsx';
import { useState } from 'react';
import { Drawer } from 'vaul';
+const snapPoints = ['148px', '355px', 1];
+
export default function Page() {
- const [snap, setSnap] = useState<number | string | null>('148px');
+ const [snap, setSnap] = useState<number | string | null>(snapPoints[0]);
+
+ const activeSnapPointIndex = snapPoints.indexOf(snap as string);
return (
<div className="w-screen h-screen bg-white p-8 flex justify-center items-center">
- <Drawer.Root snapPoints={['148px', '355px', 1]} activeSnapPoint={snap} setActiveSnapPoint={setSnap}>
+ <div data-testid="active-snap-index">{activeSnapPointIndex}</div>
+ <Drawer.Root snapPoints={snapPoints} activeSnapPoint={snap} setActiveSnapPoint={setSnap}>
<Drawer.Trigger asChild>
- <button>Open Drawer</button>
+ <button data-testid="trigger">Open Drawer</button>
</Drawer.Trigger>
<Drawer.Overlay className="fixed inset-0 bg-black/40" />
<Drawer.Portal>
- <Drawer.Content className="fixed flex flex-col bg-white border border-gray-200 border-b-none rounded-t-[10px] bottom-0 left-0 right-0 h-full max-h-[97%] mx-[-1px]">
+ <Drawer.Content
+ data-testid="content"
+ className="fixed flex flex-col bg-white border border-gray-200 border-b-none rounded-t-[10px] bottom-0 left-0 right-0 h-full max-h-[97%] mx-[-1px]"
+ >
<div
className={clsx('flex flex-col max-w-md mx-auto w-full p-4 pt-5', {
'overflow-y-auto': snap === 1,
diff --git a/test/src/app/without-scaled-background/page.tsx b/test/src/app/without-scaled-background/page.tsx
index 84a5928..e6431fd 100644
--- a/test/src/app/without-scaled-background/page.tsx
+++ b/test/src/app/without-scaled-background/page.tsx
@@ -1,22 +1,28 @@
'use client';
+import { useState } from 'react';
import { Drawer } from 'vaul';
export default function Page() {
+ const [open, setOpen] = useState(false);
return (
<div className="w-screen h-screen bg-white p-8 flex justify-center items-center" vaul-drawer-wrapper="">
- <Drawer.Root>
+ <Drawer.Root open={open} onOpenChange={setOpen}>
<Drawer.Trigger asChild>
<button data-testid="trigger" className="text-2xl">
Open Drawer
</button>
</Drawer.Trigger>
<Drawer.Portal>
- <Drawer.Overlay data-testid="ovarlay" className="fixed inset-0 bg-black/40" />
+ <Drawer.Overlay data-testid="overlay" className="fixed inset-0 bg-black/40" />
<Drawer.Content
data-testid="content"
className="bg-zinc-100 flex flex-col rounded-t-[10px] h-[96%] mt-24 fixed bottom-0 left-0 right-0"
>
+ <Drawer.Close data-testid="drawer-close">Close</Drawer.Close>
+ <button data-testid="controlled-close" onClick={() => setOpen(false)} className="text-2xl">
+ Close
+ </button>
<div className="p-4 bg-white rounded-t-[10px] flex-1">
<div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-zinc-300 mb-8" />
<div className="max-w-md mx-auto">
diff --git a/test/tests/base.spec.ts b/test/tests/base.spec.ts
new file mode 100644
index 0000000..207098d
--- /dev/null
+++ b/test/tests/base.spec.ts
@@ -0,0 +1,62 @@
+import { test, expect } from '@playwright/test';
+import { ANIMATION_DURATION } from './constants';
+import { openDrawer } from './helpers';
+
+test.beforeEach(async ({ page }) => {
+ await page.goto('/without-scaled-background');
+});
+
+test.describe('Base tests', () => {
+ test('should open drawer', async ({ page }) => {
+ await expect(page.getByTestId('content')).not.toBeVisible();
+
+ await page.getByTestId('trigger').click();
+
+ await expect(page.getByTestId('content')).toBeVisible();
+ });
+
+ test('should close on background interaction', async ({ page }) => {
+ await openDrawer(page);
+ // Click on the background
+ await page.mouse.click(0, 0);
+
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('content')).not.toBeVisible();
+ });
+
+ test('should close when `Drawer.Close` is clicked', async ({ page }) => {
+ await openDrawer(page);
+
+ await page.getByTestId('drawer-close').click();
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('content')).not.toBeVisible();
+ });
+
+ test('should close when controlled', async ({ page }) => {
+ await openDrawer(page);
+
+ await page.getByTestId('controlled-close').click();
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('content')).not.toBeVisible();
+ });
+
+ test('should close when dragged down', async ({ page }) => {
+ await openDrawer(page);
+ await page.hover('[vaul-drawer]');
+ await page.mouse.down();
+ await page.mouse.move(0, 800);
+ await page.mouse.up();
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('content')).not.toBeVisible();
+ });
+
+ test('should not close when dragged up', async ({ page }) => {
+ await openDrawer(page);
+ await page.hover('[vaul-drawer]');
+ await page.mouse.down();
+ await page.mouse.move(0, -800);
+ await page.mouse.up();
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('content')).toBeVisible();
+ });
+});
diff --git a/test/tests/constants.ts b/test/tests/constants.ts
new file mode 100644
index 0000000..7c4f406
--- /dev/null
+++ b/test/tests/constants.ts
@@ -0,0 +1 @@
+export const ANIMATION_DURATION = 500;
diff --git a/test/tests/helpers.ts b/test/tests/helpers.ts
new file mode 100644
index 0000000..ecd8182
--- /dev/null
+++ b/test/tests/helpers.ts
@@ -0,0 +1,28 @@
+import { expect, Page } from '@playwright/test';
+import { ANIMATION_DURATION } from './constants';
+
+export async function openDrawer(page: Page) {
+ await expect(page.getByTestId('content')).not.toBeVisible();
+ await page.getByTestId('trigger').click();
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('content')).toBeVisible();
+}
+
+export async function dragWithSpeed(
+ page: Page,
+ selector: string,
+ startY: number,
+ endY: number,
+ speed: number = 10,
+): Promise<void> {
+ const startX = 0;
+ const distance = Math.abs(endY - startY);
+ const steps = distance / speed;
+ const delayPerStep = 10; // in milliseconds
+ const yOffset = (endY - startY) / steps;
+
+ await page.hover(selector);
+ await page.mouse.down();
+ await page.mouse.move(0, -200);
+ await page.mouse.up();
+}
diff --git a/test/tests/nested.spec.ts b/test/tests/nested.spec.ts
new file mode 100644
index 0000000..9edf534
--- /dev/null
+++ b/test/tests/nested.spec.ts
@@ -0,0 +1,20 @@
+import { test, expect } from '@playwright/test';
+import { ANIMATION_DURATION } from './constants';
+import { openDrawer } from './helpers';
+
+test.beforeEach(async ({ page }) => {
+ await page.goto('/nested-drawers');
+});
+
+test.describe.only('Nested tests', () => {
+ test('should open and close nested drawer', async ({ page }) => {
+ await openDrawer(page);
+ await page.getByTestId('nested-trigger').click();
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('nested-content')).toBeVisible();
+ await page.getByTestId('nested-close').click();
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('nested-content')).not.toBeVisible();
+ await await expect(page.getByTestId('content')).toBeVisible();
+ });
+});
diff --git a/test/tests/non-dismissible.spec.ts b/test/tests/non-dismissible.spec.ts
new file mode 100644
index 0000000..3bf0259
--- /dev/null
+++ b/test/tests/non-dismissible.spec.ts
@@ -0,0 +1,35 @@
+import { test, expect } from '@playwright/test';
+import { openDrawer } from './helpers';
+import { ANIMATION_DURATION } from './constants';
+
+test.beforeEach(async ({ page }) => {
+ await page.goto('/non-dismissible');
+});
+
+test.describe('Non-dismissible', () => {
+ test('should not close on background interaction', async ({ page }) => {
+ await openDrawer(page);
+ // Click on the background
+ await page.mouse.click(0, 0);
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('content')).toBeVisible();
+ });
+
+ test('should not close when dragged down', async ({ page }) => {
+ await openDrawer(page);
+ await page.hover('[vaul-drawer]');
+ await page.mouse.down();
+ await page.mouse.move(0, 800);
+ await page.mouse.up();
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('content')).toBeVisible();
+ });
+
+ test('should close when the dismiss button is clicked', async ({ page }) => {
+ await openDrawer(page);
+
+ await page.getByTestId('dismiss-button').click();
+ await page.waitForTimeout(ANIMATION_DURATION);
+ await expect(page.getByTestId('content')).not.toBeVisible();
+ });
+});
diff --git a/test/tests/with-scaled-background.spec.ts b/test/tests/with-scaled-background.spec.ts
index 5e56f75..8c36be2 100644
--- a/test/tests/with-scaled-background.spec.ts
+++ b/test/tests/with-scaled-background.spec.ts
@@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
+import { openDrawer } from './helpers';
test.beforeEach(async ({ page }) => {
await page.goto('/with-scaled-background');
@@ -12,11 +13,20 @@ test.describe('With scaled background', () => {
await expect(page.locator('[vaul-drawer-wrapper]')).toHaveCSS('transform', /matrix/);
});
- test('should open drawer', async ({ page }) => {
- await expect(page.getByTestId('content')).not.toBeVisible();
- await page.getByTestId('trigger').click();
+ test('should scale background when dragging', async ({ page }) => {
+ await expect(page.locator('[vaul-drawer-wrapper]')).not.toHaveCSS('transform', '');
+
+ await openDrawer(page);
+
+ await page.hover('[vaul-drawer]');
+ await page.mouse.down();
+ await page.mouse.move(0, 100);
- await expect(page.getByTestId('content')).toBeVisible();
+ await expect(page.locator('[vaul-drawer-wrapper]')).toHaveCSS('transform', /matrix/);
+
+ await page.mouse.up();
+
+ await expect(page.locator('[vaul-drawer-wrapper]')).not.toHaveCSS('transform', '');
});
});
diff --git a/test/tests/with-snap-points.test.ts b/test/tests/with-snap-points.test.ts
new file mode 100644
index 0000000..554cf27
--- /dev/null
+++ b/test/tests/with-snap-points.test.ts
@@ -0,0 +1,20 @@
+// import { test, expect } from '@playwright/test';
+// import { ANIMATION_DURATION } from './constants';
+// import { openDrawer } from './helpers';
+
+// test.beforeEach(async ({ page }) => {
+// await page.goto('/with-snap-points');
+// });
+
+// test.describe.only('Base tests', () => {
+// test('should change active snap point', async ({ page }) => {
+// await openDrawer(page);
+// await page.hover('[vaul-drawer]');
+// await page.mouse.down();
+// await page.mouse.move(0, -800);
+// await page.mouse.up();
+// const activeSnap = await page.getByTestId('active-snap-index').innerText();
+// await page.waitForTimeout(ANIMATION_DURATION);
+// expect(activeSnap).toBe('2');
+// });
+// });
diff --git a/test/tests/without-scaled-background.spec.ts b/test/tests/without-scaled-background.spec.ts
index 4ff3dd2..359d3f3 100644
--- a/test/tests/without-scaled-background.spec.ts
+++ b/test/tests/without-scaled-background.spec.ts
@@ -5,18 +5,11 @@ test.beforeEach(async ({ page }) => {
});
test.describe('Without scaled background', () => {
- test('should scale background', async ({ page }) => {
+ test('should not scale background', async ({ page }) => {
await expect(page.locator('[vaul-drawer-wrapper]')).not.toHaveCSS('transform', '');
await page.getByTestId('trigger').click();
await expect(page.locator('[vaul-drawer-wrapper]')).not.toHaveCSS('transform', '');
});
- test('should open drawer', async ({ page }) => {
- await expect(page.getByTestId('content')).not.toBeVisible();
-
- await page.getByTestId('trigger').click();
-
- await expect(page.getByTestId('content')).toBeVisible();
- });
});