added document#createTable

This commit is contained in:
felipe
2017-03-11 09:02:36 +01:00
parent 62a238de84
commit 210b97d00b
2 changed files with 31 additions and 0 deletions

View File

@ -45,4 +45,11 @@ export class Document extends XmlComponent {
public addTable(table: Table): void { public addTable(table: Table): void {
this.body.push(table); this.body.push(table);
} }
public createTable(rows: number, cols: number): Table {
const table = new Table(rows, cols);
this.addTable(table);
return table;
}
} }

View File

@ -46,4 +46,28 @@ describe("Document", () => {
}); });
}); });
}); });
describe("#createTable", () => {
it("should create a new table and append it to body", () => {
const table = document.createTable(2, 3);
expect(table).to.be.an.instanceof(docx.Table);
const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body).to.be.an("array").which.has.length.at.least(1);
expect(body[0]).to.have.property("w:tbl");
});
it("should create a table with the correct dimensions", () => {
const table = document.createTable(2, 3);
const body = new Formatter().format(document)["w:document"][1]["w:body"];
expect(body).to.be.an("array").which.has.length.at.least(1);
expect(body[0]).to.have.property("w:tbl").which.includes({
"w:tblGrid": [
{"w:gridCol": [{_attr: {"w:w": 1}}]},
{"w:gridCol": [{_attr: {"w:w": 1}}]},
{"w:gridCol": [{_attr: {"w:w": 1}}]},
],
});
expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2);
});
});
}); });