2018-10-31 21:57:10 +00:00
|
|
|
import { expect } from "chai";
|
|
|
|
import { Element, xml2js } from "xml-js";
|
2018-05-28 09:15:44 +02:00
|
|
|
|
2019-04-10 13:47:38 -04:00
|
|
|
import { EMPTY_OBJECT, ImportedXmlComponent } from "./";
|
2021-03-11 01:06:55 +00:00
|
|
|
import { IContext } from "./base";
|
2018-10-31 21:57:10 +00:00
|
|
|
import { convertToXmlComponent } from "./imported-xml-component";
|
2018-05-28 09:15:44 +02:00
|
|
|
|
2018-10-31 21:57:10 +00:00
|
|
|
const xmlString = `
|
|
|
|
<w:p w:one="value 1" w:two="value 2">
|
|
|
|
<w:rPr>
|
|
|
|
<w:noProof>some value</w:noProof>
|
|
|
|
</w:rPr>
|
|
|
|
<w:r active="true">
|
|
|
|
<w:t>Text 1</w:t>
|
|
|
|
</w:r>
|
|
|
|
<w:r active="true">
|
|
|
|
<w:t>Text 2</w:t>
|
|
|
|
</w:r>
|
|
|
|
</w:p>
|
|
|
|
`;
|
2018-05-28 09:15:44 +02:00
|
|
|
|
2018-10-31 21:57:10 +00:00
|
|
|
const convertedXmlElement = {
|
|
|
|
deleted: false,
|
|
|
|
root: [
|
|
|
|
{
|
|
|
|
deleted: false,
|
|
|
|
rootKey: "w:p",
|
|
|
|
root: [
|
2019-04-09 05:27:18 -04:00
|
|
|
{ deleted: false, rootKey: "_attr", root: { "w:one": "value 1", "w:two": "value 2" } },
|
2018-10-31 21:57:10 +00:00
|
|
|
{ deleted: false, rootKey: "w:rPr", root: [{ deleted: false, rootKey: "w:noProof", root: ["some value"] }] },
|
2019-04-09 05:27:18 -04:00
|
|
|
{
|
|
|
|
deleted: false,
|
|
|
|
rootKey: "w:r",
|
|
|
|
root: [
|
|
|
|
{ deleted: false, rootKey: "_attr", root: { active: "true" } },
|
|
|
|
{ deleted: false, rootKey: "w:t", root: ["Text 1"] },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
deleted: false,
|
|
|
|
rootKey: "w:r",
|
|
|
|
root: [
|
|
|
|
{ deleted: false, rootKey: "_attr", root: { active: "true" } },
|
|
|
|
{ deleted: false, rootKey: "w:t", root: ["Text 2"] },
|
|
|
|
],
|
|
|
|
},
|
2018-10-31 21:57:10 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
rootKey: undefined,
|
|
|
|
};
|
2018-03-28 14:54:07 +02:00
|
|
|
|
2018-10-31 21:57:10 +00:00
|
|
|
describe("ImportedXmlComponent", () => {
|
|
|
|
let importedXmlComponent: ImportedXmlComponent;
|
2018-03-28 14:54:07 +02:00
|
|
|
|
2018-10-31 21:57:10 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
const attributes = {
|
|
|
|
someAttr: "1",
|
|
|
|
otherAttr: "2",
|
|
|
|
};
|
|
|
|
importedXmlComponent = new ImportedXmlComponent("w:test", attributes);
|
|
|
|
importedXmlComponent.push(new ImportedXmlComponent("w:child"));
|
|
|
|
});
|
2018-03-28 14:54:07 +02:00
|
|
|
|
2018-10-31 21:57:10 +00:00
|
|
|
describe("#prepForXml()", () => {
|
|
|
|
it("should transform for xml", () => {
|
2021-03-11 01:06:55 +00:00
|
|
|
// tslint:disable-next-line: no-object-literal-type-assertion
|
|
|
|
const converted = importedXmlComponent.prepForXml({} as IContext);
|
2019-01-11 01:45:57 +00:00
|
|
|
expect(converted).to.deep.equal({
|
2018-10-31 21:57:10 +00:00
|
|
|
"w:test": [
|
|
|
|
{
|
|
|
|
_attr: {
|
|
|
|
someAttr: "1",
|
|
|
|
otherAttr: "2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-04-10 13:47:38 -04:00
|
|
|
"w:child": EMPTY_OBJECT,
|
2018-10-31 21:57:10 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-05-28 09:15:44 +02:00
|
|
|
|
2018-10-31 21:57:10 +00:00
|
|
|
it("should create XmlComponent from xml string", () => {
|
|
|
|
const converted = ImportedXmlComponent.fromXmlString(xmlString);
|
2019-01-11 01:45:57 +00:00
|
|
|
expect(converted).to.deep.equal(convertedXmlElement);
|
2018-10-31 21:57:10 +00:00
|
|
|
});
|
2018-05-28 09:15:44 +02:00
|
|
|
|
2018-10-31 21:57:10 +00:00
|
|
|
describe("convertToXmlComponent", () => {
|
|
|
|
it("should convert to xml component", () => {
|
|
|
|
const xmlObj = xml2js(xmlString, { compact: false }) as Element;
|
|
|
|
const converted = convertToXmlComponent(xmlObj);
|
2019-01-11 01:45:57 +00:00
|
|
|
expect(converted).to.deep.equal(convertedXmlElement);
|
2018-10-31 21:57:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|