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

78 lines
2.3 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";
import { stub } from "sinon";
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",
});
2019-07-31 08:48:02 +01:00
file.addSection({
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"),
],
});
2018-07-01 02:26:23 +01:00
});
describe("#toBuffer()", () => {
2018-07-01 02:26:23 +01:00
it("should create a standard docx file", async function() {
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);
});
});
});
2018-11-12 13:03:17 +00:00
describe("#toBase64String()", () => {
it("should create a standard docx file", async function() {
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);
});
});
});
2018-07-01 02:26:23 +01:00
});