diff --git a/.nycrc b/.nycrc index d79a0ddd65..63dce4516c 100644 --- a/.nycrc +++ b/.nycrc @@ -1,9 +1,9 @@ { "check-coverage": true, - "lines": 98.53, - "functions": 96.59, + "lines": 98.66, + "functions": 97.06, "branches": 95.51, - "statements": 98.51, + "statements": 98.67, "include": [ "src/**/*.ts" ], diff --git a/src/export/packer/image-replacer.spec.ts b/src/export/packer/image-replacer.spec.ts new file mode 100644 index 0000000000..a5f000c2df --- /dev/null +++ b/src/export/packer/image-replacer.spec.ts @@ -0,0 +1,89 @@ +import { expect } from "chai"; + +import { File, HeadingLevel, Media, Paragraph } from "file"; + +import { ImageReplacer } from "./image-replacer"; + +describe("ImageReplacer", () => { + let file: File; + + beforeEach(() => { + file = new File({ + creator: "Dolan Miu", + revision: "1", + lastModifiedBy: "Dolan Miu", + }); + + 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"), + ], + }); + }); + + describe("#replace()", () => { + it("should replace properly", () => { + const imageReplacer = new ImageReplacer(); + const result = imageReplacer.replace( + "test {test-image.png} test", + [ + { + stream: Buffer.from(""), + fileName: "test-image.png", + dimensions: { + pixels: { + x: 100, + y: 100, + }, + emus: { + x: 100, + y: 100, + }, + }, + }, + ], + 0, + ); + + expect(result).to.equal("test 0 test"); + }); + }); + + describe("#getMediaData()", () => { + it("should get media data", () => { + const imageReplacer = new ImageReplacer(); + const result = imageReplacer.getMediaData("test {test-image} test", ({ + Array: [ + { + stream: Buffer.from(""), + fileName: "test-image", + dimensions: { + pixels: { + x: 100, + y: 100, + }, + emus: { + x: 100, + y: 100, + }, + }, + }, + ], + } as unknown) as Media); + + expect(result).to.have.length(1); + }); + }); +}); diff --git a/src/export/packer/packer.spec.ts b/src/export/packer/packer.spec.ts index 18ae459b49..e40c9cb5ea 100644 --- a/src/export/packer/packer.spec.ts +++ b/src/export/packer/packer.spec.ts @@ -1,6 +1,6 @@ /* tslint:disable:typedef space-before-function-paren */ import { assert } from "chai"; -import { stub } from "sinon"; +import { mock, stub } from "sinon"; import { File, HeadingLevel, Paragraph } from "file"; @@ -84,4 +84,32 @@ describe("Packer", () => { (Packer as any).compiler.compile.restore(); }); }); + + 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(); + }); + }); });