added tests for table v1

This commit is contained in:
felipe
2017-03-10 17:38:04 +01:00
parent e7e5c61a90
commit c0b0649f37
4 changed files with 129 additions and 3 deletions

View File

@ -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() {

View File

@ -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: {}}],
});
});
});
});

View File

@ -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}}]},
],
});
});
});
});

View File

@ -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")]},
],
});
});
});
});