Files
docx-js/src/export/packer/packer.spec.ts

117 lines
3.6 KiB
TypeScript
Raw Normal View History

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
2019-06-12 01:03:36 +01:00
import { File, HeadingLevel, Paragraph } from "file";
2018-10-26 01:04:07 +01:00
import { Packer } from "./packer";
2018-07-01 02:26:23 +01:00
describe("Packer", () => {
let file: File;
2018-07-01 02:26:23 +01:00
beforeEach(() => {
file = new File({
2018-07-01 02:26:23 +01:00
creator: "Dolan Miu",
revision: "1",
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
});
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-07-01 02:26:23 +01:00
assert.isDefined(buffer);
assert.isTrue(buffer.byteLength > 0);
});
it("should handle exception if it throws any", () => {
// tslint:disable-next-line:no-any
2019-08-07 22:12:14 +01:00
const compiler = stub((Packer as any).compiler, "compile");
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(() => {
// tslint:disable-next-line:no-any
(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", () => {
// tslint:disable-next-line:no-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(() => {
// tslint:disable-next-line:no-any
(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 () => {
// tslint:disable-next-line: no-any
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", () => {
// tslint:disable-next-line:no-any
const compiler = stub((Packer as any).compiler, "compile");
compiler.throwsException();
return Packer.toBlob(file).catch((error) => {
assert.isDefined(error);
});
});
afterEach(() => {
// tslint:disable-next-line:no-any
(Packer as any).compiler.compile.restore();
});
});
2018-07-01 02:26:23 +01:00
});