Skip to content

Commit

Permalink
[Fix] NaN is also a valid propValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 27, 2016
1 parent 404160e commit 659a829
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/ShallowTraversal.js
@@ -1,6 +1,7 @@
import React from 'react';
import isEmpty from 'lodash/isEmpty';
import isSubset from 'is-subset';
import is from 'object-is';
import {
coercePropValue,
propsOfNode,
Expand Down Expand Up @@ -98,8 +99,8 @@ export function nodeHasProperty(node, propKey, stringifiedPropValue) {
return false;
}

if (propValue || propValue === 0 || propValue === '') {
return nodePropValue === propValue;
if (propValue || propValue === 0 || propValue === '' || is(NaN, propValue)) {
return is(nodePropValue, propValue);
}

return Object.prototype.hasOwnProperty.call(nodeProps, propKey);
Expand Down
6 changes: 5 additions & 1 deletion src/Utils.js
Expand Up @@ -218,6 +218,10 @@ export function coercePropValue(propName, propValue) {
return propValue;
}

if (propValue === 'NaN') {
return NaN;
}

const trimmedValue = propValue.trim();

// if propValue includes quotes, it should be
Expand All @@ -229,7 +233,7 @@ export function coercePropValue(propName, propValue) {
const numericPropValue = +trimmedValue;

// if parseInt is not NaN, then we've wanted a number
if (!isNaN(numericPropValue)) {
if (!is(NaN, numericPropValue)) {
return numericPropValue;
}

Expand Down
5 changes: 5 additions & 0 deletions test/ShallowTraversal-spec.jsx
Expand Up @@ -126,6 +126,11 @@ describe('ShallowTraversal', () => {
expect(nodeHasProperty(<div foo={'bar'} />, 'foo', '""')).to.equal(false);
});

it('should work with NaN', () => {
expect(nodeHasProperty(<div foo={NaN} />, 'foo', 'NaN')).to.equal(true);
expect(nodeHasProperty(<div foo={0} />, 'foo', 'NaN')).to.equal(false);
});

it('should throw when un unquoted string is passed in', () => {
const node = (<div title="foo" />);

Expand Down

0 comments on commit 659a829

Please sign in to comment.