2017-03-09 12:25:59 +01:00
|
|
|
import { assert, expect } from "chai";
|
|
|
|
|
2016-07-01 22:09:55 +01:00
|
|
|
import * as docx from "../../../docx";
|
2017-03-08 18:23:00 +01:00
|
|
|
import { Formatter } from "../../../export/formatter";
|
|
|
|
import { Numbering } from "../../../numbering";
|
2016-03-29 04:10:33 +01:00
|
|
|
|
2017-03-09 12:25:59 +01:00
|
|
|
function jsonify(obj: object) {
|
|
|
|
const stringifiedJson = JSON.stringify(obj);
|
2016-03-30 03:58:53 +01:00
|
|
|
return JSON.parse(stringifiedJson);
|
|
|
|
}
|
|
|
|
|
2016-04-03 05:24:56 +01:00
|
|
|
describe("Paragraph", () => {
|
2016-05-26 15:08:34 +01:00
|
|
|
let paragraph: docx.Paragraph;
|
2016-03-29 04:10:33 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
paragraph = new docx.Paragraph();
|
|
|
|
});
|
|
|
|
|
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 12:25:59 +01:00
|
|
|
const stringifiedJson = JSON.stringify(paragraph);
|
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);
|
|
|
|
});
|
2016-05-09 13:12:28 +01:00
|
|
|
|
|
|
|
it("should create have valid properties", () => {
|
2017-03-09 12:25:59 +01:00
|
|
|
const stringifiedJson = JSON.stringify(paragraph);
|
|
|
|
const newJson = JSON.parse(stringifiedJson);
|
2016-05-09 21:50:46 +01:00
|
|
|
assert.equal(newJson.root[0].rootKey, "w:pPr");
|
2016-05-09 13:12:28 +01:00
|
|
|
});
|
2016-03-29 04:10:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#heading1()", () => {
|
|
|
|
it("should add heading style to JSON", () => {
|
|
|
|
paragraph.heading1();
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2016-05-01 22:24:20 +01:00
|
|
|
assert.equal(newJson.root[0].root[1].root[0].root.val, "Heading1");
|
2016-03-29 04:10:33 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#heading2()", () => {
|
|
|
|
it("should add heading style to JSON", () => {
|
|
|
|
paragraph.heading2();
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2016-03-29 04:10:33 +01:00
|
|
|
|
2016-05-01 22:24:20 +01:00
|
|
|
assert.equal(newJson.root[0].root[1].root[0].root.val, "Heading2");
|
2016-03-29 04:10:33 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#heading3()", () => {
|
|
|
|
it("should add heading style to JSON", () => {
|
|
|
|
paragraph.heading3();
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2016-03-29 04:10:33 +01:00
|
|
|
|
2016-05-01 22:24:20 +01:00
|
|
|
assert.equal(newJson.root[0].root[1].root[0].root.val, "Heading3");
|
2016-03-29 04:10:33 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#title()", () => {
|
|
|
|
it("should add title style to JSON", () => {
|
|
|
|
paragraph.title();
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2016-03-29 04:10:33 +01:00
|
|
|
|
2016-05-01 22:24:20 +01:00
|
|
|
assert.equal(newJson.root[0].root[1].root[0].root.val, "Title");
|
2016-03-29 04:10:33 +01:00
|
|
|
});
|
|
|
|
});
|
2016-03-29 04:50:23 +01:00
|
|
|
|
|
|
|
describe("#center()", () => {
|
|
|
|
it("should add center alignment to JSON", () => {
|
|
|
|
paragraph.center();
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2016-03-29 04:50:23 +01:00
|
|
|
|
2016-05-01 22:24:20 +01:00
|
|
|
assert.equal(newJson.root[0].root[1].root[0].root.val, "center");
|
2016-03-29 04:50:23 +01:00
|
|
|
});
|
|
|
|
});
|
2016-03-29 05:01:33 +01:00
|
|
|
|
2016-03-29 23:36:57 +01:00
|
|
|
describe("#thematicBreak()", () => {
|
2016-03-29 05:01:33 +01:00
|
|
|
it("should add thematic break to JSON", () => {
|
2016-03-29 23:36:57 +01:00
|
|
|
paragraph.thematicBreak();
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2016-05-01 22:24:20 +01:00
|
|
|
assert.equal(newJson.root[0].root[1].rootKey, "w:pBdr");
|
2016-03-29 05:01:33 +01:00
|
|
|
});
|
|
|
|
});
|
2016-03-30 00:28:05 +01:00
|
|
|
|
|
|
|
describe("#pageBreak()", () => {
|
|
|
|
it("should add page break to JSON", () => {
|
|
|
|
paragraph.pageBreak();
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2016-05-01 22:24:20 +01:00
|
|
|
assert.equal(newJson.root[0].root[1].root[1].rootKey, "w:br");
|
2016-03-30 00:28:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should add page break with 'page' type", () => {
|
|
|
|
paragraph.pageBreak();
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2016-05-01 22:24:20 +01:00
|
|
|
assert.equal(newJson.root[0].root[1].root[1].root[0].root.type, "page");
|
2016-03-30 00:28:05 +01:00
|
|
|
});
|
|
|
|
});
|
2016-03-30 02:55:11 +01:00
|
|
|
|
|
|
|
describe("#bullet()", () => {
|
|
|
|
it("should add list paragraph style to JSON", () => {
|
|
|
|
paragraph.bullet();
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2016-05-01 22:24:20 +01:00
|
|
|
assert.equal(newJson.root[0].root[1].root[0].root.val, "ListParagraph");
|
2016-03-30 02:55:11 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("it should add numbered properties", () => {
|
|
|
|
paragraph.bullet();
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2016-05-01 22:24:20 +01:00
|
|
|
assert.isDefined(newJson.root[0].root[2]);
|
2016-03-30 02:55:11 +01:00
|
|
|
});
|
|
|
|
});
|
2017-03-08 18:23:00 +01:00
|
|
|
|
|
|
|
describe("#setNumbering", () => {
|
|
|
|
it("should add list paragraph style to JSON", () => {
|
|
|
|
const numbering = new Numbering();
|
|
|
|
const numberedAbstract = numbering.createAbstractNumbering();
|
|
|
|
numberedAbstract.createLevel(0, "lowerLetter", "%1)", "start");
|
|
|
|
const letterNumbering = numbering.createConcreteNumbering(numberedAbstract);
|
|
|
|
|
|
|
|
paragraph.setNumbering(letterNumbering, 0);
|
2017-03-09 12:25:59 +01:00
|
|
|
const newJson = jsonify(paragraph);
|
2017-03-08 18:23:00 +01:00
|
|
|
assert.equal(newJson.root[0].root[1].root[0].root.val, "ListParagraph");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("it should add numbered properties", () => {
|
|
|
|
const numbering = new Numbering();
|
|
|
|
const numberedAbstract = numbering.createAbstractNumbering();
|
|
|
|
numberedAbstract.createLevel(0, "lowerLetter", "%1)", "start");
|
|
|
|
const letterNumbering = numbering.createConcreteNumbering(numberedAbstract);
|
|
|
|
|
|
|
|
paragraph.setNumbering(letterNumbering, 0);
|
|
|
|
const tree = new Formatter().format(paragraph);
|
|
|
|
expect(tree).to.deep.equal({
|
|
|
|
"w:p": [
|
|
|
|
{
|
|
|
|
"w:pPr": [
|
2017-03-09 12:25:59 +01:00
|
|
|
{_attr: {}},
|
|
|
|
{"w:pStyle": [{_attr: {"w:val": "ListParagraph"}}]},
|
2017-03-08 18:23:00 +01:00
|
|
|
{
|
|
|
|
"w:numPr": [
|
2017-03-09 12:25:59 +01:00
|
|
|
{"w:ilvl": [{_attr: {"w:val": 0}}]},
|
|
|
|
{"w:numId": [{_attr: {"w:val": letterNumbering.id}}]},
|
|
|
|
],
|
2017-03-08 18:23:00 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2017-03-09 12:25:59 +01:00
|
|
|
],
|
|
|
|
});
|
2017-03-08 18:23:00 +01:00
|
|
|
});
|
|
|
|
});
|
2017-03-09 09:18:17 +01:00
|
|
|
|
2017-03-09 09:46:12 +01:00
|
|
|
describe("#style", () => {
|
|
|
|
it("should set the paragraph style to the given styleId", () => {
|
2017-03-09 12:25:59 +01:00
|
|
|
paragraph.style("myFancyStyle");
|
2017-03-09 09:46:12 +01:00
|
|
|
const tree = new Formatter().format(paragraph);
|
|
|
|
expect(tree).to.deep.equal({
|
|
|
|
"w:p": [
|
|
|
|
{
|
|
|
|
"w:pPr": [
|
2017-03-09 12:25:59 +01:00
|
|
|
{_attr: {}},
|
|
|
|
{"w:pStyle": [{_attr: {"w:val": "myFancyStyle"}}]},
|
2017-03-09 09:46:12 +01:00
|
|
|
],
|
|
|
|
},
|
2017-03-09 12:25:59 +01:00
|
|
|
],
|
|
|
|
});
|
2017-03-09 09:46:12 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-09 09:18:17 +01:00
|
|
|
describe("#indent", () => {
|
|
|
|
it("should set the paragraph indent to the given values", () => {
|
|
|
|
paragraph.indent(720);
|
|
|
|
const tree = new Formatter().format(paragraph);
|
|
|
|
expect(tree).to.deep.equal({
|
|
|
|
"w:p": [
|
|
|
|
{
|
|
|
|
"w:pPr": [
|
2017-03-09 12:25:59 +01:00
|
|
|
{_attr: {}},
|
|
|
|
{"w:ind": [{_attr: {"w:left": 720}}]},
|
2017-03-09 09:18:17 +01:00
|
|
|
],
|
|
|
|
},
|
2017-03-09 12:25:59 +01:00
|
|
|
],
|
|
|
|
});
|
2017-03-09 09:18:17 +01:00
|
|
|
});
|
|
|
|
});
|
2017-03-09 12:12:33 +01:00
|
|
|
|
|
|
|
describe("#spacing", () => {
|
|
|
|
it("should set the paragraph spacing to the given values", () => {
|
|
|
|
paragraph.spacing({before: 90, line: 50});
|
|
|
|
const tree = new Formatter().format(paragraph);
|
|
|
|
expect(tree).to.deep.equal({
|
|
|
|
"w:p": [
|
|
|
|
{
|
|
|
|
"w:pPr": [
|
2017-03-09 12:25:59 +01:00
|
|
|
{_attr: {}},
|
|
|
|
{"w:spacing": [{_attr: {"w:before": 90, "w:line": 50}}]},
|
2017-03-09 12:12:33 +01:00
|
|
|
],
|
|
|
|
},
|
2017-03-09 12:25:59 +01:00
|
|
|
],
|
|
|
|
});
|
2017-03-09 12:12:33 +01:00
|
|
|
});
|
|
|
|
});
|
2017-03-08 18:23:00 +01:00
|
|
|
});
|