diff --git a/ts/docx/document/index.ts b/ts/docx/document/index.ts index de8f3f0053..555ebf55bf 100644 --- a/ts/docx/document/index.ts +++ b/ts/docx/document/index.ts @@ -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; + } } diff --git a/ts/tests/docx/document/documentTest.ts b/ts/tests/docx/document/documentTest.ts index 43b352a1a7..98b9a9cf97 100644 --- a/ts/tests/docx/document/documentTest.ts +++ b/ts/tests/docx/document/documentTest.ts @@ -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"]}, + ], + }); + }); + }); });