import { expect, test } from './fixtures';
test('portfolio switches to a right-side menu drawer strictly below 770px', async ({ page }) => {
await page.emulateMedia({ reducedMotion: 'reduce' });
await page.setViewportSize({ width: 770, height: 600 });
await page.goto('/about-me');
const nav = page.getByRole('navigation', { name: 'Main navigation' });
const toggle = page.getByRole('button', { name: 'Menu', exact: true });
const menu = page.getByRole('dialog', { name: 'Menu' });
const current = nav.getByRole('link', { name: 'About me' });
await expect(current).toHaveAttribute('aria-current', 'page');
await expect(toggle).toBeHidden();
await page.setViewportSize({ width: 769, height: 600 });
await expect(nav).toBeHidden();
await expect(toggle).toBeVisible();
await expect(page.getByRole('button', { name: 'Shortlist 0' })).toBeVisible();
await toggle.click();
await expect(menu).toHaveCSS('animation-name', 'none');
expect(await menu.boundingBox()).toMatchObject({ x: 417, y: 0, width: 352, height: 600 });
await expect(toggle).toHaveAttribute('aria-expanded', 'true');
await expect(menu.getByRole('button', { name: 'Close menu' })).toBeFocused();
await expect(page.locator('html')).toHaveCSS('overflow', 'hidden');
await menu.getByRole('heading', { name: 'Menu' }).click();
await expect(menu).toBeVisible();
await page.mouse.click(5, 5);
await expect(menu).toBeHidden();
await expect(toggle).toBeFocused();
await expect(page.locator('html')).not.toHaveCSS('overflow', 'hidden');
await toggle.click();
await page.keyboard.press('Escape');
await expect(toggle).toHaveAttribute('aria-expanded', 'false');
await toggle.click();
await page.setViewportSize({ width: 770, height: 600 });
await expect(menu).toBeHidden();
await expect(current).toBeFocused();
await page.setViewportSize({ width: 390, height: 600 });
await toggle.click();
expect(await menu.boundingBox()).toMatchObject({ x: 48, y: 0, width: 342, height: 600 });
await menu.getByRole('link', { name: 'Case studies' }).click();
await expect(page).toHaveURL('/case-studies');
await expect(menu).toBeHidden();
});
for (const [width, compact] of [[390, true], [736, true], [737, false], [1200, false]] as const) {
test(`shows the correct navigation at ${width}px`, async ({ page }) => {
await page.setViewportSize({ width, height: 850 });
await page.goto('/trailhead');
const header = page.getByRole('banner');
const nav = header.getByRole('navigation', { name: 'Main navigation' });
const menu = header.getByRole('button', { name: 'Menu' });
await expect(menu).toHaveCount(compact ? 1 : 0);
await expect(nav).toHaveCount(compact ? 0 : 1);
const logo = await header.getByRole('link', { name: 'Trailhead.' }).boundingBox();
const rightControl = await header.getByRole('button', { name: compact ? 'Menu' : 'Sign in' }).boundingBox();
expect(logo).not.toBeNull();
expect(rightControl).not.toBeNull();
expect(Math.abs(logo!.x - (width - rightControl!.x - rightControl!.width))).toBeLessThan(2);
});
}
test('the mobile menu opens, closes with Escape, and resets after resizing', async ({ page }) => {
await page.setViewportSize({ width: 390, height: 844 });
await page.goto('/trailhead');
const menu = page.getByRole('button', { name: 'Menu' });
const nav = page.getByRole('navigation', { name: 'Main navigation' });
const hikes = nav.getByRole('link', { name: 'Hikes' });
const dialog = page.getByRole('dialog', { name: 'Menu' });
await menu.click();
await expect(page.getByRole('button', { name: 'Menu', exact: true, includeHidden: true }))
.toHaveAttribute('aria-expanded', 'true');
const close = dialog.getByRole('button', { name: 'Close menu' });
await expect(close).toBeFocused();
await page.locator('#search').evaluate((search) => search.focus());
await expect(close).toBeFocused();
await page.keyboard.press('Tab');
await expect(hikes).toBeFocused();
await page.keyboard.press('Escape');
await expect(menu).toBeFocused();
await expect(menu).toHaveAttribute('aria-expanded', 'false');
await expect(nav).toBeHidden();
await menu.click();
await page.setViewportSize({ width: 1024, height: 844 });
await expect(nav).toBeVisible();
await expect(menu).toBeHidden();
await expect(dialog).toBeHidden();
await expect(hikes).toBeFocused();
await page.setViewportSize({ width: 390, height: 844 });
await expect(menu).toHaveAttribute('aria-expanded', 'false');
await expect(nav).toBeHidden();
});
test('the footer stacks at tablet width and keeps its three navigation links on one row', async ({ page }) => {
await page.setViewportSize({ width: 607, height: 800 });
await page.goto('/trailhead/about');
const footer = page.getByRole('contentinfo');
const brand = await footer.getByRole('link', { name: 'Trailhead.' }).boundingBox();
const nav = footer.getByRole('navigation', { name: 'Footer navigation' });
const links = await Promise.all(['Hikes', 'About us', 'Contact'].map((name) =>
nav.getByRole('link', { name }).boundingBox()
));
const email = await footer.getByRole('link', { name: 'sevelova.an@gmail.com' }).boundingBox();
expect(brand).not.toBeNull();
expect(email).not.toBeNull();
expect(links.every((link) => link !== null && link.y === links[0]?.y)).toBe(true);
expect(links[0]!.y).toBeGreaterThan(brand!.y + brand!.height);
expect(email!.y).toBeGreaterThan(links[0]!.y + links[0]!.height);
expect(Math.abs(email!.x - brand!.x)).toBeLessThan(2);
});
test('wheel input cannot scroll the page behind the menu and works again after closing it', async ({ page }) => {
await page.setViewportSize({ width: 390, height: 650 });
await page.goto('/trailhead');
await expect(page.getByRole('article').first()).toBeVisible();
const menu = page.getByRole('button', { name: 'Menu' });
await menu.click();
const dialog = page.getByRole('dialog', { name: 'Menu' });
const lockedPosition = await page.evaluate(() => window.scrollY);
await expect(page.locator('html')).toHaveCSS('overflow', 'hidden');
await page.mouse.move(190, 600);
await page.mouse.wheel(0, 600);
await page.evaluate(() => new Promise<void>((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))));
expect(await page.evaluate(() => window.scrollY)).toBe(lockedPosition);
await dialog.getByRole('button', { name: 'Close menu' }).click();
await expect(dialog).toBeHidden();
await expect(menu).toBeFocused();
expect(await page.evaluate(() => window.scrollY)).toBe(lockedPosition);
await page.mouse.move(190, 600);
await page.mouse.wheel(0, 600);
await expect.poll(() => page.evaluate(() => window.scrollY)).toBeGreaterThan(lockedPosition);
});