improve test coverage
This commit is contained in:
54
src/file/border/border.spec.ts
Normal file
54
src/file/border/border.spec.ts
Normal 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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -7,6 +7,7 @@ import { ISectionOptions } from "../file";
|
|||||||
import { INumberingOptions } from "../numbering";
|
import { INumberingOptions } from "../numbering";
|
||||||
import { Paragraph } from "../paragraph";
|
import { Paragraph } from "../paragraph";
|
||||||
import { IStylesOptions } from "../styles";
|
import { IStylesOptions } from "../styles";
|
||||||
|
import { dateTimeValue } from "../values";
|
||||||
|
|
||||||
export interface IPropertiesOptions {
|
export interface IPropertiesOptions {
|
||||||
readonly sections: ISectionOptions[];
|
readonly sections: ISectionOptions[];
|
||||||
@ -103,6 +104,6 @@ class TimestampElement extends XmlComponent {
|
|||||||
type: "dcterms:W3CDTF",
|
type: "dcterms:W3CDTF",
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
this.root.push(new Date().toISOString());
|
this.root.push(dateTimeValue(new Date()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { IContext, IXmlableObject, XmlComponent } from "file/xml-components";
|
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";
|
import { ISectionPropertiesOptions, SectionProperties } from "./section-properties/section-properties";
|
||||||
|
|
||||||
export class Body extends XmlComponent {
|
export class Body extends XmlComponent {
|
||||||
@ -38,10 +38,6 @@ export class Body extends XmlComponent {
|
|||||||
this.root.push(component);
|
this.root.push(component);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getTablesOfContents(): TableOfContents[] {
|
|
||||||
return this.root.filter((child) => child instanceof TableOfContents) as TableOfContents[];
|
|
||||||
}
|
|
||||||
|
|
||||||
private createSectionParagraph(section: SectionProperties): Paragraph {
|
private createSectionParagraph(section: SectionProperties): Paragraph {
|
||||||
const paragraph = new Paragraph({});
|
const paragraph = new Paragraph({});
|
||||||
const properties = new ParagraphProperties({});
|
const properties = new ParagraphProperties({});
|
||||||
|
@ -69,8 +69,4 @@ export class Document extends XmlComponent {
|
|||||||
public get Body(): Body {
|
public get Body(): Body {
|
||||||
return this.body;
|
return this.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getTablesOfContents(): TableOfContents[] {
|
|
||||||
return this.body.getTablesOfContents();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ describe("ParagraphStyle", () => {
|
|||||||
const style = new StyleForParagraph({
|
const style = new StyleForParagraph({
|
||||||
id: "myStyleId",
|
id: "myStyleId",
|
||||||
paragraph: {
|
paragraph: {
|
||||||
indent: { left: 720 },
|
indent: { left: 720, right: 500 },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const tree = new Formatter().format(style);
|
const tree = new Formatter().format(style);
|
||||||
@ -86,7 +86,7 @@ describe("ParagraphStyle", () => {
|
|||||||
"w:style": [
|
"w:style": [
|
||||||
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
|
{ _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 } } }],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
@ -1,8 +1,14 @@
|
|||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import {
|
import {
|
||||||
|
dateTimeValue,
|
||||||
hexColorValue,
|
hexColorValue,
|
||||||
hpsMeasureValue,
|
hpsMeasureValue,
|
||||||
|
longHexNumber,
|
||||||
|
measurementOrPercentValue,
|
||||||
|
percentageValue,
|
||||||
positiveUniversalMeasureValue,
|
positiveUniversalMeasureValue,
|
||||||
|
shortHexNumber,
|
||||||
|
signedHpsMeasureValue,
|
||||||
signedTwipsMeasureValue,
|
signedTwipsMeasureValue,
|
||||||
twipsMeasureValue,
|
twipsMeasureValue,
|
||||||
universalMeasureValue,
|
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", () => {
|
describe("hexColorValue", () => {
|
||||||
it("should allow valid values", () => {
|
it("should allow valid values", () => {
|
||||||
expect(hexColorValue("auto")).to.eq("auto");
|
expect(hexColorValue("auto")).to.eq("auto");
|
||||||
@ -114,4 +143,51 @@ describe("values", () => {
|
|||||||
expect(() => hpsMeasureValue("-5mm")).to.throw();
|
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/);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,23 +1,62 @@
|
|||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
|
|
||||||
import { Formatter } from "export/formatter";
|
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", () => {
|
describe("XmlComponent", () => {
|
||||||
let xmlComponent: TestComponent;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
xmlComponent = new TestComponent("w:test");
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("#constructor()", () => {
|
describe("#constructor()", () => {
|
||||||
it("should create an Xml Component which has the correct rootKey", () => {
|
it("should create an Xml Component which has the correct rootKey", () => {
|
||||||
|
const xmlComponent = new TestComponent("w:test");
|
||||||
const tree = new Formatter().format(xmlComponent);
|
const tree = new Formatter().format(xmlComponent);
|
||||||
expect(tree).to.deep.equal({
|
expect(tree).to.deep.equal({
|
||||||
"w:test": {},
|
"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",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user