Skip to content

Commit

Permalink
Add more tests (#133)
Browse files Browse the repository at this point in the history
* Add base tests

* Add tests for scaled background

* Add tests for dismiss button

* More tests

* Start snap points test

* Add tests for nested drawers

* Rename
  • Loading branch information
emilkowalski committed Oct 3, 2023
1 parent 9a26d57 commit a73a8c7
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 26 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default defineConfig({
projects: [
{
name: 'iPhone',
use: { ...devices['iPhone 12'] },
use: { ...devices['iPhone 13 Pro'] },
},
{
name: 'Pixel',
Expand Down
18 changes: 14 additions & 4 deletions test/src/app/nested-drawers/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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">
Expand Down
8 changes: 6 additions & 2 deletions test/src/app/non-dismissible/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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"
>
Expand Down
2 changes: 1 addition & 1 deletion test/src/app/with-scaled-background/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 12 additions & 4 deletions test/src/app/with-snap-points/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions test/src/app/without-scaled-background/page.tsx
Original file line number Diff line number Diff line change
@@ -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">
Expand Down
62 changes: 62 additions & 0 deletions test/tests/base.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
1 change: 1 addition & 0 deletions test/tests/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ANIMATION_DURATION = 500;
28 changes: 28 additions & 0 deletions test/tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -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();
}
20 changes: 20 additions & 0 deletions test/tests/nested.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
35 changes: 35 additions & 0 deletions test/tests/non-dismissible.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
18 changes: 14 additions & 4 deletions test/tests/with-scaled-background.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
import { openDrawer } from './helpers';

test.beforeEach(async ({ page }) => {
await page.goto('/with-scaled-background');
Expand All @@ -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', '');
});
});
20 changes: 20 additions & 0 deletions test/tests/with-snap-points.test.ts
Original file line number Diff line number Diff line change
@@ -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');
// });
// });
9 changes: 1 addition & 8 deletions test/tests/without-scaled-background.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit a73a8c7

Please sign in to comment.