/
index.test.tsx
156 lines (130 loc) · 4.11 KB
/
index.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import * as React from "react";
import renderer from "react-test-renderer";
import serializer from ".";
describe("Serializer test function", () => {
// This is how React Helmet sets data of the element. An end user likely wouldn't do this on their own.
it("returns true when given a JSON+LD script element with data set dangerously", () => {
const element = (
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify({}) }}
/>
);
const shouldSerialize = serializer.test(element);
expect(shouldSerialize).toEqual(true);
});
it("returns false when given a script element that's not JSON+LD", () => {
const element = (
<script
type="application/json"
dangerouslySetInnerHTML={{ __html: JSON.stringify({}) }}
/>
);
const shouldSerialize = serializer.test(element);
expect(shouldSerialize).toEqual(false);
});
it("returns false when given a non-script element", () => {
const element = <div />;
const shouldSerialize = serializer.test(element);
expect(shouldSerialize).toEqual(false);
});
it("return false when given a React component", () => {
const Component = (): React.ReactElement => (
<div>
<span>Test Component</span>
</div>
);
const shouldSerialize = serializer.test(<Component />);
expect(shouldSerialize).toEqual(false);
});
it("returns false if value is undefined", () => {
const shouldSerialize = serializer.test(undefined);
expect(shouldSerialize).toEqual(false);
});
});
describe("Serialization function", () => {
it("serializes a JSON+LD script element with data set dangerously correctly", () => {
expect.addSnapshotSerializer(serializer);
const ldScriptElement = (
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify({}) }}
/>
);
expect(ldScriptElement).toMatchSnapshot();
});
it("serializes a script element with data correctly", () => {
expect.addSnapshotSerializer(serializer);
const element = (
<script
type="application/ld+json"
dangerouslySetInnerHTML={{
__html: JSON.stringify({
"@context": "http://schema.org",
"@type": "Thing",
"@id": "https://example.dev/example_thing"
})
}}
/>
);
expect(element).toMatchSnapshot();
});
it("serializes a wrapped script element with data correctly", () => {
expect.addSnapshotSerializer(serializer);
const element = (
<div>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{
__html: JSON.stringify({
"@context": "http://schema.org",
"@type": "Thing",
"@id": "https://example.dev/example_thing"
})
}}
/>
</div>
);
expect(element).toMatchSnapshot();
});
it("serializes a script element created using React.createElement", () => {
expect.addSnapshotSerializer(serializer);
const element = React.createElement("script", {
type: "application/ld+json",
dangerouslySetInnerHTML: {
__html: JSON.stringify({
"@context": "http://schema.org",
"@type": "Thing",
"@id": "https://example.dev/example_thing"
})
}
});
expect(element).toMatchSnapshot();
});
it("serializes a script element wrapped in a component", () => {
expect.addSnapshotSerializer(serializer);
const Component = () => (
<div>
<div>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{
__html: JSON.stringify({
"@context": "http://schema.org",
"@type": "Thing",
"@id": "https://example.dev/example_thing"
})
}}
/>
</div>
</div>
);
const tree = renderer.create(<Component />).toJSON();
expect(tree).toMatchSnapshot();
});
});
describe("Serialization module", () => {
it("matches expectations", () => {
expect(serializer).toMatchSnapshot();
});
});