2023-03-15 02:46:39 +00:00
|
|
|
|
import { IViewWrapper } from "@file/document-wrapper";
|
|
|
|
|
import { File } from "@file/file";
|
2023-03-16 01:55:18 +00:00
|
|
|
|
import { Paragraph, TextRun } from "@file/paragraph";
|
2023-03-15 02:46:39 +00:00
|
|
|
|
import { IContext } from "@file/xml-components";
|
2023-06-08 13:30:31 +01:00
|
|
|
|
import { describe, expect, it, vi } from "vitest";
|
2023-03-15 02:46:39 +00:00
|
|
|
|
|
|
|
|
|
import { PatchType } from "./from-docx";
|
|
|
|
|
|
|
|
|
|
import { replacer } from "./replacer";
|
|
|
|
|
|
|
|
|
|
const MOCK_JSON = {
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:hdr",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:p",
|
|
|
|
|
attributes: { "w14:paraId": "3BE1A671", "w14:textId": "74E856C4", "w:rsidR": "000D38A7", "w:rsidRDefault": "000D38A7" },
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:pPr",
|
|
|
|
|
elements: [{ type: "element", name: "w:pStyle", attributes: { "w:val": "Header" } }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [{ type: "element", name: "w:t", elements: [{ type: "text", text: "This is a {{head" }] }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
attributes: { "w:rsidR": "004A3A99" },
|
|
|
|
|
elements: [{ type: "element", name: "w:t", elements: [{ type: "text", text: "er" }] }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "element", name: "w:t", elements: [{ type: "text", text: "_adjective}} don’t you think?" }] },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2023-10-18 12:58:01 -03:00
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:p",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "What a {{bold}} text!" }],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2023-03-15 02:46:39 +00:00
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
describe("replacer", () => {
|
|
|
|
|
describe("replacer", () => {
|
|
|
|
|
it("should return the same object if nothing is added", () => {
|
|
|
|
|
const output = replacer(
|
|
|
|
|
{
|
|
|
|
|
elements: [],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: PatchType.PARAGRAPH,
|
|
|
|
|
children: [],
|
|
|
|
|
},
|
|
|
|
|
"hello",
|
|
|
|
|
[],
|
2023-06-08 13:30:31 +01:00
|
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
|
|
|
|
vi.fn<[], IContext>()(),
|
2023-03-15 02:46:39 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(output).to.deep.equal({
|
|
|
|
|
elements: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-16 01:55:18 +00:00
|
|
|
|
it("should replace paragraph type", () => {
|
2023-03-15 02:46:39 +00:00
|
|
|
|
const output = replacer(
|
|
|
|
|
MOCK_JSON,
|
|
|
|
|
{
|
|
|
|
|
type: PatchType.PARAGRAPH,
|
|
|
|
|
children: [new TextRun("Delightful Header")],
|
|
|
|
|
},
|
|
|
|
|
"{{header_adjective}}",
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
text: "This is a {{header_adjective}} don’t you think?",
|
|
|
|
|
runs: [
|
|
|
|
|
{
|
|
|
|
|
text: "This is a {{head",
|
|
|
|
|
parts: [{ text: "This is a {{head", index: 0, start: 0, end: 15 }],
|
|
|
|
|
index: 1,
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 15,
|
|
|
|
|
},
|
|
|
|
|
{ text: "er", parts: [{ text: "er", index: 0, start: 16, end: 17 }], index: 2, start: 16, end: 17 },
|
|
|
|
|
{
|
|
|
|
|
text: "_adjective}} don’t you think?",
|
|
|
|
|
parts: [{ text: "_adjective}} don’t you think?", index: 0, start: 18, end: 46 }],
|
|
|
|
|
index: 3,
|
|
|
|
|
start: 18,
|
|
|
|
|
end: 46,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
index: 0,
|
|
|
|
|
path: [0, 0, 0],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
{
|
|
|
|
|
file: {} as unknown as File,
|
|
|
|
|
viewWrapper: {
|
|
|
|
|
Relationships: {},
|
|
|
|
|
} as unknown as IViewWrapper,
|
|
|
|
|
stack: [],
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(JSON.stringify(output)).to.contain("Delightful Header");
|
|
|
|
|
});
|
2023-03-16 01:55:18 +00:00
|
|
|
|
|
2023-10-18 12:58:01 -03:00
|
|
|
|
it("should replace paragraph type keeping original styling if keepOriginalStyles is true", () => {
|
|
|
|
|
const output = replacer(
|
|
|
|
|
MOCK_JSON,
|
|
|
|
|
{
|
|
|
|
|
type: PatchType.PARAGRAPH,
|
|
|
|
|
children: [new TextRun("sweet")],
|
|
|
|
|
},
|
|
|
|
|
"{{bold}}",
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
text: "What a {{bold}} text!",
|
|
|
|
|
runs: [
|
|
|
|
|
{
|
|
|
|
|
text: "What a {{bold}} text!",
|
|
|
|
|
parts: [{ text: "What a {{bold}} text!", index: 1, start: 0, end: 21 }],
|
|
|
|
|
index: 0,
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 21,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
index: 0,
|
|
|
|
|
path: [0, 0, 1],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
{
|
|
|
|
|
file: {} as unknown as File,
|
|
|
|
|
viewWrapper: {
|
|
|
|
|
Relationships: {},
|
|
|
|
|
} as unknown as IViewWrapper,
|
|
|
|
|
stack: [],
|
|
|
|
|
},
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(JSON.stringify(output)).to.contain("sweet");
|
|
|
|
|
expect(output.elements![0].elements![1].elements).toMatchObject([
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "What a " }],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "sweet" }],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: " text!" }],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-16 01:55:18 +00:00
|
|
|
|
it("should replace document type", () => {
|
|
|
|
|
const output = replacer(
|
|
|
|
|
MOCK_JSON,
|
|
|
|
|
{
|
|
|
|
|
type: PatchType.DOCUMENT,
|
|
|
|
|
children: [new Paragraph("Lorem ipsum paragraph")],
|
|
|
|
|
},
|
|
|
|
|
"{{header_adjective}}",
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
text: "This is a {{header_adjective}} don’t you think?",
|
|
|
|
|
runs: [
|
|
|
|
|
{
|
|
|
|
|
text: "This is a {{head",
|
|
|
|
|
parts: [{ text: "This is a {{head", index: 0, start: 0, end: 15 }],
|
|
|
|
|
index: 1,
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 15,
|
|
|
|
|
},
|
|
|
|
|
{ text: "er", parts: [{ text: "er", index: 0, start: 16, end: 17 }], index: 2, start: 16, end: 17 },
|
|
|
|
|
{
|
|
|
|
|
text: "_adjective}} don’t you think?",
|
|
|
|
|
parts: [{ text: "_adjective}} don’t you think?", index: 0, start: 18, end: 46 }],
|
|
|
|
|
index: 3,
|
|
|
|
|
start: 18,
|
|
|
|
|
end: 46,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
index: 0,
|
|
|
|
|
path: [0, 0, 0],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
{
|
|
|
|
|
file: {} as unknown as File,
|
|
|
|
|
viewWrapper: {
|
|
|
|
|
Relationships: {},
|
|
|
|
|
} as unknown as IViewWrapper,
|
|
|
|
|
stack: [],
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(JSON.stringify(output)).to.contain("Lorem ipsum paragraph");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should throw an error if the type is not supported", () => {
|
|
|
|
|
expect(() =>
|
|
|
|
|
replacer(
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
type: PatchType.DOCUMENT,
|
|
|
|
|
children: [new Paragraph("Lorem ipsum paragraph")],
|
|
|
|
|
},
|
|
|
|
|
"{{header_adjective}}",
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
text: "This is a {{header_adjective}} don’t you think?",
|
|
|
|
|
runs: [
|
|
|
|
|
{
|
|
|
|
|
text: "This is a {{head",
|
|
|
|
|
parts: [{ text: "This is a {{head", index: 0, start: 0, end: 15 }],
|
|
|
|
|
index: 1,
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 15,
|
|
|
|
|
},
|
|
|
|
|
{ text: "er", parts: [{ text: "er", index: 0, start: 16, end: 17 }], index: 2, start: 16, end: 17 },
|
|
|
|
|
{
|
|
|
|
|
text: "_adjective}} don’t you think?",
|
|
|
|
|
parts: [{ text: "_adjective}} don’t you think?", index: 0, start: 18, end: 46 }],
|
|
|
|
|
index: 3,
|
|
|
|
|
start: 18,
|
|
|
|
|
end: 46,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
index: 0,
|
|
|
|
|
path: [0, 0, 0],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
{
|
|
|
|
|
file: {} as unknown as File,
|
|
|
|
|
viewWrapper: {
|
|
|
|
|
Relationships: {},
|
|
|
|
|
} as unknown as IViewWrapper,
|
|
|
|
|
stack: [],
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
).to.throw();
|
|
|
|
|
});
|
2023-03-15 02:46:39 +00:00
|
|
|
|
});
|
|
|
|
|
});
|