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

96 lines
3.3 KiB
TypeScript
Raw Normal View History

2017-03-10 14:11:28 +01:00
import { assert, expect } from "chai";
2018-10-26 01:04:07 +01:00
import { Formatter } from "export/formatter";
2018-01-31 01:23:13 +00:00
import { Paragraph } from "../paragraph";
import { Table } from "../table";
import { Document } from "./document";
2016-03-29 04:10:33 +01:00
2016-04-03 05:24:56 +01:00
describe("Document", () => {
2018-01-31 01:23:13 +00:00
let document: Document;
2016-03-29 04:10:33 +01:00
beforeEach(() => {
2018-01-31 01:23:13 +00:00
document = new Document();
2016-03-29 04:10:33 +01:00
});
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-03-29 04:10:33 +01:00
try {
2018-01-31 23:24:55 +00:00
JSON.parse(stringifiedJson);
2016-03-29 04:10:33 +01:00
} catch (e) {
assert.isTrue(false);
}
assert.isTrue(true);
});
it("should create default section", () => {
const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body[0]).to.have.property("w:sectPr");
});
2016-03-29 04:10:33 +01:00
});
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();
2018-01-31 01:23:13 +00:00
expect(para).to.be.an.instanceof(Paragraph);
2017-03-10 14:11:28 +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);
expect(body[0]).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");
2018-01-31 01:23:13 +00:00
expect(para).to.be.an.instanceof(Paragraph);
2017-03-10 14:11:28 +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);
expect(body[0])
2018-01-23 01:33:12 +00:00
.to.have.property("w:p")
.which.includes({
"w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "sample paragraph text"] }],
2018-01-23 01:33:12 +00:00
});
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", () => {
2019-03-13 02:29:11 +00:00
const table = document.createTable({
rows: 2,
columns: 3,
});
2018-01-31 01:23:13 +00:00
expect(table).to.be.an.instanceof(Table);
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);
expect(body[0]).to.have.property("w:tbl");
2017-03-11 09:02:36 +01:00
});
it("should create a table with the correct dimensions", () => {
2019-03-13 02:29:11 +00:00
document.createTable({
rows: 2,
columns: 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);
expect(body[0])
2018-01-23 01:33:12 +00:00
.to.have.property("w:tbl")
.which.includes({
"w:tblGrid": [
{ "w:gridCol": { _attr: { "w:w": 100 } } },
{ "w:gridCol": { _attr: { "w:w": 100 } } },
{ "w:gridCol": { _attr: { "w:w": 100 } } },
2018-01-23 01:33:12 +00:00
],
});
expect(body[0]["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
});