Files
docx-js/src/file/document/document.spec.ts

83 lines
3.0 KiB
TypeScript
Raw Normal View History

2017-03-10 14:11:28 +01:00
import { assert, expect } from "chai";
import * as docx from "../../";
import { Formatter } from "../../export/formatter";
2016-03-29 04:10:33 +01:00
2016-04-03 05:24:56 +01:00
describe("Document", () => {
2016-05-26 15:08:34 +01:00
let document: docx.Document;
2016-03-29 04:10:33 +01:00
beforeEach(() => {
document = new docx.Document();
});
2016-05-26 15:08:34 +01:00
describe("#constructor()", () => {
2016-03-29 04:10:33 +01:00
it("should create valid JSON", () => {
2017-03-09 22:31:55 +00:00
const stringifiedJson = JSON.stringify(document);
2016-05-26 15:08:34 +01:00
let newJson;
2016-03-29 04:10:33 +01:00
try {
newJson = JSON.parse(stringifiedJson);
} catch (e) {
assert.isTrue(false);
}
assert.isTrue(true);
});
});
2017-03-10 14:11:28 +01:00
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"];
2018-01-23 01:33:12 +00:00
expect(body)
.to.be.an("array")
.which.has.length.at.least(1);
2018-01-28 20:23:30 +00:00
expect(body[1]).to.have.property("w:p");
2017-03-10 14:11:28 +01:00
});
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"];
2018-01-23 01:33:12 +00:00
expect(body)
.to.be.an("array")
.which.has.length.at.least(1);
2018-01-28 20:23:30 +00:00
expect(body[1])
2018-01-23 01:33:12 +00:00
.to.have.property("w:p")
.which.includes({
"w:r": [{ "w:rPr": [] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, "sample paragraph text"] }],
});
2017-03-10 14:11:28 +01:00
});
});
2017-03-11 09:02:36 +01:00
describe("#createTable", () => {
it("should create a new table and append it to body", () => {
const table = document.createTable(2, 3);
expect(table).to.be.an.instanceof(docx.Table);
const body = new Formatter().format(document)["w:document"][1]["w:body"];
2018-01-23 01:33:12 +00:00
expect(body)
.to.be.an("array")
.which.has.length.at.least(1);
2018-01-28 20:23:30 +00:00
expect(body[1]).to.have.property("w:tbl");
2017-03-11 09:02:36 +01:00
});
it("should create a table with the correct dimensions", () => {
2017-03-11 10:30:25 +01:00
document.createTable(2, 3);
2017-03-11 09:02:36 +01:00
const body = new Formatter().format(document)["w:document"][1]["w:body"];
2018-01-23 01:33:12 +00:00
expect(body)
.to.be.an("array")
.which.has.length.at.least(1);
2018-01-28 20:23:30 +00:00
expect(body[1])
2018-01-23 01:33:12 +00:00
.to.have.property("w:tbl")
.which.includes({
"w:tblGrid": [
{ "w:gridCol": [{ _attr: { "w:w": 1 } }] },
{ "w:gridCol": [{ _attr: { "w:w": 1 } }] },
{ "w:gridCol": [{ _attr: { "w:w": 1 } }] },
],
});
2018-01-28 20:23:30 +00:00
expect(body[1]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2);
2017-03-11 09:02:36 +01:00
});
});
2017-03-09 22:31:55 +00:00
});