wisemapping-frontend/packages/mindplot/test/unit/export/SVGExporterTestSuite.test.ts

43 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-12-30 02:10:28 +01:00
import Mindmap from '../../../src/components/model/Mindmap';
import fs from 'fs';
import path from 'path';
import XMLSerializerFactory from '../../../src/components/persistence/XMLSerializerFactory';
import SVGExporter from '../../../src/components/export/SVGExporter';
2021-12-31 11:02:29 +01:00
import BinaryImageExporter from '../../../src/components/export/BinaryImageExporter';
2021-12-30 02:10:28 +01:00
2021-12-31 10:18:21 +01:00
test('mindplot generation of simple maps', async () => {
// Load mindmap DOM ...
const mindmapPath = path.resolve(__dirname, './samples/welcome.xml');
const mapDocument = parseXMLFile(mindmapPath, 'text/xml');
2021-12-30 02:10:28 +01:00
// Convert to mindmap ...
const serializer = XMLSerializerFactory.getSerializerFromDocument(mapDocument);
const mindmap: Mindmap = serializer.loadFromDom(mapDocument, 'welcome');
// Load SVG ...
const svgPath = path.resolve(__dirname, './samples/welcome.svg');
const svgDocument = parseXMLFile(svgPath, 'image/svg+xml');
2021-12-30 02:10:28 +01:00
// Inspect ...
2021-12-31 11:02:29 +01:00
const svgExporter = new SVGExporter(mindmap, svgDocument.documentElement);
console.log('Exported map:' + await svgExporter.export());
2021-12-31 10:18:21 +01:00
2021-12-31 11:02:29 +01:00
const pngExporter = new BinaryImageExporter(mindmap, svgDocument.documentElement, 400, 400, 'image/png');
console.log('Exported map:' + await pngExporter.export());
2021-12-31 10:18:21 +01:00
2021-12-30 02:10:28 +01:00
});
2021-12-31 10:18:21 +01:00
function parseXMLFile(filePath: fs.PathOrFileDescriptor, mimeType: DOMParserSupportedType) {
const parser = new DOMParser();
const stream = fs.readFileSync(filePath, { encoding: 'utf-8' });
const xmlDoc = parser.parseFromString(stream.toString(), mimeType);
// Is there any parsing error ?.
if (xmlDoc.getElementsByTagName("parsererror").length > 0) {
console.log(new XMLSerializer().serializeToString(xmlDoc));
throw new Error(`Unexpected error parsing: ${filePath}. Error: ${new XMLSerializer().serializeToString(xmlDoc)}`);
}
return xmlDoc;
}