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

161 lines
5.6 KiB
TypeScript
Raw Normal View History

2018-11-02 00:42:49 +00:00
import { expect } from "chai";
2018-12-16 01:56:42 +00:00
import * as sinon from "sinon";
2018-11-02 00:42:49 +00:00
import { Formatter } from "export/formatter";
import { File } from "./file";
2019-07-31 08:48:02 +01:00
import { Footer, Header } from "./header";
2018-12-16 01:56:42 +00:00
import { Paragraph } from "./paragraph";
2019-09-13 00:51:20 +01:00
import { Table, TableCell, TableRow } from "./table";
import { TableOfContents } from "./table-of-contents";
2018-11-02 00:42:49 +00:00
describe("File", () => {
describe("#constructor", () => {
2019-07-31 08:48:02 +01:00
it("should create with correct headers and footers by default", () => {
const doc = new File();
doc.addSection({
children: [],
});
const tree = new Formatter().format(doc.Document.Body);
expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][0]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default");
2019-07-31 08:48:02 +01:00
});
2019-01-03 02:36:56 +00:00
it("should create with correct headers and footers", () => {
2018-11-02 00:42:49 +00:00
const doc = new File();
2019-07-31 08:48:02 +01:00
doc.addSection({
2018-11-02 00:42:49 +00:00
headers: {
2019-07-31 08:48:02 +01:00
default: new Header(),
2018-11-02 00:42:49 +00:00
},
footers: {
2019-07-31 08:48:02 +01:00
default: new Footer(),
2018-11-02 00:42:49 +00:00
},
2019-07-31 08:48:02 +01:00
children: [],
2018-11-02 00:42:49 +00:00
});
const tree = new Formatter().format(doc.Document.Body);
expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][0]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default");
2018-11-02 00:42:49 +00:00
});
2019-01-03 02:36:56 +00:00
it("should create with first headers and footers", () => {
const doc = new File();
2019-07-31 08:48:02 +01:00
doc.addSection({
2019-01-03 02:36:56 +00:00
headers: {
2019-07-31 08:48:02 +01:00
first: new Header(),
2019-01-03 02:36:56 +00:00
},
footers: {
2019-07-31 08:48:02 +01:00
first: new Footer(),
2019-01-03 02:36:56 +00:00
},
2019-07-31 08:48:02 +01:00
children: [],
2019-01-03 02:36:56 +00:00
});
const tree = new Formatter().format(doc.Document.Body);
expect(tree["w:body"][0]["w:sectPr"][5]["w:headerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][0]["w:sectPr"][7]["w:footerReference"]._attr["w:type"]).to.equal("first");
2019-01-03 02:36:56 +00:00
});
2018-11-02 00:42:49 +00:00
it("should create with correct headers", () => {
const doc = new File();
2019-07-31 08:48:02 +01:00
doc.addSection({
2018-11-02 00:42:49 +00:00
headers: {
2019-07-31 08:48:02 +01:00
default: new Header(),
first: new Header(),
even: new Header(),
2018-11-02 00:42:49 +00:00
},
footers: {
2019-07-31 08:48:02 +01:00
default: new Footer(),
first: new Footer(),
even: new Footer(),
2018-11-02 00:42:49 +00:00
},
2019-07-31 08:48:02 +01:00
children: [],
2018-11-02 00:42:49 +00:00
});
const tree = new Formatter().format(doc.Document.Body);
expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][0]["w:sectPr"][5]["w:headerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][0]["w:sectPr"][6]["w:headerReference"]._attr["w:type"]).to.equal("even");
2018-11-02 00:42:49 +00:00
expect(tree["w:body"][0]["w:sectPr"][7]["w:footerReference"]._attr["w:type"]).to.equal("default");
expect(tree["w:body"][0]["w:sectPr"][8]["w:footerReference"]._attr["w:type"]).to.equal("first");
expect(tree["w:body"][0]["w:sectPr"][9]["w:footerReference"]._attr["w:type"]).to.equal("even");
2018-11-02 00:42:49 +00:00
});
});
2018-12-16 01:56:42 +00:00
2019-07-31 08:48:02 +01:00
describe("#addSection", () => {
2019-06-25 23:17:56 +01:00
it("should call the underlying document's add a Paragraph", () => {
2018-12-16 01:56:42 +00:00
const file = new File();
2019-06-25 23:17:56 +01:00
const spy = sinon.spy(file.Document, "add");
2019-07-31 08:48:02 +01:00
file.addSection({
children: [new Paragraph({})],
});
2018-12-16 01:56:42 +00:00
expect(spy.called).to.equal(true);
});
2019-06-25 23:17:56 +01:00
it("should call the underlying document's add when adding a Table", () => {
2019-07-31 08:48:02 +01:00
const file = new File();
const spy = sinon.spy(file.Document, "add");
file.addSection({
children: [
new Table({
2019-09-13 00:51:20 +01:00
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
2019-07-31 08:48:02 +01:00
}),
],
});
2018-12-16 01:56:42 +00:00
expect(spy.called).to.equal(true);
});
2019-01-03 02:36:56 +00:00
2019-06-25 23:17:56 +01:00
it("should call the underlying document's add when adding an Image (paragraph)", () => {
2019-07-31 08:48:02 +01:00
const file = new File();
const spy = sinon.spy(file.Document, "add");
2019-06-25 23:17:56 +01:00
// tslint:disable-next-line:no-any
2019-07-31 08:48:02 +01:00
file.addSection({
children: [new Paragraph("")],
});
2019-01-03 02:36:56 +00:00
expect(spy.called).to.equal(true);
});
});
2019-07-31 08:48:02 +01:00
describe("#addSection", () => {
it("should call the underlying document's add", () => {
const file = new File();
const spy = sinon.spy(file.Document, "add");
file.addSection({
children: [new TableOfContents()],
});
2019-01-03 02:36:56 +00:00
expect(spy.called).to.equal(true);
});
});
describe("#createFootnote", () => {
it("should call the underlying document's createFootnote", () => {
const wrapper = new File();
const spy = sinon.spy(wrapper.FootNotes, "createFootNote");
wrapper.createFootnote(new Paragraph(""));
expect(spy.called).to.equal(true);
});
});
2018-11-02 00:42:49 +00:00
});