diff --git a/package.json b/package.json index 109fc3fd06..e2d0027352 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "devDependencies": { "@types/chai": "^3.4.35", "@types/mocha": "^2.2.39", + "@types/sinon": "^4.3.1", "awesome-typescript-loader": "^3.4.1", "chai": "^3.5.0", "glob": "^7.1.2", @@ -70,6 +71,7 @@ "replace-in-file": "^3.1.0", "rimraf": "^2.5.2", "shelljs": "^0.7.7", + "sinon": "^5.0.7", "tslint": "^5.1.0", "typedoc": "^0.9.0", "typescript": "2.6.2", diff --git a/src/export/packer/express.spec.ts b/src/export/packer/express.spec.ts new file mode 100644 index 0000000000..416d206815 --- /dev/null +++ b/src/export/packer/express.spec.ts @@ -0,0 +1,43 @@ +// tslint:disable:typedef space-before-function-paren +// tslint:disable:no-empty +// tslint:disable:no-any +import { assert } from "chai"; +import { stub } from "sinon"; + +import { ExpressPacker } from "../../export/packer/express"; +import { File, Paragraph } from "../../file"; + +describe("LocalPacker", () => { + let packer: ExpressPacker; + + beforeEach(() => { + const file = new File({ + creator: "Dolan Miu", + revision: "1", + lastModifiedBy: "Dolan Miu", + }); + const paragraph = new Paragraph("test text"); + const heading = new Paragraph("Hello world").heading1(); + file.addParagraph(new Paragraph("title").title()); + file.addParagraph(heading); + file.addParagraph(new Paragraph("heading 2").heading2()); + file.addParagraph(paragraph); + + const expressResMock = { + on: () => {}, + attachment: () => {}, + }; + + packer = new ExpressPacker(file, expressResMock as any); + }); + + describe("#pack()", () => { + it("should handle exception if it throws any", () => { + const compiler = stub((packer as any).packer, "compile"); + compiler.throwsException(); + return packer.pack("build/tests/test").catch((error) => { + assert.isDefined(error); + }); + }); + }); +}); diff --git a/src/export/packer/local.spec.ts b/src/export/packer/local.spec.ts index 16fb99705b..e942fb70a9 100644 --- a/src/export/packer/local.spec.ts +++ b/src/export/packer/local.spec.ts @@ -1,5 +1,7 @@ /* tslint:disable:typedef space-before-function-paren */ +import { assert } from "chai"; import * as fs from "fs"; +import { stub } from "sinon"; import { LocalPacker } from "../../export/packer/local"; import { File, Paragraph } from "../../file"; @@ -29,14 +31,36 @@ describe("LocalPacker", () => { await packer.pack("build/tests/test"); fs.statSync("build/tests/test.docx"); }); + + it("should handle exception if it throws any", () => { + // tslint:disable-next-line:no-any + const compiler = stub((packer as any).packer, "compile"); + compiler.throwsException(); + return packer.pack("build/tests/test").catch((error) => { + assert.isDefined(error); + }); + }); }); describe("#packPdf", () => { it("should create a standard PDF file", async function() { this.timeout(99999999); + // tslint:disable-next-line:no-any + const pdfConverterConvert = stub((packer as any).pdfConverter, "convert"); + pdfConverterConvert.returns("Test PDF Contents"); + await packer.packPdf("build/tests/pdf-test"); fs.statSync("build/tests/pdf-test.pdf"); }); + + it("should handle exception if it throws any", () => { + // tslint:disable-next-line:no-any + const compiler = stub((packer as any).packer, "compile"); + compiler.throwsException(); + return packer.packPdf("build/tests/pdf-test").catch((error) => { + assert.isDefined(error); + }); + }); }); });