File size: 2,582 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
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({ viewport: { width: 1000, height: 720 } });
  await page.goto(pathToFileURL(path.join(process.cwd(), 'index.html')).href, { waitUntil: 'domcontentloaded' });

  // Upload control must exist.
  const upload = page.locator('[data-testid="image-upload"]');
  if (!(await upload.count())) throw new Error('page should expose an image upload control');

  // A local store abstraction is required for the fixture.
  const hasStore = await page.locator('[data-testid="image-store"], [data-storage], [data-testid*="storage"]').count();
  if(!await page.evaluate(()=>typeof window.__getImageStore==='function'))throw Error('image storage observer missing');

  // Preview + zoom controls are required.
  const preview = page.locator('[data-testid="image-preview"]');
  const zoom = page.locator('[data-testid="zoom-button"], [data-testid*="zoom"], button[aria-label*="zoom" i]');
  if (!(await preview.count()) || !(await zoom.count())) throw new Error('image preview and zoom controls are required');

  // Simulate an upload and confirm the image is rendered in the gallery/list.
  const dataUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFgwJ/luznTQAAAABJRU5ErkJggg==';
  await page.evaluate(async src => {
    const blob = await (await fetch(src)).blob();
    const file = new File([blob], 'sample.png', { type: 'image/png' });
    const input = document.querySelector('[data-testid="image-upload"]');
    const dt = new DataTransfer();
    dt.items.add(file);
    input.files = dt.files;
    input.dispatchEvent(new Event('change', { bubbles: true }));
  }, dataUrl);
  await page.waitForFunction(() => window.__getImageStore().length === 1, null, {timeout: 5000});

  const renderedImage = await page.locator(
    '[data-testid="image-list"] img, [data-testid="uploaded-image"], [data-testid*="gallery"] img, main img'
  ).count();
  if (!renderedImage) throw new Error('uploaded image should render in the gallery/list');

  // Clicking an uploaded image should open the preview modal.
  const firstImg = page.locator('[data-testid="uploaded-image"], [data-testid="image-list"] img, main img').first();
  await firstImg.click();
  await preview.first().waitFor({state: 'visible', timeout: 5000});

  await browser.close();
})().catch(err => { console.error(err && err.stack || err); process.exit(1); });