Skip to content

Commit

Permalink
fix: empty collections will render to null
Browse files Browse the repository at this point in the history
  • Loading branch information
olexiyk committed Aug 26, 2019
1 parent 5429acb commit 6bc868d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/JSONLDAbstractNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ class JSONLDAbstractNode extends React.Component {
};

getChildJSON(child) {
if (!child) return null;
if (!child) {
return null;
}

if (Array.isArray(child) && child.length === 0) {
return null;
}

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

parseChildren() {
if (!this.props.children) return {};
if (!this.props.children) {
return {};
}
/*
* If a component has a single child, this.props.children is a Child object.
* If a component has multiple children, this.props.children is an array of Child objects.
Expand All @@ -39,7 +47,8 @@ class JSONLDAbstractNode extends React.Component {
.map(child => this.getChildJSON(child))
.filter(child => child);
}
return [this.getChildJSON(this.props.children)];
const childJSON = this.getChildJSON(this.props.children);
return childJSON ? [childJSON] : null;
}

render() {
Expand Down
30 changes: 30 additions & 0 deletions src/core/__tests__/GenericNodeCollection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,34 @@ describe("GenerticNodeCollection", () => {
)
).toMatchSnapshot();
});
it("do not render empty collection", () => {
expect(
renderer.create(
<JSONLD>
<GenericNode
type="review"
jsonldtype="Review"
name="With empty collection"
>
<GenericNodeCollection type="author" />
</GenericNode>
</JSONLD>
)
).toMatchSnapshot();
});
it("do not render collection of empty array in it", () => {
expect(
renderer.create(
<JSONLD>
<GenericNode
type="review"
jsonldtype="Review"
name="With collection of empty array in it"
>
<GenericNodeCollection type="author">{[]}</GenericNodeCollection>
</GenericNode>
</JSONLD>
)
).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GenerticNodeCollection do not render collection of empty array in it 1`] = `
<script
type="application/ld+json"
>
{"@context":"https://schema.org/","@type":"Review","name":"With collection of empty array in it","author":null}
</script>
`;

exports[`GenerticNodeCollection do not render empty collection 1`] = `
<script
type="application/ld+json"
>
{"@context":"https://schema.org/","@type":"Review","name":"With empty collection","author":null}
</script>
`;

exports[`GenerticNodeCollection filters out null and false nodes from a collection 1`] = `
<script
type="application/ld+json"
Expand Down

0 comments on commit 6bc868d

Please sign in to comment.