Files
docx-js/src/file/table/table.spec.ts

345 lines
13 KiB
TypeScript
Raw Normal View History

2017-04-15 17:54:47 +01:00
/* tslint:disable:no-unused-expression */
2017-03-10 17:38:04 +01:00
import { expect } from "chai";
2018-10-26 01:04:07 +01:00
import { Formatter } from "export/formatter";
import { Paragraph } from "../paragraph";
2018-10-26 01:04:07 +01:00
import { Table } from "./table";
import { WidthType } from "./table-cell";
2018-10-23 23:44:50 +01:00
import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-properties";
2017-03-10 17:38:04 +01:00
2018-03-22 23:04:46 +00:00
const DEFAULT_TABLE_PROPERTIES = {
"w:tblBorders": [
{
"w:top": [
{
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
},
],
},
{
"w:left": [
{
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
},
],
},
{
"w:bottom": [
{
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
},
],
},
{
"w:right": [
{
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
},
],
},
{
"w:insideH": [
{
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
},
],
},
{
"w:insideV": [
{
_attr: {
"w:color": "auto",
"w:space": 0,
"w:sz": 4,
"w:val": "single",
},
},
],
},
],
};
2017-03-10 17:38:04 +01:00
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);
2018-01-23 01:33:12 +00:00
const cell = { "w:tc": [{ "w:tcPr": [] }, { "w:p": [{ "w:pPr": [] }] }] };
2017-03-10 17:38:04 +01:00
expect(tree).to.deep.equal({
"w:tbl": [
2018-03-22 23:04:46 +00:00
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES] },
2018-01-23 01:33:12 +00:00
{
"w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 1 } }] }, { "w:gridCol": [{ _attr: { "w:w": 1 } }] }],
},
{ "w:tr": [{ "w:trPr": [] }, cell, cell] },
{ "w:tr": [{ "w:trPr": [] }, cell, cell] },
{ "w:tr": [{ "w:trPr": [] }, cell, cell] },
2017-03-10 17:38:04 +01:00
],
});
});
});
describe("#getRow and Row#getCell", () => {
it("returns the correct row", () => {
const table = new Table(2, 2);
2018-01-23 01:33:12 +00:00
table
.getRow(0)
.getCell(0)
.addContent(new Paragraph("A1"));
table
.getRow(0)
.getCell(1)
.addContent(new Paragraph("B1"));
table
.getRow(1)
.getCell(0)
.addContent(new Paragraph("A2"));
table
.getRow(1)
.getCell(1)
.addContent(new Paragraph("B2"));
2017-03-10 17:38:04 +01:00
const tree = new Formatter().format(table);
2018-01-23 01:33:12 +00:00
const cell = (c) => ({
"w:tc": [
{ "w:tcPr": [] },
{
"w:p": [{ "w:pPr": [] }, { "w:r": [{ "w:rPr": [] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, c] }] }],
},
],
});
2017-03-10 17:38:04 +01:00
expect(tree).to.deep.equal({
"w:tbl": [
2018-03-22 23:04:46 +00:00
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES] },
2018-01-23 01:33:12 +00:00
{
"w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 1 } }] }, { "w:gridCol": [{ _attr: { "w:w": 1 } }] }],
},
{ "w:tr": [{ "w:trPr": [] }, cell("A1"), cell("B1")] },
{ "w:tr": [{ "w:trPr": [] }, cell("A2"), cell("B2")] },
2017-03-10 17:38:04 +01:00
],
});
});
});
2017-03-10 17:48:05 +01:00
describe("#getCell", () => {
it("returns the correct cell", () => {
const table = new Table(2, 2);
2017-03-11 21:15:45 +01:00
table.getCell(0, 0).addContent(new Paragraph("A1"));
table.getCell(0, 1).addContent(new Paragraph("B1"));
table.getCell(1, 0).addContent(new Paragraph("A2"));
table.getCell(1, 1).addContent(new Paragraph("B2"));
2017-03-10 17:48:05 +01:00
const tree = new Formatter().format(table);
2018-01-23 01:33:12 +00:00
const cell = (c) => ({
"w:tc": [
{ "w:tcPr": [] },
{
"w:p": [{ "w:pPr": [] }, { "w:r": [{ "w:rPr": [] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, c] }] }],
},
],
});
2017-03-10 17:48:05 +01:00
expect(tree).to.deep.equal({
"w:tbl": [
2018-03-22 23:04:46 +00:00
{ "w:tblPr": [DEFAULT_TABLE_PROPERTIES] },
2018-01-23 01:33:12 +00:00
{
"w:tblGrid": [{ "w:gridCol": [{ _attr: { "w:w": 1 } }] }, { "w:gridCol": [{ _attr: { "w:w": 1 } }] }],
},
{ "w:tr": [{ "w:trPr": [] }, cell("A1"), cell("B1")] },
{ "w:tr": [{ "w:trPr": [] }, cell("A2"), cell("B2")] },
2017-03-10 17:48:05 +01:00
],
});
});
});
2017-03-10 18:54:35 +01:00
describe("#setWidth", () => {
it("sets the preferred width on the table", () => {
const table = new Table(2, 2).setWidth(WidthType.PERCENTAGE, 1000);
2017-03-10 18:54:35 +01:00
const tree = new Formatter().format(table);
2018-01-23 01:33:12 +00:00
expect(tree)
.to.have.property("w:tbl")
.which.is.an("array")
.with.has.length.at.least(1);
2017-03-10 18:54:35 +01:00
expect(tree["w:tbl"][0]).to.deep.equal({
2018-03-22 23:04:46 +00:00
"w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "pct", "w:w": 1000 } }] }],
2017-03-10 18:54:35 +01:00
});
});
});
2018-08-07 01:25:28 +01:00
describe("#setFixedWidthLayout", () => {
it("sets the table to fixed width layout", () => {
2018-08-07 01:25:28 +01:00
const table = new Table(2, 2).setFixedWidthLayout();
const tree = new Formatter().format(table);
2018-01-23 01:33:12 +00:00
expect(tree)
.to.have.property("w:tbl")
.which.is.an("array")
.with.has.length.at.least(1);
expect(tree["w:tbl"][0]).to.deep.equal({
2018-03-22 23:04:46 +00:00
"w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblLayout": [{ _attr: { "w:type": "fixed" } }] }],
});
});
});
describe("Cell", () => {
describe("#prepForXml", () => {
it("inserts a paragraph at the end of the cell if it is empty", () => {
const table = new Table(1, 1);
const tree = new Formatter().format(table);
2018-01-23 01:33:12 +00:00
expect(tree)
.to.have.property("w:tbl")
.which.is.an("array");
const row = tree["w:tbl"].find((x) => x["w:tr"]);
expect(row).not.to.be.undefined;
2018-01-23 01:33:12 +00:00
expect(row["w:tr"])
.to.be.an("array")
.which.has.length.at.least(1);
expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({
2018-01-23 01:33:12 +00:00
"w:tc": [{ "w:tcPr": [] }, { "w:p": [{ "w:pPr": [] }] }],
});
});
it("inserts a paragraph at the end of the cell even if it has a child table", () => {
const parentTable = new Table(1, 1);
2017-03-11 21:15:45 +01:00
parentTable.getCell(0, 0).addContent(new Table(1, 1));
const tree = new Formatter().format(parentTable);
2018-01-23 01:33:12 +00:00
expect(tree)
.to.have.property("w:tbl")
.which.is.an("array");
const row = tree["w:tbl"].find((x) => x["w:tr"]);
expect(row).not.to.be.undefined;
2018-01-23 01:33:12 +00:00
expect(row["w:tr"])
.to.be.an("array")
.which.has.length.at.least(1);
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({
2018-01-23 01:33:12 +00:00
"w:p": [{ "w:pPr": [] }],
});
});
it("does not insert a paragraph if it already ends with one", () => {
const parentTable = new Table(1, 1);
2017-03-11 21:15:45 +01:00
parentTable.getCell(0, 0).addContent(new Paragraph("Hello"));
const tree = new Formatter().format(parentTable);
2018-01-23 01:33:12 +00:00
expect(tree)
.to.have.property("w:tbl")
.which.is.an("array");
const row = tree["w:tbl"].find((x) => x["w:tr"]);
expect(row).not.to.be.undefined;
2018-01-23 01:33:12 +00:00
expect(row["w:tr"])
.to.be.an("array")
.which.has.length.at.least(1);
expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({
"w:tc": [
2018-01-23 01:33:12 +00:00
{ "w:tcPr": [] },
{
"w:p": [
{ "w:pPr": [] },
{ "w:r": [{ "w:rPr": [] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, "Hello"] }] },
],
},
],
});
});
});
2017-03-11 10:30:08 +01:00
describe("#createParagraph", () => {
it("inserts a new paragraph in the cell", () => {
const table = new Table(1, 1);
const para = table.getCell(0, 0).createParagraph("Test paragraph");
expect(para).to.be.an.instanceof(Paragraph);
const tree = new Formatter().format(table);
2018-01-23 01:33:12 +00:00
expect(tree)
.to.have.property("w:tbl")
.which.is.an("array");
2017-03-11 10:30:08 +01:00
const row = tree["w:tbl"].find((x) => x["w:tr"]);
expect(row).not.to.be.undefined;
2018-01-23 01:33:12 +00:00
expect(row["w:tr"])
.to.be.an("array")
.which.has.length.at.least(1);
2017-03-11 10:30:08 +01:00
expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({
"w:tc": [
2018-01-23 01:33:12 +00:00
{ "w:tcPr": [] },
{
"w:p": [
{ "w:pPr": [] },
{
"w:r": [{ "w:rPr": [] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, "Test paragraph"] }],
},
],
},
2017-03-11 10:30:08 +01:00
],
});
});
});
});
2018-10-23 09:08:43 -02:00
2018-10-23 20:03:29 +01:00
describe("#float", () => {
2018-10-23 09:08:43 -02:00
it("sets the table float properties", () => {
2018-10-23 20:03:29 +01:00
const table = new Table(1, 1).float({
horizontalAnchor: TableAnchorType.MARGIN,
verticalAnchor: TableAnchorType.PAGE,
absoluteHorizontalPosition: 10,
relativeHorizontalPosition: RelativeHorizontalPosition.CENTER,
absoluteVerticalPosition: 20,
relativeVerticalPosition: RelativeVerticalPosition.BOTTOM,
bottomFromText: 30,
topFromText: 40,
leftFromText: 50,
rightFromText: 60,
});
2018-10-23 09:08:43 -02:00
const tree = new Formatter().format(table);
expect(tree)
.to.have.property("w:tbl")
.which.is.an("array")
.with.has.length.at.least(1);
expect(tree["w:tbl"][0]).to.deep.equal({
"w:tblPr": [
DEFAULT_TABLE_PROPERTIES,
{
"w:tblpPr": [
{
_attr: {
"w:horzAnchor": "margin",
"w:vertAnchor": "page",
"w:tblpX": 10,
"w:tblpXSpec": "center",
"w:tblpY": 20,
"w:tblpYSpec": "bottom",
"w:bottomFromText": 30,
"w:topFromText": 40,
"w:leftFromText": 50,
"w:rightFromText": 60,
},
},
],
},
],
});
});
});
2017-03-10 17:38:04 +01:00
});