improve test coverage

This commit is contained in:
Tom Hunkapiller
2021-05-26 10:14:19 +03:00
parent 5a52541136
commit 05a6ab77cc
7 changed files with 182 additions and 20 deletions

View File

@ -0,0 +1,54 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { BorderStyle } from "file/border";
import { BorderElement } from "./border";
describe("BorderElement", () => {
describe("#constructor", () => {
it("should create a simple border element", () => {
const border = new BorderElement("w:top", {
style: BorderStyle.SINGLE,
});
const tree = new Formatter().format(border);
expect(tree).to.deep.equal({
"w:top": {
_attr: {
"w:val": "single",
},
},
});
});
it("should create a simple border element with a size", () => {
const border = new BorderElement("w:top", {
style: BorderStyle.SINGLE,
size: 22,
});
const tree = new Formatter().format(border);
expect(tree).to.deep.equal({
"w:top": {
_attr: {
"w:val": "single",
"w:sz": 22,
},
},
});
});
it("should create a simple border element with space", () => {
const border = new BorderElement("w:top", {
style: BorderStyle.SINGLE,
space: 22,
});
const tree = new Formatter().format(border);
expect(tree).to.deep.equal({
"w:top": {
_attr: {
"w:val": "single",
"w:space": 22,
},
},
});
});
});
});

View File

@ -7,6 +7,7 @@ import { ISectionOptions } from "../file";
import { INumberingOptions } from "../numbering";
import { Paragraph } from "../paragraph";
import { IStylesOptions } from "../styles";
import { dateTimeValue } from "../values";
export interface IPropertiesOptions {
readonly sections: ISectionOptions[];
@ -103,6 +104,6 @@ class TimestampElement extends XmlComponent {
type: "dcterms:W3CDTF",
}),
);
this.root.push(new Date().toISOString());
this.root.push(dateTimeValue(new Date()));
}
}

View File

