forked from antvis/G2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec-interaction.spec.ts
79 lines (73 loc) · 2.29 KB
/
spec-interaction.spec.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Canvas } from '@antv/g';
import { G2Spec } from '../../src';
import * as chartTests from '../plots/interaction';
import { kebabCase } from './utils/kebabCase';
import { filterTests } from './utils/filterTests';
import { sleep } from './utils/sleep';
import { renderSpec } from './utils/renderSpec';
import { compose } from './utils/compose';
import './utils/useSnapshotMatchers';
import './utils/useCustomFetch';
import { disableAnimation, disableAxis } from './utils/preprocess';
function disableTooltip(options): G2Spec {
const discovered = [options];
while (discovered.length) {
const node = discovered.pop();
node.interaction = {
...node.interaction,
tooltip: false,
};
if (node.children) {
discovered.push(...node.children);
}
}
return options;
}
describe('Interactions', () => {
const tests = filterTests(chartTests);
for (const [name, generateOptions] of tests) {
let gCanvas: Canvas | undefined;
it(`[Interaction]: ${name}`, async () => {
try {
// @ts-ignore
const { steps: S } = generateOptions;
if (!S) {
throw new Error(`Missing steps for ${name}`);
}
// Disable animations and delays.
const {
// @ts-ignore
preprocess = (d) => d,
// @ts-ignore
tooltip = false,
} = generateOptions;
// @ts-ignore
generateOptions.preprocess = compose([
preprocess,
disableAnimation,
disableAxis,
tooltip ? (d) => d : disableTooltip,
]);
// Render chart.
const gCanvas = await renderSpec(generateOptions);
// Asset each state.
// @ts-ignore
const steps = S({ canvas: gCanvas });
const dir = `${__dirname}/snapshots/interaction/${kebabCase(name)}`;
for (let i = 0; i < steps.length; i++) {
// Dispatch event and wait for the next tick and rerender.
// @ts-ignore
const { changeState, skip } = steps[i];
await changeState();
await sleep(100);
// If do not skip this state, asset it after dispatch the event.
if (!skip) {
await expect(gCanvas).toMatchDOMSnapshot(dir, `step${i}`);
}
}
} finally {
gCanvas?.destroy();
}
});
}
});