wisemapping-frontend/packages/mindplot/test/unit/import/Helper.ts

57 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-03-10 16:26:24 +01:00
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable import/prefer-default-export */
2022-03-14 21:31:54 +01:00
import fs from 'fs';
2022-03-10 16:26:24 +01:00
import path from 'path';
import { expect } from '@jest/globals';
2022-03-14 21:31:54 +01:00
import { diff } from 'jest-diff';
2022-03-10 16:26:24 +01:00
import Importer from '../../../src/components/import/Importer';
2022-03-18 18:23:08 +01:00
const saveOutputRecord = false;
export const parseXMLString = (xmlStr: string, mimeType: DOMParserSupportedType) => {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlStr, mimeType);
// Is there any parsing error ?.
/*
if (xmlDoc.getElementsByTagName('parsererror').length > 0) {
const xmmStr = new XMLSerializer().serializeToString(xmlDoc);
console.log(xmmStr);
throw new Error(`Unexpected error parsing: ${xmlStr}. Error: ${xmmStr}`);
}
*/
return xmlDoc;
};
export const parseXMLFile = (filePath: fs.PathOrFileDescriptor, mimeType: DOMParserSupportedType) => {
const stream = fs.readFileSync(filePath, { encoding: 'utf-8' });
let content = stream.toString();
// Hack for SVG exported from the browser ...
if (mimeType === 'image/svg+xml') {
content = content.replace('<svg ', '<svg xmlns:xlink="http://www.w3.org/1999/xlink" ');
}
return parseXMLString(content, mimeType);
};
2022-03-10 16:26:24 +01:00
export const exporterAssert = async (testName: string, importer: Importer) => {
const actualMindmap = await importer.import(testName, '');
2022-03-18 18:23:08 +01:00
// Load mindmap file..
2022-03-10 16:26:24 +01:00
const mindmapPath = path.resolve(__dirname, `./expected/${testName}.wxml`);
2022-03-18 18:23:08 +01:00
if (saveOutputRecord) {
fs.writeFileSync(mindmapPath, actualMindmap);
}
const mindmapExpect = fs.readFileSync(mindmapPath).toString();
2022-03-10 16:26:24 +01:00
2022-03-14 21:31:54 +01:00
// Compare with expected...
if (actualMindmap !== mindmapExpect) {
const diffResult = diff(actualMindmap, mindmapExpect);
console.log(diffResult);
2022-03-18 18:23:08 +01:00
expect(actualMindmap).toEqual(mindmapExpect);
2022-03-14 21:31:54 +01:00
}
2022-03-10 16:26:24 +01:00
};