Files
docx-js/ts/tests/docx/document/documentTest.ts
felipe b1c8b2beb8 add xml:space="preserve" to text runs
In MS Word 2015 (and possibly others), leading and trailing spaces are
ignored in text runs. This means that calling TextRun with
leading/trailing space would result in a document that didn't include
those spaces. The fix here (per http://officeopenxml.com/WPtext.php)
is to include an extra attribute on the "w:t" element that forces word
to recognize those spaces.
2017-04-14 11:08:19 +02:00

74 lines
2.8 KiB
TypeScript

import { assert, expect } from "chai";
import * as docx from "../../../docx";
import { Formatter } from "../../../export/formatter";
describe("Document", () => {
let document: docx.Document;
beforeEach(() => {
document = new docx.Document();
});
describe("#constructor()", () => {
it("should create valid JSON", () => {
const stringifiedJson = JSON.stringify(document);
let newJson;
try {
newJson = JSON.parse(stringifiedJson);
} catch (e) {
assert.isTrue(false);
}
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": [{_attr: {"xml:space": "preserve"}}, "sample paragraph text"]},
],
});
});
});
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"];
expect(body).to.be.an("array").which.has.length.at.least(1);
expect(body[0]).to.have.property("w:tbl");
});
it("should create a table with the correct dimensions", () => {
document.createTable(2, 3);
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:tbl").which.includes({
"w:tblGrid": [
{"w:gridCol": [{_attr: {"w:w": 1}}]},
{"w:gridCol": [{_attr: {"w:w": 1}}]},
{"w:gridCol": [{_attr: {"w:w": 1}}]},
],
});
expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2);
});
});
});