Files
docx-js/src/patcher/replacer.spec.ts

205 lines
7.6 KiB
TypeScript
Raw Normal View History

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";
import { describe, expect, it, vi } from "vitest";
2023-03-15 02:46:39 +00:00
import { PatchType } from "./from-docx";
import { replacer } from "./replacer";
export const MOCK_JSON = {
2023-03-15 02:46:39 +00:00
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}} dont you think?" }] },
],
},
],
},
{
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 throw an error if nothing is added", () => {
expect(() =>
replacer({
json: {
elements: [],
},
patch: {
type: PatchType.PARAGRAPH,
children: [],
},
patchText: "hello",
// eslint-disable-next-line functional/prefer-readonly-type
context: vi.fn<[], IContext>()(),
}),
).toThrow();
2023-03-15 02:46:39 +00:00
});
2023-03-16 01:55:18 +00:00
it("should replace paragraph type", () => {
const output = replacer({
json: JSON.parse(JSON.stringify(MOCK_JSON)),
patch: {
2023-03-15 02:46:39 +00:00
type: PatchType.PARAGRAPH,
children: [new TextRun("Delightful Header")],
},
patchText: "{{header_adjective}}",
context: {
2023-03-15 02:46:39 +00:00
file: {} as unknown as File,
viewWrapper: {
Relationships: {},
} as unknown as IViewWrapper,
stack: [],
},
});
2023-03-15 02:46:39 +00:00
expect(JSON.stringify(output)).to.contain("Delightful Header");
});
2023-03-16 01:55:18 +00:00
it("should replace paragraph type keeping original styling if keepOriginalStyles is true", () => {
const output = replacer({
json: JSON.parse(JSON.stringify(MOCK_JSON)),
patch: {
type: PatchType.PARAGRAPH,
children: [new TextRun("sweet")],
},
patchText: "{{bold}}",
context: {
file: {} as unknown as File,
viewWrapper: {
Relationships: {},
} as unknown as IViewWrapper,
stack: [],
},
keepOriginalStyles: 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({
json: JSON.parse(JSON.stringify(MOCK_JSON)),
patch: {
2023-03-16 01:55:18 +00:00
type: PatchType.DOCUMENT,
children: [new Paragraph("Lorem ipsum paragraph")],
},
patchText: "{{header_adjective}}",
context: {
2023-03-16 01:55:18 +00:00
file: {} as unknown as File,
viewWrapper: {
Relationships: {},
} as unknown as IViewWrapper,
stack: [],
},
});
2023-03-16 01:55:18 +00:00
expect(JSON.stringify(output)).to.contain("Lorem ipsum paragraph");
});
2023-03-15 02:46:39 +00:00
});
});