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

504 lines
20 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";
2019-11-24 01:22:17 +00:00
import { AlignmentType, Paragraph } from "../paragraph";
2018-10-26 01:04:07 +01:00
import { Table } from "./table";
2019-03-08 01:09:21 +00:00
// 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
2019-09-29 04:17:21 +01:00
import { TableCell, WidthType } from "./table-cell";
2019-06-25 20:57:46 +01:00
import { TableLayoutType } from "./table-properties/table-layout";
2019-09-13 00:51:20 +01:00
import { TableRow } from "./table-row";
2018-03-22 23:04:46 +00:00
const DEFAULT_TABLE_PROPERTIES = {
2019-03-18 23:50:21 +00:00
"w:tblCellMar": [
2018-03-22 23:04:46 +00:00
{
"w:top": {
_attr: {
"w:type": "auto",
"w:w": 0,
2018-03-22 23:04:46 +00:00
},
},
2018-03-22 23:04:46 +00:00
},
{
"w:left": {
_attr: {
"w:type": "auto",
"w:w": 0,
2018-03-22 23:04:46 +00:00
},
},
2018-03-22 23:04:46 +00:00
},
{
"w:bottom": {
_attr: {
"w:type": "auto",
"w:w": 0,
2018-03-22 23:04:46 +00:00
},
},
2018-03-22 23:04:46 +00:00
},
{
"w:right": {
_attr: {
"w:type": "auto",
"w:w": 0,
2018-03-22 23:04:46 +00:00
},
},
2018-03-22 23:04:46 +00:00
},
2019-03-18 23:50:21 +00:00
],
};
const BORDERS = {
"w:tblBorders": [
{ "w:top": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:left": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:bottom": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:right": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:insideH": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
{ "w:insideV": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
2019-03-18 23:50:21 +00:00
],
};
const WIDTHS = {
"w:tblW": {
_attr: {
"w:type": "auto",
"w:w": 100,
2018-03-22 23:04:46 +00:00
},
},
2018-03-22 23:04:46 +00:00
};
2019-03-18 23:50:21 +00:00
// const f = {
// "w:tbl": [
// {
// "w:tblPr": [
// {
// "w:tblCellMar": [
// { "w:bottom": { _attr: { "w:type": "auto", "w:w": 0 } } },
// { "w:top": { _attr: { "w:type": "auto", "w:w": 0 } } },
// { "w:left": { _attr: { "w:type": "auto", "w:w": 0 } } },
// { "w:right": { _attr: { "w:type": "auto", "w:w": 0 } } },
2019-03-18 23:50:21 +00:00
// ],
// },
// {
// "w:tblBorders": [
// { "w:top": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
// { "w:left": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
// { "w:bottom": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
// { "w:right": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
// { "w:insideH": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
// { "w:insideV": { _attr: { "w:val": "single", "w:sz": 4, "w:space": 0, "w:color": "auto" } } },
2019-03-18 23:50:21 +00:00
// ],
// },
// { "w:tblW": { _attr: { "w:type": "auto", "w:w": 100 } } },
2019-03-18 23:50:21 +00:00
// {
// "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,
2019-03-18 23:50:21 +00:00
// },
// },
2019-03-18 23:50:21 +00:00
// },
// ],
// },
// { "w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }] },
// { "w:tr": [{ "w:tc": [{ "w:p": EMPTY_OBJECT }] }] },
2019-03-18 23:50:21 +00:00
// ],
// };
2017-03-10 17:38:04 +01:00
describe("Table", () => {
describe("#constructor", () => {
it("creates a table with the correct number of rows and columns", () => {
2019-03-18 23:50:21 +00:00
const table = new Table({
2019-09-13 00:51:20 +01:00
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
2019-03-18 23:50:21 +00:00
});
2017-03-10 17:38:04 +01:00
const tree = new Formatter().format(table);
2019-09-25 01:09:53 +01:00
const cell = {
"w:tc": [
{
"w:p": [
{
"w:r": [
{
"w:t": [
{
_attr: {
"xml:space": "preserve",
},
},
"hello",
],
},
],
},
],
},
],
};
2017-03-10 17:38:04 +01:00
expect(tree).to.deep.equal({
"w:tbl": [
{ "w:tblPr": [WIDTHS, BORDERS, DEFAULT_TABLE_PROPERTIES] },
2018-01-23 01:33:12 +00:00
{
"w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }, { "w:gridCol": { _attr: { "w:w": 100 } } }],
2018-01-23 01:33:12 +00:00
},
{ "w:tr": [cell, cell] },
{ "w:tr": [cell, cell] },
{ "w:tr": [cell, cell] },
2017-03-10 17:38:04 +01:00
],
});
});
2019-06-25 20:57:46 +01:00
it("creates a table with the correct columnSpan and rowSpan", () => {
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
columnSpan: 2,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
rowSpan: 2,
}),
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
});
const tree = new Formatter().format(table);
const cellP = { "w:p": [{ "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "hello"] }] }] };
expect(tree).to.deep.equal({
"w:tbl": [
{ "w:tblPr": [WIDTHS, BORDERS, DEFAULT_TABLE_PROPERTIES] },
{
"w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }, { "w:gridCol": { _attr: { "w:w": 100 } } }],
},
{
"w:tr": [
{
"w:tc": [{ "w:tcPr": [{ "w:gridSpan": { _attr: { "w:val": 2 } } }] }, cellP],
},
],
},
{
"w:tr": [
{
"w:tc": [{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "restart" } } }] }, cellP],
},
{ "w:tc": [cellP] },
],
},
{
"w:tr": [
{
"w:tc": [{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "continue" } } }] }, { "w:p": {} }],
},
{ "w:tc": [cellP] },
],
},
],
});
});
2019-06-25 20:57:46 +01:00
it("sets the table to fixed width layout", () => {
const table = new Table({
2019-09-13 00:51:20 +01:00
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
2019-06-25 20:57:46 +01:00
layout: TableLayoutType.FIXED,
});
const tree = new Formatter().format(table);
2020-08-01 17:40:57 +01:00
expect(tree).to.have.property("w:tbl").which.is.an("array").with.has.length.at.least(1);
2019-06-25 20:57:46 +01:00
expect(tree["w:tbl"][0]).to.deep.equal({
"w:tblPr": [WIDTHS, BORDERS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }, DEFAULT_TABLE_PROPERTIES],
2019-06-25 20:57:46 +01:00
});
});
2019-09-29 04:17:21 +01:00
2019-11-24 01:22:17 +00:00
it("should center the table", () => {
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
alignment: AlignmentType.CENTER,
});
const tree = new Formatter().format(table);
2020-08-01 17:40:57 +01:00
expect(tree).to.have.property("w:tbl").which.is.an("array").with.has.length.at.least(1);
2019-11-24 01:22:17 +00:00
expect(tree["w:tbl"][0]).to.deep.equal({
"w:tblPr": [WIDTHS, { "w:jc": { _attr: { "w:val": "center" } } }, BORDERS, DEFAULT_TABLE_PROPERTIES],
2019-11-24 01:22:17 +00:00
});
});
2019-09-29 04:17:21 +01:00
it("should set the table to provided width", () => {
const table = new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
width: {
size: 100,
type: WidthType.PERCENTAGE,
},
layout: TableLayoutType.FIXED,
});
const tree = new Formatter().format(table);
2020-08-01 17:40:57 +01:00
expect(tree).to.have.property("w:tbl").which.is.an("array").with.has.length.at.least(1);
2019-09-29 04:17:21 +01:00
expect(tree["w:tbl"][0]).to.deep.equal({
"w:tblPr": [
{
"w:tblW": {
_attr: {
"w:type": "pct",
"w:w": "100%",
},
},
},
BORDERS,
2019-09-29 04:17:21 +01:00
{ "w:tblLayout": { _attr: { "w:type": "fixed" } } },
DEFAULT_TABLE_PROPERTIES,
2019-09-29 04:17:21 +01:00
],
});
});
2017-03-10 17:38:04 +01:00
});
describe("Cell", () => {
describe("#prepForXml", () => {
it("inserts a paragraph at the end of the cell if it is empty", () => {
2019-03-18 23:50:21 +00:00
const table = new Table({
2019-09-13 00:51:20 +01:00
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
2019-03-18 23:50:21 +00:00
});
const tree = new Formatter().format(table);
2020-08-01 17:40:57 +01: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;
2020-08-01 17:40:57 +01: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({
2019-09-25 01:09:53 +01:00
"w:tc": [
{
"w:p": [
{
"w:r": [
{
"w:t": [
{
_attr: {
"xml:space": "preserve",
},
},
"hello",
],
},
],
},
],
},
],
});
});
2019-09-13 00:51:20 +01:00
// it("inserts a paragraph at the end of the cell even if it has a child table", () => {
// const table = new Table({
// rows: [
// new TableRow({
// children: [
// new TableCell({
// children: [new Paragraph("hello")],
// }),
// ],
// }),
// ],
// });
// table.getCell(0, 0).add(
// new Table({
// rows: [
// new TableRow({
// children: [
// new TableCell({
// children: [new Paragraph("hello")],
// }),
// ],
// }),
// ],
// }),
// );
// const tree = new Formatter().format(table);
// 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;
// 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({
// "w:p": EMPTY_OBJECT,
// });
// });
2019-09-13 00:51:20 +01:00
// it("does not insert a paragraph if it already ends with one", () => {
// const table = new Table({
// rows: [
// new TableRow({
// children: [
// new TableCell({
// children: [new Paragraph("hello")],
// }),
// ],
// }),
// ],
// });
// table.getCell(0, 0).add(new Paragraph("Hello"));
// const tree = new Formatter().format(table);
// 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;
// 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": [
// {
// "w:p": [{ "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "Hello"] }] }],
// },
// ],
// });
// });
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", () => {
2019-03-18 23:50:21 +00:00
const table = new Table({
2019-09-13 00:51:20 +01:00
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
2019-03-18 23:50:21 +00:00
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 20:03:29 +01:00
});
2018-10-23 09:08:43 -02:00
const tree = new Formatter().format(table);
2020-08-01 17:40:57 +01:00
expect(tree).to.have.property("w:tbl").which.is.an("array").with.has.length.at.least(1);
2018-10-23 09:08:43 -02:00
expect(tree["w:tbl"][0]).to.deep.equal({
"w:tblPr": [
{
"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,
2018-10-23 09:08:43 -02:00
},
},
2018-10-23 09:08:43 -02:00
},
WIDTHS,
BORDERS,
DEFAULT_TABLE_PROPERTIES,
2018-10-23 09:08:43 -02:00
],
});
});
});
2017-03-10 17:38:04 +01:00
});