2018-07-01 02:26:23 +01:00
|
|
|
/* tslint:disable:typedef space-before-function-paren */
|
|
|
|
import { assert } from "chai";
|
2021-03-14 00:44:12 +00:00
|
|
|
import { mock, stub } from "sinon";
|
2018-07-01 02:26:23 +01:00
|
|
|
|
2022-06-26 23:26:42 +01:00
|
|
|
import { File } from "@file/file";
|
|
|
|
import { HeadingLevel, Paragraph } from "@file/paragraph";
|
2018-10-26 01:04:07 +01:00
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
import { Packer } from "./packer";
|
2018-07-01 02:26:23 +01:00
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
describe("Packer", () => {
|
|
|
|
let file: File;
|
2018-07-01 02:26:23 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-08-14 01:46:48 +01:00
|
|
|
file = new File({
|
2018-07-01 02:26:23 +01:00
|
|
|
creator: "Dolan Miu",
|
2021-10-08 15:52:12 -06:00
|
|
|
revision: 1,
|
2018-07-01 02:26:23 +01:00
|
|
|
lastModifiedBy: "Dolan Miu",
|
2021-03-19 20:53:56 +00:00
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
text: "title",
|
|
|
|
heading: HeadingLevel.TITLE,
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
text: "Hello world",
|
|
|
|
heading: HeadingLevel.HEADING_1,
|
|
|
|
}),
|
|
|
|
new Paragraph({
|
|
|
|
text: "heading 2",
|
|
|
|
heading: HeadingLevel.HEADING_2,
|
|
|
|
}),
|
|
|
|
new Paragraph("test text"),
|
|
|
|
],
|
|
|
|
},
|
2019-07-31 08:48:02 +01:00
|
|
|
],
|
|
|
|
});
|
2018-07-01 02:26:23 +01:00
|
|
|
});
|
|
|
|
|
2022-08-22 19:08:29 +02:00
|
|
|
describe("#toString()", () => {
|
|
|
|
it("should return a non-empty string", async () => {
|
|
|
|
const result = await Packer.toString(file);
|
|
|
|
|
|
|
|
assert.isAbove(result.length, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
describe("#toBuffer()", () => {
|
2020-08-01 17:40:57 +01:00
|
|
|
it("should create a standard docx file", async function () {
|
2018-07-01 02:26:23 +01:00
|
|
|
this.timeout(99999999);
|
2019-08-07 22:12:14 +01:00
|
|
|
const buffer = await Packer.toBuffer(file);
|
2018-08-14 01:46:48 +01:00
|
|
|
|
2018-07-01 02:26:23 +01:00
|
|
|
assert.isDefined(buffer);
|
|
|
|
assert.isTrue(buffer.byteLength > 0);
|
|
|
|
});
|
|
|
|
|
2022-09-19 20:48:50 +01:00
|
|
|
it("should handle exception if it throws any", () => {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-08-07 22:12:14 +01:00
|
|
|
const compiler = stub((Packer as any).compiler, "compile");
|
2018-08-14 01:46:48 +01:00
|
|
|
|
2018-07-01 02:26:23 +01:00
|
|
|
compiler.throwsException();
|
2019-08-07 22:12:14 +01:00
|
|
|
return Packer.toBuffer(file).catch((error) => {
|
2018-07-01 02:26:23 +01:00
|
|
|
assert.isDefined(error);
|
|
|
|
});
|
|
|
|
});
|
2019-08-07 22:27:44 +01:00
|
|
|
|
|
|
|
after(() => {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-08-07 22:27:44 +01:00
|
|
|
(Packer as any).compiler.compile.restore();
|
|
|
|
});
|
2018-07-01 02:26:23 +01:00
|
|
|
});
|
2018-11-12 13:03:17 +00:00
|
|
|
|
|
|
|
describe("#toBase64String()", () => {
|
2020-08-01 17:40:57 +01:00
|
|
|
it("should create a standard docx file", async function () {
|
2018-11-12 13:03:17 +00:00
|
|
|
this.timeout(99999999);
|
2019-08-07 22:12:14 +01:00
|
|
|
const str = await Packer.toBase64String(file);
|
2018-11-12 13:03:17 +00:00
|
|
|
|
|
|
|
assert.isDefined(str);
|
|
|
|
assert.isTrue(str.length > 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle exception if it throws any", () => {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-08-07 22:12:14 +01:00
|
|
|
const compiler = stub((Packer as any).compiler, "compile");
|
2018-11-12 13:03:17 +00:00
|
|
|
|
|
|
|
compiler.throwsException();
|
2019-08-07 22:12:14 +01:00
|
|
|
return Packer.toBase64String(file).catch((error) => {
|
2018-11-12 13:03:17 +00:00
|
|
|
assert.isDefined(error);
|
|
|
|
});
|
|
|
|
});
|
2019-08-07 22:27:44 +01:00
|
|
|
|
|
|
|
after(() => {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-08-07 22:27:44 +01:00
|
|
|
(Packer as any).compiler.compile.restore();
|
|
|
|
});
|
2018-11-12 13:03:17 +00:00
|
|
|
});
|
2021-03-14 00:44:12 +00:00
|
|
|
|
|
|
|
describe("#toBlob()", () => {
|
|
|
|
it("should create a standard docx file", async () => {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-03-14 00:44:12 +00:00
|
|
|
stub((Packer as any).compiler, "compile").callsFake(() => ({
|
|
|
|
// tslint:disable-next-line: no-empty
|
|
|
|
generateAsync: () => mock({}),
|
|
|
|
}));
|
|
|
|
const str = await Packer.toBlob(file);
|
|
|
|
|
|
|
|
assert.isDefined(str);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle exception if it throws any", () => {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-03-14 00:44:12 +00:00
|
|
|
const compiler = stub((Packer as any).compiler, "compile");
|
|
|
|
|
|
|
|
compiler.throwsException();
|
|
|
|
return Packer.toBlob(file).catch((error) => {
|
|
|
|
assert.isDefined(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2021-03-14 00:44:12 +00:00
|
|
|
(Packer as any).compiler.compile.restore();
|
|
|
|
});
|
|
|
|
});
|
2022-09-15 08:15:38 +01:00
|
|
|
|
|
|
|
describe("#toStream()", () => {
|
|
|
|
it("should create a standard docx file", async () => {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-09-15 08:15:38 +01:00
|
|
|
stub((Packer as any).compiler, "compile").callsFake(() => ({
|
|
|
|
// tslint:disable-next-line: no-empty
|
|
|
|
generateNodeStream: () => ({
|
|
|
|
on: (event: string, cb: () => void) => {
|
|
|
|
if (event === "end") {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
const stream = await Packer.toStream(file);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
stream.on("error", () => {
|
2022-09-19 20:48:50 +01:00
|
|
|
reject(new Error());
|
2022-09-15 08:15:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
stream.on("end", () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-19 20:48:50 +01:00
|
|
|
it("should handle exception if it throws any", () => {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-09-15 08:15:38 +01:00
|
|
|
const compiler = stub((Packer as any).compiler, "compile").callsFake(() => ({
|
|
|
|
// tslint:disable-next-line: no-empty
|
|
|
|
on: (event: string, cb: () => void) => {
|
|
|
|
if (event === "error") {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
compiler.throwsException();
|
2022-09-19 20:48:50 +01:00
|
|
|
try {
|
|
|
|
Packer.toStream(file);
|
|
|
|
} catch (error) {
|
2022-09-15 08:15:38 +01:00
|
|
|
assert.isDefined(error);
|
2022-09-19 20:48:50 +01:00
|
|
|
}
|
2022-09-15 08:15:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-09-15 08:15:38 +01:00
|
|
|
(Packer as any).compiler.compile.restore();
|
|
|
|
});
|
|
|
|
});
|
2018-07-01 02:26:23 +01:00
|
|
|
});
|