Skip to content

Commit a048a6c

Browse files
committed
Add tests for jsonLdScriptProps.
1 parent 4a497ec commit a048a6c

1 file changed

Lines changed: 138 additions & 28 deletions

File tree

test/jsonld_func_test.ts

Lines changed: 138 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,119 @@
1-
import {Person} from 'schema-dts';
1+
import { Person } from "schema-dts";
22

3-
import {helmetJsonLdProp} from '../src';
3+
import { helmetJsonLdProp, jsonLdScriptProps } from "../src";
44

5-
test('works', () => {
6-
expect(helmetJsonLdProp<Person>({
7-
'@context': 'https://schema.org',
8-
'@type': 'Person',
9-
})).toMatchInlineSnapshot(`
5+
test("works", () => {
6+
expect(
7+
helmetJsonLdProp<Person>({
8+
"@context": "https://schema.org",
9+
"@type": "Person",
10+
})
11+
).toMatchInlineSnapshot(`
1012
Object {
1113
"innerHTML": "{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Person\\"}",
1214
"type": "application/ld+json",
1315
}
1416
`);
17+
18+
expect(
19+
jsonLdScriptProps<Person>({
20+
"@context": "https://schema.org",
21+
"@type": "Person",
22+
})
23+
).toMatchInlineSnapshot(`
24+
Object {
25+
"dangerouslySetInnerHTML": Object {
26+
"__html": "{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Person\\"}",
27+
},
28+
"type": "application/ld+json",
29+
}
30+
`);
31+
32+
expect(
33+
jsonLdScriptProps<Person>(
34+
{
35+
"@context": "https://schema.org",
36+
"@type": "Person",
37+
},
38+
/* options=*/ {}
39+
)
40+
).toMatchInlineSnapshot(`
41+
Object {
42+
"dangerouslySetInnerHTML": Object {
43+
"__html": "{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Person\\"}",
44+
},
45+
"type": "application/ld+json",
46+
}
47+
`);
48+
49+
expect(
50+
jsonLdScriptProps<Person>(
51+
{
52+
"@context": "https://schema.org",
53+
"@type": "Person",
54+
},
55+
/* options=*/ { space: 2 }
56+
)
57+
).toMatchInlineSnapshot(`
58+
Object {
59+
"dangerouslySetInnerHTML": Object {
60+
"__html": "{
61+
\\"@context\\": \\"https://schema.org\\",
62+
\\"@type\\": \\"Person\\"
63+
}",
64+
},
65+
"type": "application/ld+json",
66+
}
67+
`);
1568
});
1669

17-
test('escapes JSON-LD-illegal chars', () => {
18-
expect(helmetJsonLdProp<Person>({
19-
'@context': 'https://schema.org',
20-
'@type': 'Person',
21-
name: 'Foo</script>',
22-
})).toMatchInlineSnapshot(`
70+
test("escapes JSON-LD-illegal chars", () => {
71+
expect(
72+
helmetJsonLdProp<Person>({
73+
"@context": "https://schema.org",
74+
"@type": "Person",
75+
name: "Foo</script>",
76+
})
77+
).toMatchInlineSnapshot(`
2378
Object {
2479
"innerHTML": "{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Person\\",\\"name\\":\\"Foo&lt;/script&gt;\\"}",
2580
"type": "application/ld+json",
2681
}
2782
`);
83+
84+
expect(
85+
jsonLdScriptProps<Person>({
86+
"@context": "https://schema.org",
87+
"@type": "Person",
88+
name: "Foo</script>",
89+
})
90+
).toMatchInlineSnapshot(`
91+
Object {
92+
"dangerouslySetInnerHTML": Object {
93+
"__html": "{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Person\\",\\"name\\":\\"Foo&lt;/script&gt;\\"}",
94+
},
95+
"type": "application/ld+json",
96+
}
97+
`);
2898
});
2999

30-
test('escapes JSON-LD-illegal chars', () => {
31-
expect(helmetJsonLdProp<Person>(
32-
{
33-
'@context': 'https://schema.org',
34-
'@type': 'Person',
35-
name: ['Foo</script>', null!, undefined!],
36-
knows: [],
37-
knowsAbout: {
38-
'@type': 'CreativeWork',
39-
name: 'Foo',
40-
copyrightYear: 2020,
41-
},
42-
},
43-
{space: 2}))
44-
.toMatchInlineSnapshot(`
100+
test("escapes JSON-LD-illegal chars", () => {
101+
expect(
102+
helmetJsonLdProp<Person>(
103+
{
104+
"@context": "https://schema.org",
105+
"@type": "Person",
106+
name: ["Foo</script>", null!, undefined!],
107+
knows: [],
108+
knowsAbout: {
109+
"@type": "CreativeWork",
110+
name: "Foo",
111+
copyrightYear: 2020,
112+
},
113+
},
114+
{ space: 2 }
115+
)
116+
).toMatchInlineSnapshot(`
45117
Object {
46118
"innerHTML": "{
47119
\\"@context\\": \\"https://schema.org\\",
@@ -61,4 +133,42 @@ test('escapes JSON-LD-illegal chars', () => {
61133
"type": "application/ld+json",
62134
}
63135
`);
136+
137+
expect(
138+
jsonLdScriptProps<Person>(
139+
{
140+
"@context": "https://schema.org",
141+
"@type": "Person",
142+
name: ["Foo</script>", null!, undefined!],
143+
knows: [],
144+
knowsAbout: {
145+
"@type": "CreativeWork",
146+
name: "Foo",
147+
copyrightYear: 2020,
148+
},
149+
},
150+
{ space: 2 }
151+
)
152+
).toMatchInlineSnapshot(`
153+
Object {
154+
"dangerouslySetInnerHTML": Object {
155+
"__html": "{
156+
\\"@context\\": \\"https://schema.org\\",
157+
\\"@type\\": \\"Person\\",
158+
\\"name\\": [
159+
\\"Foo&lt;/script&gt;\\",
160+
null,
161+
null
162+
],
163+
\\"knows\\": [],
164+
\\"knowsAbout\\": {
165+
\\"@type\\": \\"CreativeWork\\",
166+
\\"name\\": \\"Foo\\",
167+
\\"copyrightYear\\": 2020
168+
}
169+
}",
170+
},
171+
"type": "application/ld+json",
172+
}
173+
`);
64174
});

0 commit comments

Comments
 (0)