File size: 3,803 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
(async () => {
  const browser = await chromium.launch({ headless: true });
  const page = await browser.newPage();
  const errors = [];
  page.on('pageerror', err => errors.push(err.message));
  await page.setViewportSize({ width: 1200, height: 800 });
  await page.goto(pathToFileURL(path.join(process.cwd(), 'index.html')).href, { waitUntil: 'domcontentloaded' });
  let boxes = await page.evaluate(() => {
    const toBox = rect => ({ left: rect.left, right: rect.right, width: rect.width });
    const sidebar = toBox(document.querySelector('[data-testid="sidebar"]').getBoundingClientRect());
    const main = toBox(document.querySelector('[data-testid="main-content"]').getBoundingClientRect());
    return { sidebar, main, scrollWidth: document.documentElement.scrollWidth, clientWidth: document.documentElement.clientWidth };
  });
  if (!(boxes.sidebar.left <= 2 && boxes.main.left > boxes.sidebar.right - 1)) {
    throw new Error('sidebar must be left of main content in horizontal layout');
  }
  await page.locator('[data-testid="sidebar-toggle"]').click();
  await page.waitForFunction(() => {
    const el = document.querySelector('[data-testid="sidebar"]');
    const rect = el.getBoundingClientRect();
    const style = getComputedStyle(el);
    return Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left)) < 40 ||
      style.display === 'none' || style.visibility === 'hidden';
  }, null, {timeout: 3000});
  const collapsed = await page.locator('[data-testid="sidebar"]').evaluate(el => {
    const rect = el.getBoundingClientRect();
    const style = getComputedStyle(el);
    const visibleWidth = Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left));
    return visibleWidth < 40 || style.display === 'none' || style.visibility === 'hidden';
  });
  if (!collapsed) throw new Error('sidebar toggle did not collapse the sidebar');
  await page.setViewportSize({ width: 640, height: 800 });
  await page.waitForTimeout(150);
  boxes = await page.evaluate(() => {
    const rect = document.querySelector('[data-testid="main-content"]').getBoundingClientRect();
    const main = { left: rect.left, right: rect.right, width: rect.width };
    return { main, scrollWidth: document.documentElement.scrollWidth, clientWidth: document.documentElement.clientWidth };
  });
  if (boxes.scrollWidth > boxes.clientWidth + 1) throw new Error('narrow viewport should not create horizontal page scroll');
  if (boxes.main.width <= 0) throw new Error('main content should keep a positive minimum width');
  await page.setViewportSize({ width: 1600, height: 900 });
  await page.goto(pathToFileURL(path.join(process.cwd(), 'index.html')).href, { waitUntil: 'domcontentloaded' });
  await page.waitForTimeout(150);
  boxes = await page.evaluate(() => {
    const toBox = rect => ({ left: rect.left, right: rect.right, width: rect.width });
    const sidebar = toBox(document.querySelector('[data-testid="sidebar"]').getBoundingClientRect());
    const main = toBox(document.querySelector('[data-testid="main-content"]').getBoundingClientRect());
    return { sidebar, main, clientWidth: document.documentElement.clientWidth };
  });
  const mainCenter = boxes.main.left + boxes.main.width / 2;
  if (boxes.sidebar.left > 2) throw new Error('wide viewport sidebar should remain left aligned');
  const availableCenter = (boxes.sidebar.right + boxes.clientWidth) / 2;
  if (Math.abs(mainCenter - availableCenter) > 2) throw new Error('wide content is not centered beside the sidebar');
  if (errors.length) throw new Error(errors.join(' | '));
  await browser.close();
})().catch(err => { console.error(err && err.stack || err); process.exit(1); });