Files
docx-js/src/file/document/body/section-properties/section-properties.spec.ts

217 lines
9.5 KiB
TypeScript
Raw Normal View History

2018-01-25 01:21:03 +00:00
import { expect } from "chai";
2020-12-24 03:37:43 +00:00
import { convertInchesToTwip } from "convenience-functions";
2018-10-26 01:04:07 +01:00
import { Formatter } from "export/formatter";
import { FooterWrapper } from "file/footer-wrapper";
import { HeaderWrapper } from "file/header-wrapper";
import { Media } from "file/media";
import { PageBorderOffsetFrom } from "./page-border";
import { PageNumberFormat } from "./page-number";
2018-01-25 01:21:03 +00:00
import { SectionProperties } from "./section-properties";
2021-02-22 21:04:02 +00:00
import { SectionType } from "./type/section-type-attributes";
2019-10-31 17:01:17 +02:00
import { SectionVerticalAlignValue } from "./vertical-align";
2018-01-25 01:21:03 +00:00
describe("SectionProperties", () => {
describe("#constructor()", () => {
it("should create section properties with options", () => {
2018-10-23 00:31:51 +01:00
const media = new Media();
2018-01-25 01:21:03 +00:00
const properties = new SectionProperties({
width: 11906,
height: 16838,
2020-12-24 03:37:43 +00:00
top: convertInchesToTwip(1),
right: convertInchesToTwip(1),
bottom: convertInchesToTwip(1),
left: convertInchesToTwip(1),
2018-01-25 01:21:03 +00:00
header: 708,
footer: 708,
gutter: 0,
mirror: false,
2019-06-28 01:57:43 +01:00
column: {
space: 708,
count: 1,
},
2020-12-24 03:37:43 +00:00
linePitch: convertInchesToTwip(0.25),
2018-10-05 01:33:17 +01:00
headers: {
2018-10-23 00:31:51 +01:00
default: new HeaderWrapper(media, 100),
2018-10-05 01:33:17 +01:00
},
footers: {
2018-10-23 00:31:51 +01:00
even: new FooterWrapper(media, 200),
2018-10-05 01:33:17 +01:00
},
pageNumberStart: 10,
pageNumberFormatType: PageNumberFormat.CARDINAL_TEXT,
2019-09-29 04:17:21 +01:00
titlePage: true,
2019-10-31 17:01:17 +02:00
verticalAlign: SectionVerticalAlignValue.TOP,
2018-01-25 01:21:03 +00:00
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
2018-01-25 01:21:03 +00:00
expect(tree["w:sectPr"][1]).to.deep.equal({
"w:pgMar": {
_attr: {
"w:bottom": 1440,
"w:footer": 708,
"w:top": 1440,
"w:right": 1440,
"w:left": 1440,
"w:header": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
2018-01-25 01:21:03 +00:00
},
},
2018-01-25 01:21:03 +00:00
});
2019-06-21 01:22:27 +05:30
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } });
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
expect(tree["w:sectPr"][4]).to.deep.equal({ "w:headerReference": { _attr: { "r:id": "rId100", "w:type": "default" } } });
expect(tree["w:sectPr"][5]).to.deep.equal({ "w:footerReference": { _attr: { "r:id": "rId200", "w:type": "even" } } });
expect(tree["w:sectPr"][6]).to.deep.equal({ "w:pgNumType": { _attr: { "w:fmt": "cardinalText", "w:start": 10 } } });
2018-01-25 01:21:03 +00:00
});
it("should create section properties with no options", () => {
const properties = new SectionProperties();
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
2018-01-25 01:21:03 +00:00
expect(tree["w:sectPr"][1]).to.deep.equal({
"w:pgMar": {
_attr: {
"w:bottom": 1440,
"w:footer": 708,
"w:top": 1440,
"w:right": 1440,
"w:left": 1440,
"w:header": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
2018-01-25 01:21:03 +00:00
},
},
2018-01-25 01:21:03 +00:00
});
2019-06-21 01:22:27 +05:30
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } });
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
2018-01-25 01:21:03 +00:00
});
it("should create section properties with changed options", () => {
const properties = new SectionProperties({
top: 0,
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
2018-01-25 01:21:03 +00:00
expect(tree["w:sectPr"][1]).to.deep.equal({
"w:pgMar": {
_attr: {
"w:bottom": 1440,
"w:footer": 708,
"w:top": 0,
"w:right": 1440,
"w:left": 1440,
"w:header": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
2018-01-25 01:21:03 +00:00
},
},
2018-01-25 01:21:03 +00:00
});
});
it("should create section properties with changed options", () => {
const properties = new SectionProperties({
bottom: 0,
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 16838, "w:w": 11906, "w:orient": "portrait" } } });
2018-01-25 01:21:03 +00:00
expect(tree["w:sectPr"][1]).to.deep.equal({
"w:pgMar": {
_attr: {
"w:bottom": 0,
"w:footer": 708,
"w:top": 1440,
"w:right": 1440,
"w:left": 1440,
"w:header": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
2018-01-25 01:21:03 +00:00
},
},
2018-01-25 01:21:03 +00:00
});
});
it("should create section properties with changed options", () => {
const properties = new SectionProperties({
width: 0,
height: 0,
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 0, "w:w": 0, "w:orient": "portrait" } } });
2018-01-25 01:21:03 +00:00
expect(tree["w:sectPr"][1]).to.deep.equal({
"w:pgMar": {
_attr: {
"w:bottom": 1440,
"w:footer": 708,
"w:top": 1440,
"w:right": 1440,
"w:left": 1440,
"w:header": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
2018-01-25 01:21:03 +00:00
},
},
2018-01-25 01:21:03 +00:00
});
});
it("should create section properties with page borders", () => {
const properties = new SectionProperties({
pageBorders: {
offsetFrom: PageBorderOffsetFrom.PAGE,
},
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
2018-09-17 11:29:01 +03:00
const pgBorders = tree["w:sectPr"].find((item) => item["w:pgBorders"] !== undefined);
2018-09-17 11:24:56 +03:00
expect(pgBorders).to.deep.equal({
"w:pgBorders": { _attr: { "w:offsetFrom": "page" } },
});
});
it("should create section properties with page number type, but without start attribute", () => {
const properties = new SectionProperties({
pageNumberFormatType: PageNumberFormat.UPPER_ROMAN,
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined);
expect(pgNumType).to.deep.equal({
"w:pgNumType": { _attr: { "w:fmt": "upperRoman" } },
});
});
it("should create section properties without page number type", () => {
const properties = new SectionProperties({});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined);
expect(pgNumType).to.equal(undefined);
});
2021-02-22 21:04:02 +00:00
it("should create section properties with section type", () => {
const properties = new SectionProperties({
type: SectionType.CONTINUOUS,
});
const tree = new Formatter().format(properties);
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
const type = tree["w:sectPr"].find((item) => item["w:type"] !== undefined);
expect(type).to.deep.equal({
"w:type": { _attr: { "w:val": "continuous" } },
});
});
2018-01-25 01:21:03 +00:00
});
});