2018-08-14 01:46:48 +01:00
|
|
|
/* tslint:disable:typedef space-before-function-paren */
|
|
|
|
import { expect } from "chai";
|
2019-09-22 20:45:24 +01:00
|
|
|
import * as sinon from "sinon";
|
2018-10-26 01:04:07 +01:00
|
|
|
|
2022-06-26 23:26:42 +01:00
|
|
|
import { File } from "@file/file";
|
|
|
|
import { Footer, Header } from "@file/header";
|
2022-07-12 18:11:44 +01:00
|
|
|
import { ImageRun, Paragraph } from "@file/paragraph";
|
|
|
|
import * as convenienceFunctions from "@util/convenience-functions";
|
2018-10-26 01:04:07 +01:00
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
import { Compiler } from "./next-compiler";
|
|
|
|
|
|
|
|
describe("Compiler", () => {
|
|
|
|
let compiler: Compiler;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
compiler = new Compiler();
|
|
|
|
});
|
|
|
|
|
2022-07-12 18:11:44 +01:00
|
|
|
before(() => {
|
|
|
|
sinon.stub(convenienceFunctions, "uniqueId").callsFake(() => "test");
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
(convenienceFunctions.uniqueId as sinon.SinonStub).restore();
|
|
|
|
});
|
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
describe("#compile()", () => {
|
2020-08-01 17:40:57 +01:00
|
|
|
it("should pack all the content", async function () {
|
2018-08-14 01:46:48 +01:00
|
|
|
this.timeout(99999999);
|
2021-03-19 20:53:56 +00:00
|
|
|
const file = new File({
|
|
|
|
sections: [],
|
2022-06-25 13:08:20 +01:00
|
|
|
comments: {
|
|
|
|
children: [],
|
|
|
|
},
|
2021-03-19 20:53:56 +00:00
|
|
|
});
|
2018-11-12 13:17:53 +00:00
|
|
|
const zipFile = compiler.compile(file);
|
2018-08-14 01:46:48 +01:00
|
|
|
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
|
|
|
|
|
|
|
|
expect(fileNames).is.an.instanceof(Array);
|
2022-06-19 00:31:36 +01:00
|
|
|
expect(fileNames).has.length(17);
|
2018-08-14 01:46:48 +01:00
|
|
|
expect(fileNames).to.include("word/document.xml");
|
|
|
|
expect(fileNames).to.include("word/styles.xml");
|
|
|
|
expect(fileNames).to.include("docProps/core.xml");
|
2020-08-03 14:58:30 +12:00
|
|
|
expect(fileNames).to.include("docProps/custom.xml");
|
2018-08-14 01:46:48 +01:00
|
|
|
expect(fileNames).to.include("docProps/app.xml");
|
|
|
|
expect(fileNames).to.include("word/numbering.xml");
|
|
|
|
expect(fileNames).to.include("word/footnotes.xml");
|
2021-03-01 23:35:52 +00:00
|
|
|
expect(fileNames).to.include("word/_rels/footnotes.xml.rels");
|
2018-09-20 10:11:59 -03:00
|
|
|
expect(fileNames).to.include("word/settings.xml");
|
2022-06-19 00:31:36 +01:00
|
|
|
expect(fileNames).to.include("word/comments.xml");
|
2018-08-14 01:46:48 +01:00
|
|
|
expect(fileNames).to.include("word/_rels/document.xml.rels");
|
|
|
|
expect(fileNames).to.include("[Content_Types].xml");
|
|
|
|
expect(fileNames).to.include("_rels/.rels");
|
|
|
|
});
|
|
|
|
|
2020-08-01 17:40:57 +01:00
|
|
|
it("should pack all additional headers and footers", async function () {
|
2021-03-19 20:53:56 +00:00
|
|
|
const file = new File({
|
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
headers: {
|
2022-06-15 01:44:57 +01:00
|
|
|
default: new Header({
|
|
|
|
children: [new Paragraph("test")],
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
footers: {
|
2022-06-15 01:44:57 +01:00
|
|
|
default: new Footer({
|
|
|
|
children: [new Paragraph("test")],
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
children: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
headers: {
|
2022-06-15 01:44:57 +01:00
|
|
|
default: new Header({
|
|
|
|
children: [new Paragraph("test")],
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
footers: {
|
2022-06-15 01:44:57 +01:00
|
|
|
default: new Footer({
|
|
|
|
children: [new Paragraph("test")],
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
children: [],
|
|
|
|
},
|
|
|
|
],
|
2019-07-31 08:48:02 +01:00
|
|
|
});
|
2018-08-14 01:46:48 +01:00
|
|
|
|
|
|
|
this.timeout(99999999);
|
|
|
|
|
2018-11-12 13:17:53 +00:00
|
|
|
const zipFile = compiler.compile(file);
|
2018-08-14 01:46:48 +01:00
|
|
|
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
|
|
|
|
|
|
|
|
expect(fileNames).is.an.instanceof(Array);
|
2022-06-19 00:31:36 +01:00
|
|
|
expect(fileNames).has.length(25);
|
2018-08-14 01:46:48 +01:00
|
|
|
|
|
|
|
expect(fileNames).to.include("word/header1.xml");
|
|
|
|
expect(fileNames).to.include("word/_rels/header1.xml.rels");
|
|
|
|
expect(fileNames).to.include("word/header2.xml");
|
|
|
|
expect(fileNames).to.include("word/_rels/header2.xml.rels");
|
|
|
|
expect(fileNames).to.include("word/footer1.xml");
|
|
|
|
expect(fileNames).to.include("word/_rels/footer1.xml.rels");
|
|
|
|
expect(fileNames).to.include("word/footer2.xml");
|
|
|
|
expect(fileNames).to.include("word/_rels/footer2.xml.rels");
|
|
|
|
});
|
2019-09-22 20:45:24 +01:00
|
|
|
|
|
|
|
it("should call the format method X times equalling X files to be formatted", () => {
|
|
|
|
// This test is required because before, there was a case where Document was formatted twice, which was inefficient
|
|
|
|
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
|
|
|
|
const paragraph = new Paragraph("");
|
2021-03-19 20:53:56 +00:00
|
|
|
const file = new File({
|
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
properties: {},
|
|
|
|
children: [paragraph],
|
|
|
|
},
|
|
|
|
],
|
2019-09-22 20:45:24 +01:00
|
|
|
});
|
2021-03-19 20:53:56 +00:00
|
|
|
|
2019-09-22 20:45:24 +01:00
|
|
|
// tslint:disable-next-line: no-string-literal
|
|
|
|
const spy = sinon.spy(compiler["formatter"], "format");
|
|
|
|
|
|
|
|
compiler.compile(file);
|
2022-07-12 16:57:25 +01:00
|
|
|
expect(spy.callCount).to.equal(13);
|
2019-09-22 20:45:24 +01:00
|
|
|
});
|
2022-06-15 01:44:57 +01:00
|
|
|
|
|
|
|
it("should work with media datas", () => {
|
|
|
|
// This test is required because before, there was a case where Document was formatted twice, which was inefficient
|
|
|
|
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
|
|
|
|
const file = new File({
|
|
|
|
sections: [
|
|
|
|
{
|
2022-07-12 18:11:44 +01:00
|
|
|
headers: {
|
|
|
|
default: new Header({
|
|
|
|
children: [new Paragraph("test")],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
footers: {
|
|
|
|
default: new Footer({
|
|
|
|
children: [new Paragraph("test")],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new ImageRun({
|
|
|
|
data: Buffer.from("", "base64"),
|
|
|
|
transformation: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
],
|
2022-06-15 01:44:57 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
// tslint:disable-next-line: no-string-literal
|
|
|
|
sinon.stub(compiler["imageReplacer"], "getMediaData").returns([
|
|
|
|
{
|
|
|
|
stream: Buffer.from(""),
|
|
|
|
fileName: "test",
|
|
|
|
transformation: {
|
|
|
|
pixels: {
|
|
|
|
x: 100,
|
|
|
|
y: 100,
|
|
|
|
},
|
|
|
|
emus: {
|
|
|
|
x: 100,
|
|
|
|
y: 100,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
compiler.compile(file);
|
|
|
|
});
|
2018-08-14 01:46:48 +01:00
|
|
|
});
|
|
|
|
});
|