Feature/multiple patch document exports (#2497)
* Turn patch document into options object Add outputType to options * Set keep styles to true by default * Simplify method * Rename variable * #2267 Multiple patches of same key * Remove path which won't be visited
This commit is contained in:
@ -8,7 +8,7 @@ import { PatchType } from "./from-docx";
|
||||
|
||||
import { replacer } from "./replacer";
|
||||
|
||||
const MOCK_JSON = {
|
||||
export const MOCK_JSON = {
|
||||
elements: [
|
||||
{
|
||||
type: "element",
|
||||
@ -73,103 +73,60 @@ const MOCK_JSON = {
|
||||
|
||||
describe("replacer", () => {
|
||||
describe("replacer", () => {
|
||||
it("should return the same object if nothing is added", () => {
|
||||
const output = replacer(
|
||||
{
|
||||
elements: [],
|
||||
},
|
||||
{
|
||||
type: PatchType.PARAGRAPH,
|
||||
children: [],
|
||||
},
|
||||
"hello",
|
||||
[],
|
||||
// eslint-disable-next-line functional/prefer-readonly-type
|
||||
vi.fn<[], IContext>()(),
|
||||
);
|
||||
|
||||
expect(output).to.deep.equal({
|
||||
elements: [],
|
||||
});
|
||||
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();
|
||||
});
|
||||
|
||||
it("should replace paragraph type", () => {
|
||||
const output = replacer(
|
||||
MOCK_JSON,
|
||||
{
|
||||
const output = replacer({
|
||||
json: JSON.parse(JSON.stringify(MOCK_JSON)),
|
||||
patch: {
|
||||
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],
|
||||
},
|
||||
],
|
||||
{
|
||||
patchText: "{{header_adjective}}",
|
||||
context: {
|
||||
file: {} as unknown as File,
|
||||
viewWrapper: {
|
||||
Relationships: {},
|
||||
} as unknown as IViewWrapper,
|
||||
stack: [],
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
expect(JSON.stringify(output)).to.contain("Delightful Header");
|
||||
});
|
||||
|
||||
it("should replace paragraph type keeping original styling if keepOriginalStyles is true", () => {
|
||||
const output = replacer(
|
||||
MOCK_JSON,
|
||||
{
|
||||
const output = replacer({
|
||||
json: JSON.parse(JSON.stringify(MOCK_JSON)),
|
||||
patch: {
|
||||
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],
|
||||
},
|
||||
],
|
||||
{
|
||||
patchText: "{{bold}}",
|
||||
context: {
|
||||
file: {} as unknown as File,
|
||||
viewWrapper: {
|
||||
Relationships: {},
|
||||
} as unknown as IViewWrapper,
|
||||
stack: [],
|
||||
},
|
||||
true,
|
||||
);
|
||||
keepOriginalStyles: true,
|
||||
});
|
||||
|
||||
expect(JSON.stringify(output)).to.contain("sweet");
|
||||
expect(output.elements![0].elements![1].elements).toMatchObject([
|
||||
@ -225,91 +182,23 @@ describe("replacer", () => {
|
||||
});
|
||||
|
||||
it("should replace document type", () => {
|
||||
const output = replacer(
|
||||
MOCK_JSON,
|
||||
{
|
||||
const output = replacer({
|
||||
json: JSON.parse(JSON.stringify(MOCK_JSON)),
|
||||
patch: {
|
||||
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],
|
||||
},
|
||||
],
|
||||
{
|
||||
patchText: "{{header_adjective}}",
|
||||
context: {
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user