Add some tests to boost coverage

This commit is contained in:
Dolan Miu
2023-03-21 04:30:20 +00:00
parent 11c3886659
commit 7785f0df02
2 changed files with 54 additions and 11 deletions

View File

@ -206,8 +206,7 @@ const MOCK_XML = `
describe("from-docx", () => { describe("from-docx", () => {
describe("patchDocument", () => { describe("patchDocument", () => {
describe("document.xml and [Content_Types].xml", () => { describe("document.xml and [Content_Types].xml", () => {
before(() => { beforeEach(() => {
sinon.createStubInstance(JSZip, {});
sinon.stub(JSZip, "loadAsync").callsFake( sinon.stub(JSZip, "loadAsync").callsFake(
() => () =>
new Promise<JSZip>((resolve) => { new Promise<JSZip>((resolve) => {
@ -220,7 +219,7 @@ describe("from-docx", () => {
); );
}); });
after(() => { afterEach(() => {
(JSZip.loadAsync as unknown as sinon.SinonStub).restore(); (JSZip.loadAsync as unknown as sinon.SinonStub).restore();
}); });
@ -292,8 +291,7 @@ describe("from-docx", () => {
}); });
describe("document.xml and [Content_Types].xml with relationships", () => { describe("document.xml and [Content_Types].xml with relationships", () => {
before(() => { beforeEach(() => {
sinon.createStubInstance(JSZip, {});
sinon.stub(JSZip, "loadAsync").callsFake( sinon.stub(JSZip, "loadAsync").callsFake(
() => () =>
new Promise<JSZip>((resolve) => { new Promise<JSZip>((resolve) => {
@ -307,7 +305,7 @@ describe("from-docx", () => {
); );
}); });
after(() => { afterEach(() => {
(JSZip.loadAsync as unknown as sinon.SinonStub).restore(); (JSZip.loadAsync as unknown as sinon.SinonStub).restore();
}); });
@ -322,6 +320,14 @@ describe("from-docx", () => {
data: Buffer.from(""), data: Buffer.from(""),
transformation: { width: 100, height: 100 }, transformation: { width: 100, height: 100 },
}), }),
new ExternalHyperlink({
children: [
new TextRun({
text: "Google Link",
}),
],
link: "https://www.google.co.uk",
}),
], ],
}, },
}, },
@ -331,8 +337,7 @@ describe("from-docx", () => {
}); });
describe("document.xml", () => { describe("document.xml", () => {
before(() => { beforeEach(() => {
sinon.createStubInstance(JSZip, {});
sinon.stub(JSZip, "loadAsync").callsFake( sinon.stub(JSZip, "loadAsync").callsFake(
() => () =>
new Promise<JSZip>((resolve) => { new Promise<JSZip>((resolve) => {
@ -344,7 +349,45 @@ describe("from-docx", () => {
); );
}); });
after(() => { afterEach(() => {
(JSZip.loadAsync as unknown as sinon.SinonStub).restore();
});
it("should throw an error if the content types is not found", () =>
expect(
patchDocument(Buffer.from(""), {
patches: {
// eslint-disable-next-line @typescript-eslint/naming-convention
image_test: {
type: PatchType.PARAGRAPH,
children: [
new ImageRun({
data: Buffer.from(""),
transformation: { width: 100, height: 100 },
}),
],
},
},
}),
).to.eventually.be.rejected);
});
describe("Images", () => {
beforeEach(() => {
sinon.stub(JSZip, "loadAsync").callsFake(
() =>
new Promise<JSZip>((resolve) => {
const zip = new JSZip();
zip.file("word/document.xml", MOCK_XML);
zip.file("word/document.bmp", "");
resolve(zip);
}),
);
});
afterEach(() => {
(JSZip.loadAsync as unknown as sinon.SinonStub).restore(); (JSZip.loadAsync as unknown as sinon.SinonStub).restore();
}); });

View File

@ -71,8 +71,8 @@ export const patchDocument = async (data: InputDataType, options: PatchDocumentO
const binaryContentMap = new Map<string, Buffer>(); const binaryContentMap = new Map<string, Buffer>();
for (const [key, value] of Object.entries(zipContent.files)) { for (const [key, value] of Object.entries(zipContent.files)) {
if (!key.endsWith('.xml') && !key.endsWith('.rels')) { if (!key.endsWith(".xml") && !key.endsWith(".rels")) {
binaryContentMap.set(key, await value.async("nodebuffer")) binaryContentMap.set(key, await value.async("nodebuffer"));
continue; continue;
} }