Added constant EMPTY_OBJECT (an empty sealed object) that is used to indicate to the xml library that an empty XML element should be generated, and use it in the JSON hardcoded into the tests.

This commit is contained in:
Bruce Duncan
2019-04-10 13:47:38 -04:00
parent bb1604cd8f
commit 77edf8862b
16 changed files with 94 additions and 66 deletions

View File

@ -4,6 +4,8 @@ import { Formatter } from "export/formatter";
import { GridCol, TableGrid } from "./grid";
import { EMPTY_OBJECT } from "file/xml-components";
describe("GridCol", () => {
describe("#constructor", () => {
it("sets the width attribute to the value given", () => {
@ -17,7 +19,7 @@ describe("GridCol", () => {
it("does not set a width attribute if not given", () => {
const grid = new GridCol();
const tree = new Formatter().format(grid);
expect(tree).to.deep.equal({ "w:gridCol": {} });
expect(tree).to.deep.equal({ "w:gridCol": EMPTY_OBJECT });
});
});
});

View File

@ -5,6 +5,8 @@ import { Formatter } from "export/formatter";
import { TableCell } from "./table-cell";
import { TableColumn } from "./table-column";
import { EMPTY_OBJECT } from "file/xml-components";
describe("TableColumn", () => {
let cells: TableCell[];
beforeEach(() => {
@ -37,15 +39,15 @@ describe("TableColumn", () => {
const tree = new Formatter().format(cells[0]);
expect(tree).to.deep.equal({
"w:tc": [{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "restart" } } }] }, { "w:p": {} }],
"w:tc": [{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "restart" } } }] }, { "w:p": EMPTY_OBJECT }],
});
const tree2 = new Formatter().format(cells[1]);
expect(tree2).to.deep.equal({ "w:tc": [{ "w:p": {} }] });
expect(tree2).to.deep.equal({ "w:tc": [{ "w:p": EMPTY_OBJECT }] });
const tree3 = new Formatter().format(cells[2]);
expect(tree3).to.deep.equal({
"w:tc": [{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "continue" } } }] }, { "w:p": {} }],
"w:tc": [{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "continue" } } }] }, { "w:p": EMPTY_OBJECT }],
});
});
});

View File

@ -5,13 +5,15 @@ import { Formatter } from "export/formatter";
import { TableCell } from "../table-cell";
import { TableRow } from "./table-row";
import { EMPTY_OBJECT } from "file/xml-components";
describe("TableRow", () => {
describe("#constructor", () => {
it("should create with no cells", () => {
const tableRow = new TableRow([]);
const tree = new Formatter().format(tableRow);
expect(tree).to.deep.equal({
"w:tr": {},
"w:tr": EMPTY_OBJECT,
});
});
@ -23,7 +25,7 @@ describe("TableRow", () => {
{
"w:tc": [
{
"w:p": {},
"w:p": EMPTY_OBJECT,
},
],
},

View File

@ -8,6 +8,8 @@ import { Table } from "./table";
// import { WidthType } from "./table-cell";
import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-properties";
import { EMPTY_OBJECT } from "file/xml-components";
const DEFAULT_TABLE_PROPERTIES = {
"w:tblCellMar": [
{
@ -107,7 +109,7 @@ const WIDTHS = {
// ],
// },
// { "w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }] },
// { "w:tr": [{ "w:tc": [{ "w:p": {} }] }] },
// { "w:tr": [{ "w:tc": [{ "w:p": EMPTY_OBJECT }] }] },
// ],
// };
@ -119,7 +121,7 @@ describe("Table", () => {
columns: 2,
});
const tree = new Formatter().format(table);
const cell = { "w:tc": [{ "w:p": {} }] };
const cell = { "w:tc": [{ "w:p": EMPTY_OBJECT }] };
expect(tree).to.deep.equal({
"w:tbl": [
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] },
@ -284,7 +286,7 @@ describe("Table", () => {
.to.be.an("array")
.which.has.length.at.least(1);
expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({
"w:tc": [{ "w:p": {} }],
"w:tc": [{ "w:p": EMPTY_OBJECT }],
});
});
@ -311,7 +313,7 @@ describe("Table", () => {
const cell = row["w:tr"].find((x) => x["w:tc"]);
expect(cell).not.to.be.undefined;
expect(cell["w:tc"][cell["w:tc"].length - 1]).to.deep.equal({
"w:p": {},
"w:p": EMPTY_OBJECT,
});
});