Skip to content

Commit

Permalink
(fix) fail gracefully if prop is not an Array and issue warning (nfl#233
Browse files Browse the repository at this point in the history
)

* (fix) fail gracefully if prop is not an Array and issue warning

* (lint) remove console log
  • Loading branch information
potench authored and cwelch5 committed Feb 10, 2017
1 parent 79403c7 commit 9393ba2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Helmet.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ const getTagsFromPropsList = (tagName, primaryAttributes, propsList) => {
const approvedSeenTags = {};

return propsList
.filter(props => typeof props[tagName] !== "undefined")
.filter((props) => {
if (Array.isArray(props[tagName])) {
return true;
}
if (typeof props[tagName] !== "undefined") {
warn(`Helmet: ${tagName} should be of type "Array". Instead found type "${typeof props[tagName]}"`);
}
return false;
})
.map(props => props[tagName])
.reverse()
.reduce((approvedTags, instanceTags) => {
Expand Down Expand Up @@ -465,6 +473,10 @@ const handleClientStateChange = (newState) => {
onChangeClientState(newState, addedTags, removedTags);
};

const warn = (msg) => {
return console && typeof console.warn === "function" && console.warn(msg);
};

const NullComponent = () => null;

const HelmetSideEffects = withSideEffect(
Expand Down
13 changes: 13 additions & 0 deletions src/test/HelmetTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,19 @@ describe("Helmet", () => {
const existingTags = Array.prototype.slice.call(tagNodes);
expect(existingTags).to.be.empty;
});

it("fails gracefully when meta is wrong shape", () => {
ReactDOM.render(
<Helmet
meta={{"name": "title", "content": "some title"}}
/>,
container
);

const tagNodes = headElement.querySelectorAll(`meta[${HELMET_ATTRIBUTE}]`);
const existingTags = Array.prototype.slice.call(tagNodes);
expect(existingTags).to.be.empty;
});
});

describe("link tags", () => {
Expand Down

0 comments on commit 9393ba2

Please sign in to comment.