Remove usage of Utility

This commit is contained in:
Dolan
2019-06-27 01:35:58 +01:00
parent 120d97bc64
commit 1f51fd7a31
5 changed files with 115 additions and 99 deletions

View File

@ -1,10 +1,9 @@
import { assert } from "chai";
import { assert, expect } from "chai";
import { Formatter } from "../export/formatter";
import * as file from "../file";
import { CoreProperties } from "../file/core-properties";
import { Attributes } from "../file/xml-components";
import { Utility } from "../tests/utility";
import { Formatter } from "export/formatter";
import * as file from "file";
import { CoreProperties } from "file/core-properties";
import { Attributes } from "file/xml-components";
describe("Formatter", () => {
let formatter: Formatter;
@ -38,26 +37,24 @@ describe("Formatter", () => {
const attributes = new Attributes({
rsidSect: "test2",
});
let newJson = formatter.format(attributes);
newJson = Utility.jsonify(newJson);
if (newJson._attr === undefined) {
assert.fail();
return;
}
assert.isDefined(newJson._attr["w:rsidSect"]);
const tree = formatter.format(attributes);
expect(tree).to.deep.equal({
_attr: {
"w:rsidSect": "test2",
},
});
});
it("should format attributes (val)", () => {
const attributes = new Attributes({
val: "test",
});
let newJson = formatter.format(attributes);
newJson = Utility.jsonify(newJson);
if (newJson._attr === undefined) {
assert.fail();
return;
}
assert.isDefined(newJson._attr["w:val"]);
const tree = formatter.format(attributes);
expect(tree).to.deep.equal({
_attr: {
"w:val": "test",
},
});
});
it("should should change 'p' tag into 'w:p' tag", () => {

View File

@ -1,6 +1,6 @@
import { assert } from "chai";
import { expect } from "chai";
import { Utility } from "tests/utility";
import { Formatter } from "export/formatter";
import { HorizontalPositionAlign, HorizontalPositionRelativeFrom } from "./floating-position";
import { HorizontalPosition } from "./horizontal-position";
@ -8,35 +8,45 @@ import { HorizontalPosition } from "./horizontal-position";
describe("HorizontalPosition", () => {
describe("#constructor()", () => {
it("should create a element with position align", () => {
const newJson = Utility.jsonify(
const tree = new Formatter().format(
new HorizontalPosition({
relative: HorizontalPositionRelativeFrom.MARGIN,
align: HorizontalPositionAlign.CENTER,
}),
);
assert.equal(newJson.rootKey, "wp:positionH");
assert.include(newJson.root[0].root, {
relativeFrom: "margin",
expect(tree).to.deep.equal({
"wp:positionH": [
{
_attr: {
relativeFrom: "margin",
},
},
{
"wp:align": ["center"],
},
],
});
assert.equal(newJson.root[1].rootKey, "wp:align");
assert.include(newJson.root[1].root, "center");
});
it("should create a element with offset", () => {
const newJson = Utility.jsonify(
const tree = new Formatter().format(
new HorizontalPosition({
relative: HorizontalPositionRelativeFrom.MARGIN,
offset: 40,
}),
);
assert.equal(newJson.rootKey, "wp:positionH");
assert.include(newJson.root[0].root, {
relativeFrom: "margin",
expect(tree).to.deep.equal({
"wp:positionH": [
{
_attr: {
relativeFrom: "margin",
},
},
{
"wp:posOffset": ["40"],
},
],
});
assert.equal(newJson.root[1].rootKey, "wp:posOffset");
assert.include(newJson.root[1].root[0], 40);
});
});
});

View File

@ -1,6 +1,6 @@
import { assert } from "chai";
import { expect } from "chai";
import { Utility } from "tests/utility";
import { Formatter } from "export/formatter";
import { PageBreak, PageBreakBefore } from "./page-break";
@ -13,21 +13,18 @@ describe("PageBreak", () => {
describe("#constructor()", () => {
it("should create a Page Break with correct attributes", () => {
const newJson = Utility.jsonify(pageBreak);
const attributes = {
type: "page",
};
assert.equal(JSON.stringify(newJson.root[1].root[0].root), JSON.stringify(attributes));
});
it("should create a Page Break with w:r", () => {
const newJson = Utility.jsonify(pageBreak);
assert.equal(newJson.rootKey, "w:r");
});
it("should create a Page Break with a Break inside", () => {
const newJson = Utility.jsonify(pageBreak);
assert.equal(newJson.root[1].rootKey, "w:br");
const tree = new Formatter().format(pageBreak);
expect(tree).to.deep.equal({
"w:r": [
{
"w:br": {
_attr: {
"w:type": "page",
},
},
},
],
});
});
});
});
@ -35,7 +32,9 @@ describe("PageBreak", () => {
describe("PageBreakBefore", () => {
it("should create page break before", () => {
const pageBreakBefore = new PageBreakBefore();
const newJson = Utility.jsonify(pageBreakBefore);
assert.equal(newJson.rootKey, "w:pageBreakBefore");
const tree = new Formatter().format(pageBreakBefore);
expect(tree).to.deep.equal({
"w:pageBreakBefore": {},
});
});
});

View File

@ -1,6 +1,6 @@
import { assert } from "chai";
import { expect } from "chai";
import { Utility } from "tests/utility";
import { Formatter } from "export/formatter";
import { NumberProperties } from "./unordered-list";
@ -13,20 +13,25 @@ describe("NumberProperties", () => {
describe("#constructor()", () => {
it("should create a Number Properties with correct root key", () => {
const newJson = Utility.jsonify(numberProperties);
assert.equal(newJson.rootKey, "w:numPr");
});
it("should create a Page Break with a Indent Level inside", () => {
const newJson = Utility.jsonify(numberProperties);
assert.equal(newJson.root[0].rootKey, "w:ilvl");
assert.equal(newJson.root[0].root[0].root.val, 10);
});
it("should create a Page Break with a Number Id inside", () => {
const newJson = Utility.jsonify(numberProperties);
assert.equal(newJson.root[1].rootKey, "w:numId");
assert.equal(newJson.root[1].root[0].root.val, 5);
const tree = new Formatter().format(numberProperties);
expect(tree).to.deep.equal({
"w:numPr": [
{
"w:ilvl": {
_attr: {
"w:val": 10,
},
},
},
{
"w:numId": {
_attr: {
"w:val": 5,
},
},
},
],
});
});
});
});

View File

@ -1,7 +1,6 @@
import { assert, expect } from "chai";
import { expect } from "chai";
import { Formatter } from "export/formatter";
import { Utility } from "tests/utility";
import { Hyperlink } from "./";
@ -14,28 +13,23 @@ describe("Hyperlink", () => {
describe("#constructor()", () => {
it("should create a hyperlink with correct root key", () => {
const newJson = Utility.jsonify(hyperlink);
assert.equal(newJson.rootKey, "w:hyperlink");
});
it("should create a hyperlink with right attributes", () => {
const newJson = Utility.jsonify(hyperlink);
const attributes = {
history: 1,
id: "rId1",
};
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
});
it("should create a hyperlink with a run component", () => {
const tree = new Formatter().format(hyperlink);
const runJson = {
"w:r": [
{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "Hyperlink" } } }] },
{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "https://example.com"] },
expect(tree).to.deep.equal({
"w:hyperlink": [
{
_attr: {
"w:history": 1,
"r:id": "rId1",
},
},
{
"w:r": [
{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "Hyperlink" } } }] },
{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "https://example.com"] },
],
},
],
};
expect(tree["w:hyperlink"][1]).to.deep.equal(runJson);
});
});
describe("with optional anchor parameter", () => {
@ -44,12 +38,23 @@ describe("Hyperlink", () => {
});
it("should create an internal link with anchor tag", () => {
const newJson = Utility.jsonify(hyperlink);
const attributes = {
history: 1,
anchor: "anchor",
};
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
const tree = new Formatter().format(hyperlink);
expect(tree).to.deep.equal({
"w:hyperlink": [
{
_attr: {
"w:history": 1,
"w:anchor": "anchor",
},
},
{
"w:r": [
{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "Hyperlink" } } }] },
{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "Anchor Text"] },
],
},
],
});
});
});
});