2018-07-01 02:26:23 +01:00
|
|
|
/* tslint:disable:typedef space-before-function-paren */
|
|
|
|
import { assert } from "chai";
|
|
|
|
import { stub } from "sinon";
|
|
|
|
|
|
|
|
import { File, Paragraph } from "../../file";
|
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 packer: 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",
|
|
|
|
revision: "1",
|
|
|
|
lastModifiedBy: "Dolan Miu",
|
|
|
|
});
|
|
|
|
const paragraph = new Paragraph("test text");
|
|
|
|
const heading = new Paragraph("Hello world").heading1();
|
2018-08-14 01:46:48 +01:00
|
|
|
|
2018-07-01 02:26:23 +01:00
|
|
|
file.addParagraph(new Paragraph("title").title());
|
|
|
|
file.addParagraph(heading);
|
|
|
|
file.addParagraph(new Paragraph("heading 2").heading2());
|
|
|
|
file.addParagraph(paragraph);
|
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
packer = new Packer();
|
2018-07-01 02:26:23 +01:00
|
|
|
});
|
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
describe("#toBuffer()", () => {
|
2018-07-01 02:26:23 +01:00
|
|
|
it("should create a standard docx file", async function() {
|
|
|
|
this.timeout(99999999);
|
2018-08-14 01:46:48 +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
|
2018-08-14 01:46:48 +01:00
|
|
|
const compiler = stub((packer as any).compiler, "compile");
|
|
|
|
|
2018-07-01 02:26:23 +01:00
|
|
|
compiler.throwsException();
|
2018-08-14 01:46:48 +01:00
|
|
|
return packer.toBuffer(file).catch((error) => {
|
2018-07-01 02:26:23 +01:00
|
|
|
assert.isDefined(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|