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

173 lines
5.5 KiB
TypeScript
Raw Normal View History

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 } from "../../paragraph";
2019-09-26 02:14:52 +01:00
import { ShadingType } from "../shading";
2018-10-23 23:44:50 +01:00
import { WidthType } from "../table-cell";
2019-06-25 20:57:46 +01:00
import { TableLayoutType } from "./table-layout";
2018-10-23 23:44:50 +01:00
import { TableProperties } from "./table-properties";
2017-03-10 17:38:04 +01:00
describe("TableProperties", () => {
describe("#constructor", () => {
it("creates an initially empty property object", () => {
2021-03-04 01:42:58 +00:00
const tp = new TableProperties({});
// The TableProperties is ignorable if there are no attributes,
// which results in prepForXml returning undefined, which causes
// the formatter to throw an error if that is the only object it
// has been asked to format.
expect(() => new Formatter().format(tp)).to.throw("XMLComponent did not format correctly");
2017-03-10 17:38:04 +01:00
});
});
2019-10-21 13:09:44 -05:00
describe("#setStyle", () => {
it("should add a table style property", () => {
2021-03-07 21:43:30 +00:00
const tp = new TableProperties({
style: "TableNormal",
});
2019-10-21 13:09:44 -05:00
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblStyle": { _attr: { "w:val": "TableNormal" } } }],
});
});
});
2017-03-10 17:38:04 +01:00
describe("#setWidth", () => {
it("should add a table width property", () => {
2021-03-04 01:42:58 +00:00
const tp = new TableProperties({
width: {
size: 1234,
type: WidthType.DXA,
},
});
2017-03-10 17:38:04 +01:00
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblW": { _attr: { "w:type": "dxa", "w:w": 1234 } } }],
2017-03-10 17:38:04 +01:00
});
});
it("should add a table width property with default of AUTO", () => {
2021-03-04 01:42:58 +00:00
const tp = new TableProperties({
width: {
size: 1234,
},
});
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblW": { _attr: { "w:type": "auto", "w:w": 1234 } } }],
});
});
2017-03-10 17:38:04 +01:00
});
2019-06-25 20:57:46 +01:00
describe("#setLayout", () => {
it("sets the table to fixed width layout", () => {
2021-03-04 01:42:58 +00:00
const tp = new TableProperties({
layout: TableLayoutType.FIXED,
});
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblLayout": { _attr: { "w:type": "fixed" } } }],
});
});
});
2018-08-23 00:55:33 +01:00
describe("#cellMargin", () => {
it("adds a table cell top margin", () => {
2021-03-04 01:42:58 +00:00
const tp = new TableProperties({
cellMargin: {
top: {
value: 1234,
type: WidthType.DXA,
},
},
});
2018-08-23 00:55:33 +01:00
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblCellMar": [{ "w:top": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }],
2018-08-23 00:55:33 +01:00
});
});
2018-11-01 02:22:32 +00:00
it("adds a table cell left margin", () => {
2021-03-04 01:42:58 +00:00
const tp = new TableProperties({
cellMargin: {
left: {
value: 1234,
type: WidthType.DXA,
},
},
});
2018-11-01 02:22:32 +00:00
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [{ "w:tblCellMar": [{ "w:left": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }],
2018-11-01 02:22:32 +00:00
});
});
2018-08-23 00:55:33 +01:00
});
2019-09-26 02:14:52 +01:00
describe("#setShading", () => {
it("sets the shading of the table", () => {
2021-03-04 01:42:58 +00:00
const tp = new TableProperties({
shading: {
fill: "b79c2f",
val: ShadingType.REVERSE_DIAGONAL_STRIPE,
color: "auto",
},
2019-09-26 02:14:52 +01:00
});
2021-03-04 01:42:58 +00:00
2019-09-26 02:14:52 +01:00
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [
{
"w:shd": {
_attr: {
"w:color": "auto",
"w:fill": "b79c2f",
"w:val": "reverseDiagStripe",
},
},
},
],
});
});
});
2019-11-24 01:22:17 +00:00
describe("#setAlignment", () => {
2021-03-04 01:42:58 +00:00
it("sets the alignment of the table", () => {
const tp = new TableProperties({
alignment: AlignmentType.CENTER,
});
2019-11-24 01:22:17 +00:00
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [
{
"w:jc": {
_attr: {
"w:val": "center",
},
},
},
],
});
});
});
describe("#Set Virtual Right to Left", () => {
it("sets the alignment of the table", () => {
const tp = new TableProperties({
visuallyRightToLeft: true,
});
const tree = new Formatter().format(tp);
expect(tree).to.deep.equal({
"w:tblPr": [
{
"w:bidiVisual": {},
},
],
});
});
});
2017-03-10 17:38:04 +01:00
});