2017-03-10 14:11:28 +01:00
|
|
|
import { assert, expect } from "chai";
|
2017-09-17 00:00:41 +01:00
|
|
|
|
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);
|
|
|
|
});
|
2018-06-21 12:03:34 +02:00
|
|
|
|
|
|
|
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);
|
2018-06-21 12:03:34 +02:00
|
|
|
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);
|
2018-06-21 12:03:34 +02:00
|
|
|
expect(body[0])
|
2018-01-23 01:33:12 +00:00
|
|
|
.to.have.property("w:p")
|
|
|
|
.which.includes({
|
2019-04-09 05:27:18 -04:00
|
|
|
"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);
|
2018-06-21 12:03:34 +02:00
|
|
|
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);
|
2018-06-21 12:03:34 +02:00
|
|
|
expect(body[0])
|
2018-01-23 01:33:12 +00:00
|
|
|
.to.have.property("w:tbl")
|
|
|
|
.which.includes({
|
|
|
|
"w:tblGrid": [
|
2019-04-09 05:27:18 -04:00
|
|
|
{ "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
|
|
|
],
|
|
|
|
});
|
2018-06-21 12:03:34 +02: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
|
|
|
});
|