Files
docx-js/src/patcher/paragraph-split-inject.spec.ts

278 lines
9.0 KiB
TypeScript
Raw Normal View History

2023-03-15 02:46:39 +00:00
import { expect } from "chai";
import { findRunElementIndexWithToken, splitRunElement } from "./paragraph-split-inject";
describe("paragraph-split-inject", () => {
describe("findRunElementIndexWithToken", () => {
it("should find the index of a run element with a token", () => {
const output = findRunElementIndexWithToken(
{
name: "w:p",
type: "element",
elements: [
{
name: "w:r",
type: "element",
elements: [
{
name: "w:t",
type: "element",
elements: [
{
type: "text",
text: "hello world",
},
],
},
],
},
],
},
"hello",
);
expect(output).to.deep.equal(0);
});
2023-03-16 01:55:18 +00:00
it("should throw an exception when ran with empty elements", () => {
expect(() =>
findRunElementIndexWithToken(
{
name: "w:p",
type: "element",
},
"hello",
),
).to.throw();
});
it("should throw an exception when ran with empty elements", () => {
expect(() =>
findRunElementIndexWithToken(
{
name: "w:p",
type: "element",
elements: [
{
name: "w:r",
type: "element",
},
],
},
"hello",
),
).to.throw();
});
it("should throw an exception when ran with empty elements", () => {
expect(() =>
findRunElementIndexWithToken(
{
name: "w:p",
type: "element",
elements: [
{
name: "w:r",
type: "element",
elements: [
{
name: "w:t",
type: "element",
},
],
},
],
},
"hello",
),
).to.throw();
});
2023-03-31 17:16:05 +01:00
it("should continue if text run doesn't have text", () => {
2023-04-04 22:21:01 +01:00
expect(() =>
findRunElementIndexWithToken(
{
name: "w:p",
type: "element",
elements: [
{
name: "w:r",
type: "element",
elements: [
{
name: "w:t",
type: "element",
},
],
},
],
},
"hello",
),
).to.throw();
});
it("should continue if text run doesn't have text", () => {
expect(() =>
findRunElementIndexWithToken(
{
name: "w:p",
type: "element",
elements: [
{
name: "w:r",
type: "element",
elements: [
{
name: "w:t",
type: "element",
elements: [
{
type: "text",
},
],
},
],
},
],
},
"hello",
),
).to.throw();
2023-03-31 17:16:05 +01:00
});
2023-03-15 02:46:39 +00:00
});
describe("splitRunElement", () => {
it("should split a run element", () => {
const output = splitRunElement(
{
name: "w:r",
type: "element",
elements: [
{
name: "w:t",
type: "element",
elements: [
{
type: "text",
text: "hello*world",
},
],
},
2023-03-16 01:55:18 +00:00
{
name: "w:x",
type: "element",
},
2023-03-15 02:46:39 +00:00
],
},
"*",
);
expect(output).to.deep.equal({
left: {
elements: [
{
attributes: {
"xml:space": "preserve",
},
elements: [
{
text: "hello",
type: "text",
},
],
name: "w:t",
type: "element",
},
],
name: "w:r",
type: "element",
},
right: {
elements: [
{
attributes: {
"xml:space": "preserve",
},
elements: [
{
text: "world",
type: "text",
},
],
name: "w:t",
type: "element",
},
2023-03-16 01:55:18 +00:00
{
name: "w:x",
type: "element",
},
2023-03-15 02:46:39 +00:00
],
name: "w:r",
type: "element",
},
});
});
2023-03-16 01:55:18 +00:00
it("should try to split even if elements is empty for text", () => {
const output = splitRunElement(
{
name: "w:r",
type: "element",
elements: [
{
name: "w:t",
type: "element",
},
],
},
"*",
);
expect(output).to.deep.equal({
left: {
elements: [
{
attributes: {
"xml:space": "preserve",
},
elements: [],
name: "w:t",
type: "element",
},
],
name: "w:r",
type: "element",
},
right: {
elements: [],
name: "w:r",
type: "element",
},
});
});
it("should return empty elements", () => {
const output = splitRunElement(
{
name: "w:r",
type: "element",
},
"*",
);
expect(output).to.deep.equal({
left: {
elements: [],
name: "w:r",
type: "element",
},
right: {
elements: [],
name: "w:r",
type: "element",
},
});
});
2023-03-15 02:46:39 +00:00
});
});