forked from antvis/G
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.ts
62 lines (55 loc) · 1.75 KB
/
generator.ts
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
61
62
import { chromium, devices } from 'playwright';
import './utils/useSnapshotMatchers';
import { sleep } from './utils/sleep';
export function generateCanvasTestCase(
renderer: 'canvas' | 'svg' | 'webgl',
namespace: string,
tests: Record<string, any>,
params?: Partial<{
width: number;
height: number;
}>,
) {
const { width = 640, height = 640 } = params || {};
Object.keys(tests).forEach((key) => {
if (!tests[key].skip) {
it(key, async () => {
// Setup
const browser = await chromium.launch({
args: ['--headless', '--no-sandbox'],
});
const context = await browser.newContext(devices['Desktop Chrome']);
const page = await context.newPage();
await page.addInitScript(
({ renderer, width, height }) => {
window['USE_PLAYWRIGHT'] = 1;
window['DEFAULT_RENDERER'] = renderer;
window['CANVAS_WIDTH'] = width;
window['CANVAS_HEIGHT'] = height;
},
{
renderer,
width,
height,
},
);
// Go to test page served by vite devServer.
const url = `http://localhost:${globalThis.PORT}/?name=${namespace}-${key}`;
await page.goto(url);
await sleep(300);
// Chart already rendered, capture into buffer.
const buffer = await page
.locator(`#container ${renderer === 'svg' ? 'svg' : 'canvas'}`)
.screenshot();
const dir = `${__dirname}/snapshots/${namespace}/${renderer}`;
try {
const maxError = 0;
expect(buffer).toMatchCanvasSnapshot(dir, key, { maxError });
} finally {
await context.close();
await browser.close();
}
});
}
});
}