@ -1,6 +1,6 @@
import { IContext, IXmlableObject, XmlComponent } from "file/xml-components";
import { Paragraph, ParagraphProperties, TableOfContents } from "../..";
import { Paragraph, ParagraphProperties } from "../..";
import { ISectionPropertiesOptions, SectionProperties } from "./section-properties/section-properties";
export class Body extends XmlComponent {
@ -38,10 +38,6 @@ export class Body extends XmlComponent {
this.root.push(component);
}
public getTablesOfContents(): TableOfContents[] {
return this.root.filter((child) => child instanceof TableOfContents) as TableOfContents[];
}
private createSectionParagraph(section: SectionProperties): Paragraph {
const paragraph = new Paragraph({});
const properties = new ParagraphProperties({});

View File

@ -69,8 +69,4 @@ export class Document extends XmlComponent {
public get Body(): Body {
return this.body;
}
public getTablesOfContents(): TableOfContents[] {
return this.body.getTablesOfContents();
}
}

View File

@ -78,7 +78,7 @@ describe("ParagraphStyle", () => {
const style = new StyleForParagraph({
id: "myStyleId",
paragraph: {
indent: { left: 720 },
indent: { left: 720, right: 500 },
},
});
const tree = new Formatter().format(style);
@ -86,7 +86,7 @@ describe("ParagraphStyle", () => {
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [{ "w:ind": { _attr: { "w:left": 720 } } }],
"w:pPr": [{ "w:ind": { _attr: { "w:left": 720, "w:right": 500 } } }],
},
],
});

View File

@ -1,8 +1,14 @@
import { expect } from "chai";
import {
dateTimeValue,
hexColorValue,
hpsMeasureValue,
longHexNumber,
measurementOrPercentValue,
percentageValue,
positiveUniversalMeasureValue,
shortHexNumber,
signedHpsMeasureValue,
signedTwipsMeasureValue,
twipsMeasureValue,
universalMeasureValue,
@ -48,6 +54,29 @@ describe("values", () => {
});
});
describe("longHexNumber", () => {
it("should allow valid values", () => {
expect(longHexNumber("112233FF")).to.eq("112233FF");
});
it("should throw on invalid values", () => {
expect(() => longHexNumber("112233GG")).to.throw();
expect(() => longHexNumber("112233F")).to.throw();
expect(() => longHexNumber("112233FFF")).to.throw();
});
});
describe("shortHexNumber", () => {
it("should allow valid values", () => {
expect(shortHexNumber("1122")).to.eq("1122");
expect(shortHexNumber("FFFF")).to.eq("FFFF");
});
it("should throw on invalid values", () => {
expect(() => shortHexNumber("11")).to.throw();
expect(() => shortHexNumber("112233")).to.throw();
expect(() => shortHexNumber("FFFG")).to.throw();
});
});
describe("hexColorValue", () => {
it("should allow valid values", () => {
expect(hexColorValue("auto")).to.eq("auto");
@ -114,4 +143,51 @@ describe("values", () => {
expect(() => hpsMeasureValue("-5mm")).to.throw();
});
});
describe("signedHpsMeasureValue", () => {
it("should allow valid values", () => {
expect(signedHpsMeasureValue(1243)).to.eq(1243);
expect(signedHpsMeasureValue(-1243)).to.eq(-1243);
expect(signedHpsMeasureValue("5mm")).to.eq("5mm");
expect(signedHpsMeasureValue("-5mm")).to.eq("-5mm");
});
it("should throw on invalid values", () => {
expect(() => hpsMeasureValue(NaN)).to.throw();
expect(() => hpsMeasureValue("5FF")).to.throw();
});
});
describe("percentageValue", () => {
it("should allow valid values", () => {
expect(percentageValue("0%")).to.eq("0%");
expect(percentageValue("-20%")).to.eq("-20%");
expect(percentageValue("100%")).to.eq("100%");
expect(percentageValue("1000%")).to.eq("1000%");
});
it("should throw on invalid values", () => {
expect(() => percentageValue("0%%")).to.throw();
expect(() => percentageValue("20")).to.throw();
expect(() => percentageValue("FF%")).to.throw();
});
});
describe("measurementOrPercentValue", () => {
it("should allow valid values", () => {
expect(measurementOrPercentValue(1243)).to.eq(1243);
expect(measurementOrPercentValue(-1243)).to.eq(-1243);
expect(measurementOrPercentValue("10%")).to.eq("10%");
expect(measurementOrPercentValue("5mm")).to.eq("5mm");
});
it("should throw on invalid values", () => {
expect(() => measurementOrPercentValue(NaN)).to.throw();
expect(() => measurementOrPercentValue("10%%")).to.throw();
expect(() => measurementOrPercentValue("10F")).to.throw();
});
});
describe("dateTimeValue", () => {
it("should allow valid values", () => {
expect(dateTimeValue(new Date())).to.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:.\d+)?Z/);
});
});
});

View File

@ -1,23 +1,62 @@
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { XmlComponent } from "./";
import { Attributes, BaseXmlComponent, XmlComponent } from "./";
class TestComponent extends XmlComponent {}
class TestComponent extends XmlComponent {
public push(el: BaseXmlComponent): void {
this.root.push(el);
}
}
describe("XmlComponent", () => {
let xmlComponent: TestComponent;
beforeEach(() => {
xmlComponent = new TestComponent("w:test");
});
describe("#constructor()", () => {
it("should create an Xml Component which has the correct rootKey", () => {
const xmlComponent = new TestComponent("w:test");
const tree = new Formatter().format(xmlComponent);
expect(tree).to.deep.equal({
"w:test": {},
});
});
it("should handle children elements", () => {
const xmlComponent = new TestComponent("w:test");
xmlComponent.push(
new Attributes({
val: "test",
}),
);
xmlComponent.push(new TestComponent("innerTest"));
const tree = new Formatter().format(xmlComponent);
expect(tree).to.deep.equal({
"w:test": [
{
_attr: {
"w:val": "test",
},
},
{
innerTest: {},
},
],
});
});
it("should hoist attrs if only attrs are present", () => {
const xmlComponent = new TestComponent("w:test");
xmlComponent.push(
new Attributes({
val: "test",
}),
);
const tree = new Formatter().format(xmlComponent);
expect(tree).to.deep.equal({
"w:test": {
_attr: {
"w:val": "test",
},
},
});
});
});
});