Skip to content

Commit 40ccc1c

Browse files
committed
test: added additional test cases for nested attrs
1 parent 6898b67 commit 40ccc1c

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

src/fromRedactor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ const getFinalImageAttributes = ({elementAttrs, newChildren, extraAttrs, sizeAtt
797797
return imageAttrs
798798
}
799799

800-
const getNestedValueIfAvailable = (value: string) => {
800+
export const getNestedValueIfAvailable = (value: string) => {
801801
try {
802802
if (typeof value === "string" && value.trim().match(/^{|\[/i)) {
803803
return JSON.parse(value);

test/fromRedactor.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @ts-nocheck
2-
import { fromRedactor } from "../src/fromRedactor"
2+
import { fromRedactor, getNestedValueIfAvailable } from "../src/fromRedactor"
33
import { JSDOM } from "jsdom"
44
import { isEqual } from "lodash"
55
import omitdeep from "omit-deep-lodash"
@@ -227,6 +227,8 @@ describe("Testing html to json conversion", () => {
227227
expect(testResult).toBe(true)
228228
})
229229

230+
describe("Nested attrs", () =>{
231+
230232
test("should convert stringified attrs to proper nested JSON attrs", () => {
231233
for (const testCase of expectedValue["nested-attrs"]) {
232234
const { json, html } = testCase;
@@ -236,4 +238,35 @@ describe("Testing html to json conversion", () => {
236238
expect(jsonValue).toStrictEqual(json);
237239
}
238240
});
239-
})
241+
242+
test("should not convert stringify attrs when `allowNonStandardTags` is not true", () => {
243+
const html = `<p><span from="Paul, Addy" to="[object Object]">Hi There!</span></p>`;
244+
const json = {"attrs": {}, "children": [{"attrs": {}, "children": [{"attrs": {"redactor-attributes": {"from": "Paul, Addy", "to": "[object Object]"}, "style": {}}, "children": [{"attrs": {"style": {}}, "text": "Hi There!"}], "type": "span", "uid": "uid"}], "type": "p", "uid": "uid"}], "type": "doc", "uid": "uid"};
245+
246+
const dom = new JSDOM(html);
247+
let htmlDoc = dom.window.document.querySelector("body");
248+
const jsonValue = fromRedactor(htmlDoc);
249+
expect(jsonValue).toStrictEqual(json);
250+
});
251+
})
252+
253+
})
254+
255+
256+
describe('getNestedValueIfAvailable', () => {
257+
258+
it('should return the input value when it\'s not a string containing JSON', () => {
259+
expect(getNestedValueIfAvailable(10)).toBe(10);
260+
expect(getNestedValueIfAvailable(null)).toBeNull();
261+
expect(getNestedValueIfAvailable('{ "name": "John", "age": }')).toBe('{ "name": "John", "age": }');
262+
expect(getNestedValueIfAvailable({ "name": "John", "age": 30})).toStrictEqual({ "name": "John", "age": 30});
263+
expect(getNestedValueIfAvailable('[Object Object]')).toBe('[Object Object]');
264+
});
265+
266+
it('should return the parsed JSON when the input value is a string containing JSON', () => {
267+
const value = '{ "name": "John", "age": 30 }';
268+
const result = getNestedValueIfAvailable(value);
269+
expect(result).toEqual({ name: "John", age: 30 });
270+
});
271+
272+
});

test/toRedactor.test.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,23 @@ describe("Testing json to html conversion", () => {
122122
let testResult = isEqual(htmlValue, expectedValue["inline-classname-and-id"].html)
123123
expect(testResult).toBe(true)
124124
})
125-
test("should have stringified attrs for nested json", () => {
126-
for (const testCase of expectedValue["nested-attrs"]) {
127-
const { json, html } = testCase;
128-
const htmlValue = toRedactor(json, { allowNonStandardTypes: true });
125+
126+
describe("Nested attrs", () => {
127+
128+
test("should have stringified attrs for nested json", () => {
129+
for (const testCase of expectedValue["nested-attrs"]) {
130+
const { json, html } = testCase;
131+
const htmlValue = toRedactor(json, { allowNonStandardTypes: true });
132+
expect(htmlValue).toBe(html);
133+
}
134+
});
135+
136+
test("should not convert to stringify attrs when `allowNonStandardTypes` is not true", () => {
137+
const html = `This is HTML-formatted content.`
138+
const json = {"type":"doc","attrs":{}, "children":[{"type":"aprimo","attrs":{ nestedAttrs: { "k1" : "v1"} },"children":[{"text":"This is HTML-formatted content."}]}]};
139+
140+
const htmlValue = toRedactor(json);
129141
expect(htmlValue).toBe(html);
130-
}
131-
});
142+
});
143+
})
132144
})

0 commit comments

Comments
 (0)