diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index 15656c6e45..f205f9a96b 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -1,7 +1,7 @@ import { Paragraph } from "../paragraph"; import { XmlComponent } from "../xml-components"; -import { GridCol, TableGrid } from "./grid"; +import { TableGrid } from "./grid"; import { TableProperties } from "./properties"; export class Table extends XmlComponent { @@ -15,7 +15,7 @@ export class Table extends XmlComponent { this.root.push(this.properties); const gridCols: number[] = []; - for (let i = 0; i++; i < cols) { + for (let i = 0; i < cols; i++) { gridCols.push(0); } this.grid = new TableGrid(gridCols); @@ -66,7 +66,7 @@ class TableRowProperties extends XmlComponent { } class TableCell extends XmlComponent { - public content: XmlComponent; + public content: Paragraph; private properties: TableCellProperties; constructor() { diff --git a/ts/tests/docx/table/testGrid.ts b/ts/tests/docx/table/testGrid.ts new file mode 100644 index 0000000000..edb0b50d4b --- /dev/null +++ b/ts/tests/docx/table/testGrid.ts @@ -0,0 +1,47 @@ +import { expect } from "chai"; +import { GridCol, TableGrid } from "../../../docx/table/grid"; +import { Formatter } from "../../../export/formatter"; + +describe("GridCol", () => { + describe("#constructor", () => { + it("sets the width attribute to the value given", () => { + const grid = new GridCol(1234); + const tree = new Formatter().format(grid); + expect(tree).to.deep.equal({ + "w:gridCol": [{_attr: {"w:w": 1234}}], + }); + }); + + 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": [{_attr: {}}], + }); + }); + }); +}); + +describe("TableGrid", () => { + describe("#constructor", () => { + it("creates a column for each width given", () => { + const grid = new TableGrid([1234, 321, 123]); + const tree = new Formatter().format(grid); + expect(tree).to.deep.equal({ + "w:tblGrid": [ + {"w:gridCol": [{_attr: {"w:w": 1234}}]}, + {"w:gridCol": [{_attr: {"w:w": 321}}]}, + {"w:gridCol": [{_attr: {"w:w": 123}}]}, + ], + }); + }); + + 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": [{_attr: {}}], + }); + }); + }); +}); diff --git a/ts/tests/docx/table/testProperties.ts b/ts/tests/docx/table/testProperties.ts new file mode 100644 index 0000000000..50da89489e --- /dev/null +++ b/ts/tests/docx/table/testProperties.ts @@ -0,0 +1,25 @@ +import { expect } from "chai"; +import { TableProperties } from "../../../docx/table/properties"; +import { Formatter } from "../../../export/formatter"; + +describe("TableProperties", () => { + describe("#constructor", () => { + it("creates an initially empty property object", () => { + const tp = new TableProperties(); + const tree = new Formatter().format(tp); + expect(tree).to.deep.equal({"w:tblPr": []}); + }); + }); + + describe("#setWidth", () => { + it("adds a table width property", () => { + const tp = new TableProperties().setWidth("dxa", 1234); + const tree = new Formatter().format(tp); + expect(tree).to.deep.equal({ + "w:tblPr": [ + {"w:tblW": [{_attr: {"w:type": "dxa", "w:w": 1234}}]}, + ], + }); + }); + }); +}); diff --git a/ts/tests/docx/table/testTable.ts b/ts/tests/docx/table/testTable.ts new file mode 100644 index 0000000000..9a5d855da0 --- /dev/null +++ b/ts/tests/docx/table/testTable.ts @@ -0,0 +1,54 @@ +import { expect } from "chai"; +import { Table } from "../../../docx/table"; +import { Formatter } from "../../../export/formatter"; + +describe("Table", () => { + describe("#constructor", () => { + it("creates a table with the correct number of rows and columns", () => { + const table = new Table(3, 2); + const tree = new Formatter().format(table); + const cell = {"w:tc": [{"w:tcPr": []}, {"w:p": [{"w:pPr": []}]}]}; + expect(tree).to.deep.equal({ + "w:tbl": [ + {"w:tblPr": []}, + {"w:tblGrid": [ + {"w:gridCol": [{_attr: {"w:w": 0}}]}, + {"w:gridCol": [{_attr: {"w:w": 0}}]}, + ]}, + {"w:tr": [{"w:trPr": []}, cell, cell]}, + {"w:tr": [{"w:trPr": []}, cell, cell]}, + {"w:tr": [{"w:trPr": []}, cell, cell]}, + ], + }); + }); + }); + + describe("#getRow and Row#getCell", () => { + it("returns the correct row", () => { + const table = new Table(2, 2); + table.getRow(0).getCell(0).content.createTextRun("A1"); + table.getRow(0).getCell(1).content.createTextRun("B1"); + table.getRow(1).getCell(0).content.createTextRun("A2"); + table.getRow(1).getCell(1).content.createTextRun("B2"); + const tree = new Formatter().format(table); + const cell = (c) => ({"w:tc": [ + {"w:tcPr": []}, + {"w:p": [ + {"w:pPr": []}, + {"w:r": [{"w:rPr": []}, {"w:t": [c]}]}, + ]}, + ]}); + expect(tree).to.deep.equal({ + "w:tbl": [ + {"w:tblPr": []}, + {"w:tblGrid": [ + {"w:gridCol": [{_attr: {"w:w": 0}}]}, + {"w:gridCol": [{_attr: {"w:w": 0}}]}, + ]}, + {"w:tr": [{"w:trPr": []}, cell("A1"), cell("B1")]}, + {"w:tr": [{"w:trPr": []}, cell("A2"), cell("B2")]}, + ], + }); + }); + }); +});