import { expect } from "chai";
import * as sinon from "sinon";
import * as JSZip from "jszip";
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(() => {
sinon.createStubInstance(JSZip, {});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sinon.stub(JSZip, "loadAsync").callsFake(
() =>
new Promise((resolve) => {
const zip = new JSZip();
zip.file("word/document.xml", MOCK_XML);
zip.file("[Content_Types].xml", ``);
resolve(zip);
}),
);
});
after(() => {
(JSZip.loadAsync as unknown as sinon.SinonStub).restore();
});
it("should find the index of a run element with a token", async () => {
const output = await patchDocument(Buffer.from(""), {
patches: {
name: {
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;
});
});
});