add document.createParagraph method

This commit is contained in:
felipe
2017-03-10 14:11:28 +01:00
parent 8c91c04afa
commit ea647d84df
2 changed files with 31 additions and 1 deletions

View File

@ -33,4 +33,10 @@ export class Document extends XmlComponent {
public addParagraph(paragraph: Paragraph): void {
this.body.push(paragraph);
}
public createParagraph(text?: string): Paragraph {
const para = new Paragraph(text);
this.addParagraph(para);
return para;
}
}

View File

@ -1,5 +1,6 @@
import { assert } from "chai";
import { assert, expect } from "chai";
import * as docx from "../../../docx";
import { Formatter } from "../../../export/formatter";
describe("Document", () => {
let document: docx.Document;
@ -22,4 +23,27 @@ describe("Document", () => {
assert.isTrue(true);
});
});
describe("#createParagraph", () => {
it("should create a new paragraph and append it to body", () => {
const para = document.createParagraph();
expect(para).to.be.an.instanceof(docx.Paragraph);
const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body).to.be.an("array").which.has.length.at.least(1);
expect(body[0]).to.have.property("w:p");
});
it("should use the text given to create a run in the paragraph", () => {
const para = document.createParagraph("sample paragraph text");
expect(para).to.be.an.instanceof(docx.Paragraph);
const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body).to.be.an("array").which.has.length.at.least(1);
expect(body[0]).to.have.property("w:p").which.includes({
"w:r": [
{"w:rPr": []},
{"w:t": ["sample paragraph text"]},
],
});
});
});
});