Skip to content

Commit 78c6069

Browse files
committed
update tests
1 parent f59c78e commit 78c6069

File tree

4 files changed

+35
-63
lines changed

4 files changed

+35
-63
lines changed

tests/transfomers/portable-text-transformer/html-transformer.spec.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ import { PortableTextTypeComponentOptions } from "@portabletext/to-html";
33

44
import {
55
ArbitraryTypedObject,
6-
nodesToPortableText,
76
PortableTextBlock,
87
PortableTextComponentOrItem,
98
PortableTextExternalLink,
109
PortableTextImage,
1110
PortableTextItemLink,
1211
PortableTextMark,
1312
PortableTextTable,
13+
transformToPortableText,
1414
} from "../../../src";
15-
import { browserParse } from "../../../src/parser/browser";
16-
import { nodeParse } from "../../../src/parser/node";
1715
import { PortableTextHtmlResolvers, resolveImage, toHTML } from "../../../src/utils/resolution/html";
1816

1917
jest.mock("short-unique-id", () => {
@@ -125,21 +123,13 @@ describe("HTML transformer", () => {
125123
) => {
126124
richTextInput.value = richTextValue;
127125

128-
const browserTree = browserParse(richTextInput.value);
129-
const nodeTree = nodeParse(richTextInput.value);
130-
const nodePortableText = nodesToPortableText(nodeTree);
131-
const browserPortableText = nodesToPortableText(browserTree);
132-
const nodeResult = toHTML(
133-
nodePortableText,
134-
getPortableTextComponents(richTextInput, customResolvers),
135-
);
136-
const browserResult = toHTML(
137-
browserPortableText,
126+
const portableText = transformToPortableText(richTextInput.value);
127+
const result = toHTML(
128+
portableText,
138129
getPortableTextComponents(richTextInput, customResolvers),
139130
);
140131

141-
expect(nodeResult).toMatchSnapshot();
142-
expect(nodeResult).toEqual(browserResult);
132+
expect(result).toMatchSnapshot();
143133
};
144134

145135
it("builds basic portable text into HTML", () => {

tests/transfomers/portable-text-transformer/mapi-transformer.spec.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { ArbitraryTypedObject, nodesToPortableText, PortableTextSpan, traversePortableText } from "../../../src";
2-
import { browserParse } from "../../../src/parser/browser";
3-
import { nodeParse } from "../../../src/parser/node";
1+
import { ArbitraryTypedObject, PortableTextSpan, transformToPortableText, traversePortableText } from "../../../src";
42
import { toManagementApiFormat } from "../../../src/utils/resolution/mapi";
53

64
jest.mock("short-unique-id", () => {
@@ -17,29 +15,18 @@ const sortMarks = (obj: ArbitraryTypedObject) => isSpan(obj) ? { ...obj, marks:
1715

1816
describe("portabletext to MAPI resolver", () => {
1917
const transformAndCompare = (richTextContent: string) => {
20-
// Parse the rich text content into a tree
21-
const browserTree = browserParse(richTextContent);
22-
const nodeTree = nodeParse(richTextContent);
23-
24-
// Convert the tree to Portable Text
25-
const nodePortableText = nodesToPortableText(nodeTree);
26-
const browserPortableText = nodesToPortableText(browserTree);
18+
const portableText = transformToPortableText(richTextContent);
2719

2820
// Convert Portable Text to MAPI format
29-
const nodeManagementApiFormat = toManagementApiFormat(nodePortableText);
30-
const browserManagementApiFormat = toManagementApiFormat(browserPortableText);
21+
const managementApiFormat = toManagementApiFormat(portableText);
3122

3223
// Parse the MAPI format back into a tree and convert it to Portable Text
33-
const secondParseTree = nodeParse(nodeManagementApiFormat);
34-
const secondParsePortableText = nodesToPortableText(secondParseTree);
35-
36-
// Compare the MAPI formats to ensure consistency across platforms
37-
expect(nodeManagementApiFormat).toEqual(browserManagementApiFormat);
24+
const secondParsePortableText = transformToPortableText(managementApiFormat);
3825

3926
// Compare the original Portable Text to the re-parsed Portable Text after MAPI conversion
4027
expect(
4128
traversePortableText(secondParsePortableText, sortMarks),
42-
).toStrictEqual(traversePortableText(nodePortableText, sortMarks));
29+
).toStrictEqual(traversePortableText(portableText, sortMarks));
4330
};
4431

4532
it("handles nested style marks", () => {
@@ -99,12 +86,10 @@ describe("portabletext to MAPI resolver", () => {
9986
*/
10087
const richTextContent =
10188
`<p><strong>strong text </strong><a href="https://example.com"><strong>example strong link text</strong>not strong link text</a></p>`;
102-
const tree = nodeParse(richTextContent);
103-
const portableText = nodesToPortableText(tree);
89+
const portableText = transformToPortableText(richTextContent);
10490
const mapiFormat = toManagementApiFormat(portableText);
10591

106-
const secondParseTree = nodeParse(mapiFormat);
107-
const secondParsePortableText = nodesToPortableText(secondParseTree);
92+
const secondParsePortableText = transformToPortableText(mapiFormat);
10893
const secondParseMapiFormat = toManagementApiFormat(
10994
secondParsePortableText,
11095
);

tests/transfomers/portable-text-transformer/portable-text-transformer.spec.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { nodesToPortableText, PortableTextItem, traversePortableText } from "../../../src";
1+
import { DomNode, PortableTextItem, transformToPortableText, traversePortableText } from "../../../src";
22
import { browserParse } from "../../../src/parser/browser";
33
import { nodeParse } from "../../../src/parser/node";
44

@@ -14,21 +14,23 @@ describe("Portable Text Transformer", () => {
1414
const transformInput = (
1515
input: string,
1616
): {
17-
nodeResult: PortableTextItem[];
18-
browserResult: PortableTextItem[];
17+
nodeTree: DomNode[];
18+
browserTree: DomNode[];
19+
result: PortableTextItem[];
1920
} => {
2021
const browserTree = browserParse(input);
2122
const nodeTree = nodeParse(input);
2223
return {
23-
nodeResult: nodesToPortableText(nodeTree),
24-
browserResult: nodesToPortableText(browserTree),
24+
nodeTree,
25+
browserTree,
26+
result: transformToPortableText(input),
2527
};
2628
};
2729

2830
const transformAndCompare = (input: string) => {
29-
const { nodeResult, browserResult } = transformInput(input);
30-
expect(nodeResult).toMatchSnapshot();
31-
expect(nodeResult).toMatchObject(browserResult);
31+
const { nodeTree, browserTree, result } = transformInput(input);
32+
expect(result).toMatchSnapshot();
33+
expect(nodeTree).toMatchObject(browserTree);
3234
};
3335

3436
it("transforms empty rich text", () => {
@@ -246,14 +248,10 @@ describe("Portable Text Transformer", () => {
246248
transformAndCompare(`<p>text<strong>bold</strong></p>`);
247249
});
248250

249-
it.each([nodeParse, browserParse])(
250-
"throws error for non-supported tags for %s",
251-
(parse) => {
252-
const input = "<p>text in a paragraph</p><div>text in a div, which doesnt exist in kontent RTE</div>";
253-
const tree = parse(input);
254-
expect(() => nodesToPortableText(tree)).toThrow();
255-
},
256-
);
251+
it("throws error for non-supported tags", () => {
252+
const input = "<p>text in a paragraph</p><div>text in a div, which doesnt exist in kontent RTE</div>";
253+
expect(() => transformToPortableText(input)).toThrow();
254+
});
257255

258256
it("doesn't extend link mark to adjacent spans", () => {
259257
transformAndCompare(
@@ -301,13 +299,14 @@ describe("Portable Text Transformer", () => {
301299
`<object type="application/kenticocloud" data-type="item" data-rel="link" data-codename="test_item"></object>`;
302300

303301
const processBlock = (block: PortableTextItem) =>
304-
block._type === "componentOrItem" ? { ...block, additionalData: "data" } : null;
302+
block._type === "componentOrItem"
303+
? { ...block, additionalData: "data" }
304+
: null;
305305

306-
const { nodeResult } = transformInput(input);
307-
const modifiedResult = traversePortableText(nodeResult, processBlock);
306+
const { result } = transformInput(input);
307+
const modifiedResult = traversePortableText(result, processBlock);
308308

309309
expect(modifiedResult).toMatchSnapshot();
310-
expect(modifiedResult).toMatchObject(nodeResult);
311310
});
312311

313312
it("extends link nested in a table with additional data", () => {
@@ -316,11 +315,10 @@ describe("Portable Text Transformer", () => {
316315
const processBlock = (block: PortableTextItem) =>
317316
block._type === "link" ? { ...block, additionalData: "data" } : null;
318317

319-
const { nodeResult } = transformInput(input);
320-
const transformedResult = traversePortableText(nodeResult, processBlock);
318+
const { result } = transformInput(input);
319+
const transformedResult = traversePortableText(result, processBlock);
321320

322321
expect(transformedResult).toMatchSnapshot();
323-
expect(transformedResult).toMatchObject(nodeResult);
324322
});
325323

326324
it("transforms a linked item and a component from MAPI with corresponding dataType", () => {

tests/transfomers/portable-text-transformer/vue-transformer.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { PortableText, PortableTextComponentProps, PortableTextComponents, toPla
22
import { mount } from "@vue/test-utils";
33
import { h } from "vue";
44

5-
import { nodesToPortableText, parseHtml, PortableTextImage, PortableTextTable } from "../../../src";
5+
import { PortableTextImage, PortableTextTable, transformToPortableText } from "../../../src";
66
import { resolveImage, resolveTable } from "../../../src/utils/resolution/vue";
77

88
const components: PortableTextComponents = {
@@ -17,8 +17,7 @@ describe("PortableText Vue Renderer", () => {
1717
richTextValue: string,
1818
customComponents = components,
1919
) => {
20-
const jsonTree = parseHtml(richTextValue);
21-
const portableText = nodesToPortableText(jsonTree);
20+
const portableText = transformToPortableText(richTextValue);
2221

2322
return mount(PortableText, {
2423
props: {

0 commit comments

Comments
 (0)