import 'jest-puppeteer'; import { gotoStory } from '../../../../helpers/test'; type Bar = { current: string | null, last: string | null, next: string | null, prev: string | null, size: string }; function readBar(): Promise { return page.evaluate(() => { const items = document.querySelectorAll('[data-scope="pagination"][data-part="item"]'); return { current: document.querySelector('[data-scope="pagination"][data-part="item"][aria-current="page"]')?.textContent ?? null, last: items[items.length - 1]?.textContent ?? null, next: document.querySelector('[data-part="next-trigger"]')?.getAttribute('href') ?? null, prev: document.querySelector('[data-part="prev-trigger"]')?.getAttribute('href') ?? null, size: document.querySelector('[data-ods="pagination-page-size-selector"] [data-part="trigger"]')?.textContent ?? '', }; }); } async function selectPageSize(size: number): Promise { const trigger = await page.waitForSelector('[data-ods="pagination-page-size-selector"] [data-scope="select"][data-part="trigger"]'); await trigger?.click(); const option = await page.waitForSelector(`[data-scope="select"][data-part="item"][data-value="${size}"]`, { visible: true }); await option?.click(); } describe('Pagination navigation', () => { describe('link mode', () => { it('should navigate when a page link is activated', async() => { await gotoStory(page, 'navigation/link'); await page.waitForSelector('[data-testid="link"]'); const target = await page.waitForSelector('[data-part="item"]:not([aria-current="page"])'); const href = await target?.evaluate((el: Element) => el.getAttribute('href')); expect(href).toBe('#page-2-size-10'); await target?.click(); expect(await page.evaluate(() => window.location.hash)).toBe('#page-2-size-10'); }); it('should follow the next trigger to the page it points at', async() => { await gotoStory(page, 'navigation/link'); await page.waitForSelector('[data-testid="link"]'); const trigger = await page.waitForSelector('[data-part="next-trigger"]'); const href = await trigger?.evaluate((el: Element) => el.getAttribute('href')); expect(href).toBe('#page-2-size-10'); await trigger?.click(); expect(await page.evaluate(() => window.location.hash)).toBe(href); }); it('should walk the pages with the triggers when the page comes from the url', async() => { await gotoStory(page, 'navigation/link-from-url'); await page.waitForSelector('[data-testid="link-from-url"]'); async function activate(part: string): Promise { const trigger = await page.waitForSelector(`[data-part="${part}"]`); await trigger?.click(); return page.evaluate(() => window.location.hash); } expect(await activate('next-trigger')).toBe('#page-2-size-10'); expect(await activate('next-trigger')).toBe('#page-3-size-10'); expect(await activate('prev-trigger')).toBe('#page-2-size-10'); }); it('should hand linkAs the controls that lead somewhere, and only those', async() => { await gotoStory(page, 'navigation/link-as'); await page.waitForSelector('[data-testid="link-as"]'); const rendered = await page.evaluate(() => [...document.querySelectorAll('[data-part="prev-trigger"], [data-part="item"], [data-part="next-trigger"]')] .map((el) => ({ hasHref: el.hasAttribute('href'), part: el.getAttribute('data-part'), routed: el.hasAttribute('data-router-link') }))); // On the first page neither the previous trigger nor the active page has a destination. expect(rendered[0]).toEqual({ hasHref: false, part: 'prev-trigger', routed: false }); expect(rendered[1]).toEqual({ hasHref: false, part: 'item', routed: false }); expect(rendered[rendered.length - 1]).toEqual({ hasHref: true, part: 'next-trigger', routed: true }); rendered.forEach(({ hasHref, routed }) => { expect(routed).toBe(hasHref); }); }); it('should hit a page from anywhere in its cell, not only on the digits', async() => { await gotoStory(page, 'navigation/link'); await page.waitForSelector('[data-testid="link"]'); const target = await page.waitForSelector('[data-part="item"]:not([aria-current="page"])'); // The corner of the 40px cell: several pixels clear of the digits, on the overlay only. const corner = await target?.evaluate((el: Element) => { const { left, top } = (el.parentElement as HTMLElement).getBoundingClientRect(); return { x: left + 2, y: top + 2 }; }); await page.mouse.click(corner!.x, corner!.y); expect(await page.evaluate(() => window.location.hash)).toBe('#page-2-size-10'); }); it('should hit a trigger from anywhere in its cell, not only on the chevron', async() => { await gotoStory(page, 'navigation/link'); await page.waitForSelector('[data-testid="link"]'); const trigger = await page.waitForSelector('[data-part="next-trigger"]'); const corner = await trigger?.evaluate((el: Element) => { const { left, top } = (el.parentElement as HTMLElement).getBoundingClientRect(); return { x: left + 2, y: top + 2 }; }); await page.mouse.click(corner!.x, corner!.y); expect(await page.evaluate(() => window.location.hash)).toBe('#page-2-size-10'); }); it('should stay silent when a link is followed, as the URL is the only signal', async() => { await gotoStory(page, 'navigation/link-reporting'); await page.waitForSelector('[data-testid="link-reporting"]'); const target = await page.waitForSelector('[data-part="item"]:not([aria-current="page"])'); await target?.click(); // A hash navigation keeps the document alive, so the recorder survives the click. expect(await page.evaluate(() => window.location.hash)).toMatch(/^#page-\d+-size-10$/); // Why nothing is reported: see handlePageChange in usePagination. expect(await page.evaluate(() => (window as unknown as { __pageChanges?: unknown[] }).__pageChanges ?? [])).toEqual([]); }); it('should report the wanted page from the go-to-page form, which is not a link', async() => { await gotoStory(page, 'navigation/link-reporting'); await page.waitForSelector('[data-testid="link-reporting"]'); await page.type('[data-ods="pagination-page-selector"] input', '7'); await page.click('[data-ods="pagination-page-selector"] button[type="submit"]'); expect(await page.evaluate(() => (window as unknown as { __pageChanges?: unknown[] }).__pageChanges ?? [])) .toEqual([{ page: 7, pageSize: 10 }]); // The form has no href to follow, so the application is the one that navigates. expect(await page.evaluate(() => window.location.hash)).toBe(''); }); it('should not report a page the go-to-page form brings back to the active one', async() => { await gotoStory(page, 'navigation/link-reporting'); await page.waitForSelector('[data-testid="link-reporting"]'); await page.type('[data-ods="pagination-page-selector"] input', '99'); await page.click('[data-ods="pagination-page-selector"] button[type="submit"]'); expect(await page.evaluate(() => (window as unknown as { __pageChanges?: unknown[] }).__pageChanges ?? [])).toEqual([]); }); it('should keep the triggers announced as links when they carry a tooltip', async() => { await gotoStory(page, 'navigation/link-with-tooltips'); await page.waitForSelector('[data-testid="link-with-tooltips"]'); const triggers = await page.evaluate(() => [...document.querySelectorAll('[data-ods="tooltip-trigger"]')] .map((el) => ({ href: el.getAttribute('href'), role: el.getAttribute('role'), tag: el.tagName }))); // TooltipTrigger hardcodes role="button", see PaginationButtonWithTooltip. expect(triggers).toEqual([ { href: '#page-2-size-10', role: null, tag: 'A' }, { href: '#page-4-size-10', role: null, tag: 'A' }, ]); }); it('should report the wanted size and stay put until the application navigates', async() => { await gotoStory(page, 'navigation/link-size-selection'); await page.waitForSelector('[data-testid="link-size-selection"]'); const before = await readBar(); expect(before).toEqual({ current: '18', last: '20', next: '#page-19-size-10', prev: '#page-17-size-10', size: '10' }); await selectPageSize(100); expect(await page.evaluate(() => (window as unknown as { __pageSizeChanges?: unknown[] }).__pageSizeChanges ?? [])) .toEqual([{ pageSize: 100 }]); expect(await readBar()).toEqual(before); expect(await page.evaluate(() => (window as unknown as { __pageChanges?: unknown[] }).__pageChanges ?? [])).toEqual([]); }); it('should follow the size of the url as it follows its page, history included', async() => { await gotoStory(page, 'navigation/link-from-url'); await page.waitForSelector('[data-testid="link-from-url"]'); // Waits for the render that follows hashchange, which lands after the URL has changed. function waitForNext(href: string): Promise { return page.waitForFunction((expected: string) => document.querySelector('[data-part="next-trigger"]')?.getAttribute('href') === expected, {}, href); } await page.click('[data-part="next-trigger"]'); await waitForNext('#page-3-size-10'); await page.click('[data-part="next-trigger"]'); await waitForNext('#page-4-size-10'); const before = await readBar(); expect(before).toEqual({ current: '3', last: '20', next: '#page-4-size-10', prev: '#page-2-size-10', size: '10' }); await selectPageSize(25); await waitForNext('#page-2-size-25'); expect(await page.evaluate(() => window.location.hash)).toBe('#page-1-size-25'); expect(await readBar()).toEqual({ current: '1', last: '8', next: '#page-2-size-25', prev: null, size: '25' }); await page.goBack(); await waitForNext('#page-4-size-10'); expect(await readBar()).toEqual(before); }); it('should keep each cell its own target when the pages do not fit', async() => { await gotoStory(page, 'navigation/link-narrow'); await page.waitForSelector('[data-testid="link-narrow"]'); // The edge of the last page, next to the chevron of the next trigger. const edge = await page.evaluate(() => { const items = document.querySelectorAll('[data-scope="pagination"][data-part="item"]'); const { right, top } = (items[items.length - 1].parentElement as HTMLElement).getBoundingClientRect(); return { x: right - 2, y: top + 2 }; }); await page.mouse.click(edge.x, edge.y); expect(await page.evaluate(() => window.location.hash)).toBe('#page-20-size-10'); }); it('should keep each cell its own target when the page numbers are wider than a page button', async() => { await gotoStory(page, 'navigation/link-narrow-long-pages'); await page.waitForSelector('[data-testid="link-narrow-long-pages"]'); // The page a click lands on at both edges of every page link, where the digits of a // neighbour would spill over. const reached = await page.evaluate(() => Array.from( document.querySelectorAll('[data-scope="pagination"][data-part="item"]:not([aria-current="page"])'), (item) => { const { bottom, left, right, top } = (item.parentElement as HTMLElement).getBoundingClientRect(); const y = (top + bottom) / 2; return [left + 2, right - 2].map((x) => document.elementFromPoint(x, y)?.closest('[data-part="item"]')?.textContent); }, )); expect(reached).toEqual([ ['1', '1'], ['49999999', '49999999'], ['50000001', '50000001'], ['100000000', '100000000'], ]); }); it('should not navigate when the active page is activated', async() => { await gotoStory(page, 'navigation/link'); await page.waitForSelector('[data-testid="link"]'); await page.click('[data-part="item"][aria-current="page"]'); expect(await page.evaluate(() => window.location.hash)).toBe(''); }); it('should not navigate when the inert previous trigger is clicked', async() => { await gotoStory(page, 'navigation/link'); await page.waitForSelector('[data-testid="link"]'); await page.click('[data-part="prev-trigger"]'); expect(await page.evaluate(() => window.location.hash)).toBe(''); }); it('should keep the inert previous trigger out of the tab order', async() => { await gotoStory(page, 'navigation/link'); const trigger = await page.waitForSelector('[data-part="prev-trigger"]'); await page.keyboard.press('Tab'); expect(await trigger?.evaluate((el: Element) => document.activeElement === el)).toBe(false); }); it('should keep the active page out of the tab order, the way a current page is', async() => { await gotoStory(page, 'navigation/link'); const current = await page.waitForSelector('[data-part="item"][aria-current="page"]'); await page.keyboard.press('Tab'); await page.keyboard.press('Tab'); expect(await current?.evaluate((el: Element) => document.activeElement === el)).toBe(false); }); }); describe('button mode', () => { it('should bring a page past the last one back to the last page when it is submitted', async() => { await gotoStory(page, 'navigation/button-past-last-page'); await page.waitForSelector('[data-testid="button-past-last-page"]'); await page.type('[data-ods="pagination-page-selector"] input', '99'); await page.click('[data-ods="pagination-page-selector"] button[type="submit"]'); await page.waitForSelector('[data-part="item"][aria-current="page"]'); expect(await page.evaluate(() => document.querySelector('[data-part="item"][aria-current="page"]')?.textContent)).toBe('20'); expect(await page.evaluate(() => (window as unknown as { __pageChanges?: unknown[] }).__pageChanges ?? [])) .toEqual([{ page: 20, pageSize: 10 }]); }); }); });