2019-06-27 01:35:58 +01:00
|
|
|
import { assert, expect } from "chai";
|
2019-09-22 19:09:34 +01:00
|
|
|
import * as sinon from "sinon";
|
2017-03-09 21:06:54 +01:00
|
|
|
|
2019-06-27 01:35:58 +01:00
|
|
|
import { Formatter } from "export/formatter";
|
2019-09-22 19:09:34 +01:00
|
|
|
import { Paragraph, TextRun } from "file";
|
2019-06-27 01:35:58 +01:00
|
|
|
import { CoreProperties } from "file/core-properties";
|
|
|
|
import { Attributes } from "file/xml-components";
|
2016-03-28 00:53:24 +01:00
|
|
|
|
2016-04-03 20:00:30 +01:00
|
|
|
describe("Formatter", () => {
|
2016-05-26 15:08:34 +01:00
|
|
|
let formatter: Formatter;
|
2016-03-28 00:53:24 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
formatter = new Formatter();
|
|
|
|
});
|
|
|
|
|
2016-05-26 15:08:34 +01:00
|
|
|
describe("#format()", () => {
|
2016-04-03 05:23:13 +01:00
|
|
|
it("should format simple paragraph", () => {
|
2019-09-22 19:09:34 +01:00
|
|
|
const paragraph = new Paragraph("");
|
2017-03-09 21:06:54 +01:00
|
|
|
const newJson = formatter.format(paragraph);
|
2016-04-03 05:23:13 +01:00
|
|
|
assert.isDefined(newJson["w:p"]);
|
|
|
|
});
|
|
|
|
|
2016-04-05 01:49:12 +01:00
|
|
|
it("should remove xmlKeys", () => {
|
2019-09-22 19:09:34 +01:00
|
|
|
const paragraph = new Paragraph("");
|
2017-03-09 21:06:54 +01:00
|
|
|
const newJson = formatter.format(paragraph);
|
|
|
|
const stringifiedJson = JSON.stringify(newJson);
|
2016-04-05 01:49:12 +01:00
|
|
|
assert(stringifiedJson.indexOf("xmlKeys") < 0);
|
|
|
|
});
|
|
|
|
|
2016-05-01 22:24:20 +01:00
|
|
|
it("should format simple paragraph with bold text", () => {
|
2019-09-30 22:56:21 +01:00
|
|
|
const paragraph = new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun({
|
|
|
|
text: "test",
|
|
|
|
bold: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const tree = formatter.format(paragraph);
|
|
|
|
expect(tree).to.deep.equal({
|
|
|
|
"w:p": [
|
|
|
|
{
|
|
|
|
"w:r": [
|
|
|
|
{
|
|
|
|
"w:rPr": [
|
|
|
|
{
|
|
|
|
"w:b": {
|
|
|
|
_attr: {
|
|
|
|
"w:val": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"w:bCs": {
|
|
|
|
_attr: {
|
|
|
|
"w:val": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"w:t": [
|
|
|
|
{
|
|
|
|
_attr: {
|
|
|
|
"xml:space": "preserve",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"test",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2016-04-03 05:23:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should format attributes (rsidSect)", () => {
|
2017-03-09 21:06:54 +01:00
|
|
|
const attributes = new Attributes({
|
|
|
|
rsidSect: "test2",
|
2016-04-03 05:23:13 +01:00
|
|
|
});
|
2019-06-27 01:35:58 +01:00
|
|
|
const tree = formatter.format(attributes);
|
|
|
|
expect(tree).to.deep.equal({
|
|
|
|
_attr: {
|
|
|
|
"w:rsidSect": "test2",
|
|
|
|
},
|
|
|
|
});
|
2016-04-03 05:23:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should format attributes (val)", () => {
|
2017-03-09 21:06:54 +01:00
|
|
|
const attributes = new Attributes({
|
|
|
|
val: "test",
|
2016-04-03 05:23:13 +01:00
|
|
|
});
|
2019-06-27 01:35:58 +01:00
|
|
|
const tree = formatter.format(attributes);
|
|
|
|
expect(tree).to.deep.equal({
|
|
|
|
_attr: {
|
|
|
|
"w:val": "test",
|
|
|
|
},
|
|
|
|
});
|
2016-03-31 04:33:06 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should should change 'p' tag into 'w:p' tag", () => {
|
2019-09-22 19:09:34 +01:00
|
|
|
const paragraph = new Paragraph("");
|
2017-03-09 21:06:54 +01:00
|
|
|
const newJson = formatter.format(paragraph);
|
2016-03-31 05:30:37 +01:00
|
|
|
assert.isDefined(newJson["w:p"]);
|
2016-03-28 00:53:24 +01:00
|
|
|
});
|
2016-04-04 17:30:29 +01:00
|
|
|
|
2016-04-05 01:49:12 +01:00
|
|
|
it("should format Properties object correctly", () => {
|
2018-02-04 01:43:03 +00:00
|
|
|
const properties = new CoreProperties({
|
2016-04-04 17:30:29 +01:00
|
|
|
title: "test document",
|
2017-03-09 21:06:54 +01:00
|
|
|
creator: "Dolan",
|
2016-04-04 17:30:29 +01:00
|
|
|
});
|
2017-03-09 21:06:54 +01:00
|
|
|
const newJson = formatter.format(properties);
|
2016-04-05 01:49:12 +01:00
|
|
|
assert.isDefined(newJson["cp:coreProperties"]);
|
2016-04-04 17:30:29 +01:00
|
|
|
});
|
2019-09-22 19:09:34 +01:00
|
|
|
|
|
|
|
it("should call the prep method only once", () => {
|
|
|
|
const paragraph = new Paragraph("");
|
|
|
|
const spy = sinon.spy(paragraph, "prepForXml");
|
|
|
|
|
|
|
|
formatter.format(paragraph);
|
|
|
|
expect(spy.calledOnce).to.equal(true);
|
|
|
|
});
|
2016-03-28 00:53:24 +01:00
|
|
|
});
|
2017-03-09 21:06:54 +01:00
|
|
|
});
|