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

81 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 packer: 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
packer = new Packer();
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);
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
const compiler = stub((packer as any).compiler, "compile");
2018-07-01 02:26:23 +01:00
compiler.throwsException();
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);
const str = await packer.toBase64String(file);
assert.isDefined(str);
assert.isTrue(str.length > 0);
});
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.toBase64String(file).catch((error) => {
assert.isDefined(error);
});
});
});
2018-07-01 02:26:23 +01:00
});