diff --git a/src/patcher/from-docx.spec.ts b/src/patcher/from-docx.spec.ts index 1e82f86059..147bdd29d0 100644 --- a/src/patcher/from-docx.spec.ts +++ b/src/patcher/from-docx.spec.ts @@ -2,10 +2,203 @@ import { expect } from "chai"; import * as sinon from "sinon"; import * as JSZip from "jszip"; -import { TextRun } from "@file/paragraph"; +import { ExternalHyperlink, ImageRun, Paragraph, TextRun } from "@file/paragraph"; import { patchDocument, PatchType } from "./from-docx"; +const MOCK_XML = ` + + + + + + + + + Hello World + + + + + + Hello {{name}}, + + + how are you? + + + + + + {{paragraph_replace}} + + + + + + {{table}} + + + + + + + + + + + + + + + + + + + + + + {{table_heading_1}} + + + + + + + + + + + + + + + + + + + + + + + + Item: {{item_1}} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {{image_test}} + + + + + + Thank you + + + + + + + + + + + + +`; + describe("from-docx", () => { describe("patchDocument", () => { before(() => { @@ -16,7 +209,8 @@ describe("from-docx", () => { new Promise((resolve) => { const zip = new JSZip(); - zip.file("word/document.xml", ``); + zip.file("word/document.xml", MOCK_XML); + zip.file("[Content_Types].xml", ``); resolve(zip); }), ); @@ -33,6 +227,49 @@ describe("from-docx", () => { type: PatchType.PARAGRAPH, children: [new TextRun("Sir. "), new TextRun("John Doe"), new TextRun("(The Conqueror)")], }, + item_1: { + type: PatchType.PARAGRAPH, + children: [ + new TextRun("#657"), + new ExternalHyperlink({ + children: [ + new TextRun({ + text: "BBC News Link", + }), + ], + link: "https://www.bbc.co.uk/news", + }), + ], + }, + // eslint-disable-next-line @typescript-eslint/naming-convention + paragraph_replace: { + type: PatchType.DOCUMENT, + children: [ + new Paragraph({ + children: [ + new TextRun("This is a "), + new ExternalHyperlink({ + children: [ + new TextRun({ + text: "Google Link", + }), + ], + link: "https://www.google.co.uk", + }), + ], + }), + ], + }, + // eslint-disable-next-line @typescript-eslint/naming-convention + image_test: { + type: PatchType.PARAGRAPH, + children: [ + new ImageRun({ + data: Buffer.from(""), + transformation: { width: 100, height: 100 }, + }), + ], + }, }, }); expect(output).to.not.be.undefined; diff --git a/src/patcher/from-docx.ts b/src/patcher/from-docx.ts index c464ce6031..3d27bd402d 100644 --- a/src/patcher/from-docx.ts +++ b/src/patcher/from-docx.ts @@ -169,15 +169,8 @@ export const patchDocument = async (data: InputDataType, options: PatchDocumentO // eslint-disable-next-line functional/immutable-data const relationshipKey = `word/_rels/${key.split("/").pop()}.rels`; - if (!map.has(relationshipKey)) { - map.set(relationshipKey, createRelationshipFile()); - } - - const relationshipsJson = map.get(relationshipKey); - - if (!relationshipsJson) { - throw new Error("Could not find relationships file"); - } + const relationshipsJson = map.get(relationshipKey) ?? createRelationshipFile(); + map.set(relationshipKey, relationshipsJson); appendRelationship( relationshipsJson,