Skip to content

Commit

Permalink
test: reset entity counter in each test, update snapshots (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca authored Oct 23, 2024
1 parent 4a5a129 commit 470d756
Show file tree
Hide file tree
Showing 315 changed files with 3,156 additions and 3,194 deletions.
13 changes: 1 addition & 12 deletions __tests__/integration/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,9 @@ const View: React.FC = () => {
return renderers[renderer];
}, [renderer]);

const destroyShapeRecursive = (shape?: DisplayObject) => {
if (!shape) return;
if (shape.childNodes.length > 0) {
for (let index = shape.childNodes.length - 1; index > 0; index--) {
const child = shape.childNodes[index] as DisplayObject;
destroyShapeRecursive(child);
}
}
shape.destroy();
};

const renderCase = (name: string) => {
if (!canvasRef.current || !casesName.includes(name)) return;
destroyShapeRecursive(nodeRef.current);
nodeRef.current?.destroy();
const node = cases[name]();
nodeRef.current = node;
const title = cases[name].tags || [name];
Expand Down
14 changes: 10 additions & 4 deletions __tests__/integration/snapshot.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-restricted-syntax */
import * as fs from 'fs';
import { Canvas } from '@antv/g';
import { Canvas, resetEntityCounter } from '@antv/g';
import { format } from 'prettier';
import xmlserializer from 'xmlserializer';
import * as tests from './components';
Expand All @@ -17,23 +18,28 @@ describe('integration', () => {

const finalTests = onlyTests.length === 0 ? tests : Object.fromEntries(onlyTests);

if (!fs.existsSync(`${__dirname}/snapshots`)) {
fs.mkdirSync(`${__dirname}/snapshots`);
}

for (const [name, target] of Object.entries(finalTests)) {
// @ts-ignore
if (!target.skip) {
it(`[Canvas]: ${name}`, async () => {
resetEntityCounter();
let canvas: Canvas | undefined;
let actual: string;
try {
const actualPath = `${__dirname}/snapshots/${name}-actual.svg`;
const expectedPath = `${__dirname}/snapshots/${name}.svg`;
const options = await target();
const options = target();
// @ts-ignore
const wait = target.wait;
canvas = await renderCanvas(options, wait);
const container = canvas.getConfig().container as HTMLElement;
const dom = container.querySelector('svg');

actual = await format(xmlserializer.serializeToString(dom as any), {
actual = format(xmlserializer.serializeToString(dom as any), {
parser: 'babel',
});

Expand All @@ -46,7 +52,7 @@ describe('integration', () => {
throw new Error(`Please generate golden image for ${name}`);
}
console.warn(`! generate ${name}`);
await fs.writeFileSync(expectedPath, actual);
fs.writeFileSync(expectedPath, actual);
} else {
const expected = fs.readFileSync(expectedPath, {
encoding: 'utf8',
Expand Down
Loading

0 comments on commit 470d756

Please sign in to comment.