|
| 1 | +import { expect, type Page, test } from "@playwright/test"; |
| 2 | + |
| 3 | +const layoutWidths = [900, 1728]; |
| 4 | +const maxLayoutDelta = 1; |
| 5 | +const daysInWeek = 7; |
| 6 | + |
| 7 | +test.describe("Week view layout", () => { |
| 8 | + for (const width of layoutWidths) { |
| 9 | + test(`aligns day headers with calendar columns at ${width}px`, async ({ |
| 10 | + page, |
| 11 | + }) => { |
| 12 | + await page.setViewportSize({ width, height: 1000 }); |
| 13 | + await page.goto("/week"); |
| 14 | + await page.locator("#allDayColumns").waitFor(); |
| 15 | + await page.locator("#timedColumns").waitFor(); |
| 16 | + |
| 17 | + const layout = await getWeekColumnLayout(page); |
| 18 | + const mainGridScrollbarWidth = await page |
| 19 | + .locator("#mainGrid") |
| 20 | + .evaluate( |
| 21 | + (node) => getComputedStyle(node, "::-webkit-scrollbar").width, |
| 22 | + ); |
| 23 | + const horizontalScrollState = await getHorizontalScrollState(page); |
| 24 | + |
| 25 | + expect(layout.allDayColumns).toHaveLength(daysInWeek); |
| 26 | + expect(layout.dayLabels).toHaveLength(daysInWeek); |
| 27 | + expect(layout.timedColumns).toHaveLength(daysInWeek); |
| 28 | + expect(mainGridScrollbarWidth).toBe("0px"); |
| 29 | + expect(horizontalScrollState.scrollbarHeight).toBe("0px"); |
| 30 | + if (width === 900) { |
| 31 | + expect(horizontalScrollState.isScrollable).toBe(true); |
| 32 | + await expectWeekGridCanScrollHorizontally(page); |
| 33 | + } else { |
| 34 | + expect(horizontalScrollState.isScrollable).toBe(false); |
| 35 | + } |
| 36 | + |
| 37 | + for (const [index, dayLabel] of layout.dayLabels.entries()) { |
| 38 | + expectColumnsToAlign(dayLabel, layout.allDayColumns[index]); |
| 39 | + expectColumnsToAlign(dayLabel, layout.timedColumns[index]); |
| 40 | + } |
| 41 | + }); |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +const getWeekColumnLayout = async (page: Page) => |
| 46 | + page.evaluate((daysInView) => { |
| 47 | + const roundRect = (rect: DOMRect) => ({ |
| 48 | + right: Math.round(rect.right * 100) / 100, |
| 49 | + width: Math.round(rect.width * 100) / 100, |
| 50 | + x: Math.round(rect.x * 100) / 100, |
| 51 | + }); |
| 52 | + |
| 53 | + const dayLabels = [ |
| 54 | + ...document.querySelectorAll("#weekGridScroller [title]"), |
| 55 | + ] |
| 56 | + .filter((node): node is HTMLElement => node instanceof HTMLElement) |
| 57 | + .slice(0, daysInView) |
| 58 | + .map((node) => roundRect(node.getBoundingClientRect())); |
| 59 | + |
| 60 | + const getColumns = (selector: string) => |
| 61 | + [...document.querySelectorAll(selector)] |
| 62 | + .filter((node): node is HTMLElement => node instanceof HTMLElement) |
| 63 | + .map((node) => { |
| 64 | + const rect = node.getBoundingClientRect(); |
| 65 | + |
| 66 | + return { |
| 67 | + ...roundRect(rect), |
| 68 | + height: rect.height, |
| 69 | + }; |
| 70 | + }) |
| 71 | + .filter((rect) => rect.height > 20) |
| 72 | + .slice(0, daysInView); |
| 73 | + |
| 74 | + return { |
| 75 | + allDayColumns: getColumns("#allDayColumns > div"), |
| 76 | + dayLabels, |
| 77 | + timedColumns: getColumns("#timedColumns > div"), |
| 78 | + }; |
| 79 | + }, daysInWeek); |
| 80 | + |
| 81 | +const getHorizontalScrollState = async (page: Page) => |
| 82 | + page.locator("#weekGridScroller").evaluate((node) => { |
| 83 | + return { |
| 84 | + isScrollable: node.scrollWidth > node.clientWidth, |
| 85 | + scrollbarHeight: getComputedStyle(node, "::-webkit-scrollbar").height, |
| 86 | + }; |
| 87 | + }); |
| 88 | + |
| 89 | +const expectWeekGridCanScrollHorizontally = async (page: Page) => { |
| 90 | + const scrollLeft = await page |
| 91 | + .locator("#weekGridScroller") |
| 92 | + .evaluate((node) => { |
| 93 | + node.scrollLeft = node.scrollWidth; |
| 94 | + return node.scrollLeft; |
| 95 | + }); |
| 96 | + |
| 97 | + expect(scrollLeft).toBeGreaterThan(0); |
| 98 | +}; |
| 99 | + |
| 100 | +const expectColumnsToAlign = ( |
| 101 | + dayLabel: { right: number; width: number; x: number }, |
| 102 | + gridColumn: { right: number; width: number; x: number }, |
| 103 | +) => { |
| 104 | + expect(Math.abs(dayLabel.x - gridColumn.x)).toBeLessThanOrEqual( |
| 105 | + maxLayoutDelta, |
| 106 | + ); |
| 107 | + expect(Math.abs(dayLabel.right - gridColumn.right)).toBeLessThanOrEqual( |
| 108 | + maxLayoutDelta, |
| 109 | + ); |
| 110 | + expect(Math.abs(dayLabel.width - gridColumn.width)).toBeLessThanOrEqual( |
| 111 | + maxLayoutDelta, |
| 112 | + ); |
| 113 | +}; |
0 commit comments