Skip to content

Commit 6bc868d

Browse files
committed
fix: empty collections will render to null
1 parent 5429acb commit 6bc868d

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/JSONLDAbstractNode.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ class JSONLDAbstractNode extends React.Component {
1111
};
1212

1313
getChildJSON(child) {
14-
if (!child) return null;
14+
if (!child) {
15+
return null;
16+
}
17+
18+
if (Array.isArray(child) && child.length === 0) {
19+
return null;
20+
}
1521

1622
const ChildClass = child.type;
1723
const { children, type, id, ...schema } = child.props;
@@ -28,7 +34,9 @@ class JSONLDAbstractNode extends React.Component {
2834
}
2935

3036
parseChildren() {
31-
if (!this.props.children) return {};
37+
if (!this.props.children) {
38+
return {};
39+
}
3240
/*
3341
* If a component has a single child, this.props.children is a Child object.
3442
* If a component has multiple children, this.props.children is an array of Child objects.
@@ -39,7 +47,8 @@ class JSONLDAbstractNode extends React.Component {
3947
.map(child => this.getChildJSON(child))
4048
.filter(child => child);
4149
}
42-
return [this.getChildJSON(this.props.children)];
50+
const childJSON = this.getChildJSON(this.props.children);
51+
return childJSON ? [childJSON] : null;
4352
}
4453

4554
render() {

src/core/__tests__/GenericNodeCollection.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,34 @@ describe("GenerticNodeCollection", () => {
3030
)
3131
).toMatchSnapshot();
3232
});
33+
it("do not render empty collection", () => {
34+
expect(
35+
renderer.create(
36+
<JSONLD>
37+
<GenericNode
38+
type="review"
39+
jsonldtype="Review"
40+
name="With empty collection"
41+
>
42+
<GenericNodeCollection type="author" />
43+
</GenericNode>
44+
</JSONLD>
45+
)
46+
).toMatchSnapshot();
47+
});
48+
it("do not render collection of empty array in it", () => {
49+
expect(
50+
renderer.create(
51+
<JSONLD>
52+
<GenericNode
53+
type="review"
54+
jsonldtype="Review"
55+
name="With collection of empty array in it"
56+
>
57+
<GenericNodeCollection type="author">{[]}</GenericNodeCollection>
58+
</GenericNode>
59+
</JSONLD>
60+
)
61+
).toMatchSnapshot();
62+
});
3363
});

src/core/__tests__/__snapshots__/GenericNodeCollection.test.js.snap

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`GenerticNodeCollection do not render collection of empty array in it 1`] = `
4+
<script
5+
type="application/ld+json"
6+
>
7+
{"@context":"https://schema.org/","@type":"Review","name":"With collection of empty array in it","author":null}
8+
</script>
9+
`;
10+
11+
exports[`GenerticNodeCollection do not render empty collection 1`] = `
12+
<script
13+
type="application/ld+json"
14+
>
15+
{"@context":"https://schema.org/","@type":"Review","name":"With empty collection","author":null}
16+
</script>
17+
`;
18+
319
exports[`GenerticNodeCollection filters out null and false nodes from a collection 1`] = `
420
<script
521
type="application/ld+json"

0 commit comments

Comments
 (0)