Skip to content

Commit

Permalink
test: remove id in snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca committed Oct 21, 2024
1 parent 14c6ba1 commit 6df10d8
Show file tree
Hide file tree
Showing 314 changed files with 5,959 additions and 18,260 deletions.
37 changes: 34 additions & 3 deletions __tests__/integration/snapshot.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 @@ -9,6 +9,29 @@ import { fetch } from './fetch';
// @ts-ignore
global.fetch = fetch;

const removeId = (svg: string, reserved?: Map<string, number>) => {
if (!reserved) return svg.replace(/ *id="[^"]*" */g, ' ');
return svg.replace(/ *id="([^"]*)" *| *href="#([^"]*)" *|url\(#([^"]*)\)/g, (match, id, href, url) => {
const value = id || href || url;
const index = reserved.get(value);
if (index !== undefined) return match.replace(value, `ref-${index}`);
return ' ';
});
};

const formatSVG = (svg: string) => {
if (!svg.includes('<defs>')) return svg; // svg defs, it's a little complex to handle

let counter = 0;
const refs = new Map<string, number>();

svg.match(/href="#[^"]*"/g)?.forEach((ref) => refs.set(ref.slice(7, -1), counter++));

const [before, after] = svg.split('</defs>');

return `${before}</defs>${removeId(after, refs)}`.replace(/\r\n|\n\s+\n/g, '\n');
};

describe('integration', () => {
const onlyTests = Object.entries(tests).filter(
// @ts-ignore
Expand All @@ -17,10 +40,18 @@ describe('integration', () => {

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

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

beforeEach(() => {
resetEntityCounter();
});

for (const [name, target] of Object.entries(finalTests)) {

Check warning on line 51 in __tests__/integration/snapshot.spec.ts

View workflow job for this annotation

GitHub Actions / build

iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations

Check warning on line 51 in __tests__/integration/snapshot.spec.ts

View workflow job for this annotation

GitHub Actions / build

iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations
// @ts-ignore
if (!target.skip) {
it(`[Canvas]: ${name}`, async () => {
it.concurrent(`[Canvas]: ${name}`, async () => {
let canvas: Canvas | undefined;
let actual: string;
try {
Expand All @@ -33,7 +64,7 @@ describe('integration', () => {
const container = canvas.getConfig().container as HTMLElement;
const dom = container.querySelector('svg');

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

Expand Down
Loading

0 comments on commit 6df10d8

Please sign in to comment.