Add more tests

This commit is contained in:
Dolan
2021-03-14 00:44:12 +00:00
parent f0e50dd21f
commit 2fa8df7cf4
3 changed files with 121 additions and 4 deletions

6
.nycrc
View File

@ -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"
],

View File

@ -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);
});
});
});

View File

@ -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();
});
});
});