Fix tests and use proper types for disregarding XMLComponent

This commit is contained in:
Dolan
2018-09-20 00:41:57 +01:00
parent fc71ebdfef
commit f2b50478bf
14 changed files with 68 additions and 44 deletions

View File

@ -8,7 +8,7 @@ export abstract class BaseXmlComponent {
this.rootKey = rootKey;
}
public abstract prepForXml(): IXmlableObject;
public abstract prepForXml(): IXmlableObject | undefined;
public get IsDeleted(): boolean {
return this.deleted;

View File

@ -1,7 +1,7 @@
/* tslint:disable */
import { XmlComponent, IXmlableObject } from ".";
// tslint:disable:no-any
import * as fastXmlParser from "fast-xml-parser";
import { flatMap } from "lodash";
import { IXmlableObject, XmlComponent } from ".";
export const parseOptions = {
ignoreAttributes: false,
@ -54,8 +54,27 @@ export function convertToXmlComponent(elementName: string, element: any): Import
* Represents imported xml component from xml file.
*/
export class ImportedXmlComponent extends XmlComponent {
private _attr: any;
/**
* Converts the xml string to a XmlComponent tree.
*
* @param importedContent xml content of the imported component
*/
public static fromXmlString(importedContent: string): ImportedXmlComponent {
const imported = fastXmlParser.parse(importedContent, parseOptions);
const elementName = Object.keys(imported)[0];
const converted = convertToXmlComponent(elementName, imported[elementName]);
if (Array.isArray(converted) && converted.length > 1) {
throw new Error("Invalid conversion, input must be one element.");
}
return Array.isArray(converted) ? converted[0] : converted;
}
// tslint:disable-next-line:variable-name
private readonly _attr: any;
// tslint:disable-next-line:variable-name
constructor(rootKey: string, _attr?: any) {
super(rootKey);
if (_attr) {
@ -89,8 +108,12 @@ export class ImportedXmlComponent extends XmlComponent {
* ]
* }
*/
prepForXml(): IXmlableObject {
public prepForXml(): IXmlableObject | undefined {
const result = super.prepForXml();
if (!result) {
return undefined;
}
if (!!this._attr) {
if (!Array.isArray(result[this.rootKey])) {
result[this.rootKey] = [result[this.rootKey]];
@ -100,33 +123,17 @@ export class ImportedXmlComponent extends XmlComponent {
return result;
}
push(xmlComponent: XmlComponent) {
public push(xmlComponent: XmlComponent): void {
this.root.push(xmlComponent);
}
/**
* Converts the xml string to a XmlComponent tree.
*
* @param importedContent xml content of the imported component
*/
static fromXmlString(importedContent: string): ImportedXmlComponent {
const imported = fastXmlParser.parse(importedContent, parseOptions);
const elementName = Object.keys(imported)[0];
const converted = convertToXmlComponent(elementName, imported[elementName]);
if (Array.isArray(converted) && converted.length > 1) {
throw new Error("Invalid conversion, input must be one element.");
}
return Array.isArray(converted) ? converted[0] : converted;
}
}
/**
* Used for the attributes of root element that is being imported.
*/
export class ImportedRootElementAttributes extends XmlComponent {
constructor(private _attr: any) {
// tslint:disable-next-line:variable-name
constructor(private readonly _attr: any) {
super("");
}

View File

@ -26,6 +26,11 @@ describe("XmlComponent", () => {
xmlComponent.addChildElement(child);
const xml = xmlComponent.prepForXml();
if (!xml) {
return;
}
assert.equal(xml["w:test"].length, 0);
});
});

View File

@ -7,10 +7,10 @@ export abstract class XmlComponent extends BaseXmlComponent {
constructor(rootKey: string) {
super(rootKey);
this.root = new Array<BaseXmlComponent>();
this.root = new Array<BaseXmlComponent | string>();
}
public prepForXml(): IXmlableObject {
public prepForXml(): IXmlableObject | undefined {
const children = this.root
.filter((c) => {
if (c instanceof BaseXmlComponent) {
@ -24,13 +24,12 @@ export abstract class XmlComponent extends BaseXmlComponent {
}
return comp;
})
.filter((comp) => comp); // Exclude null, undefined, and empty strings
.filter((comp) => comp !== undefined); // Exclude undefined
return {
[this.rootKey]: children,
};
}
// TODO: Unused method
public addChildElement(child: XmlComponent | string): XmlComponent {
this.root.push(child);