react-moment-proptypes
Advanced tools
Comparing version 1.2.1 to 1.3.0
{ | ||
"name": "react-moment-proptypes", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "React proptype for moment module", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -11,3 +11,3 @@ # react-moment-proptypes | ||
``` javascript | ||
``` jsx | ||
var momentPropTypes = require('react-moment-proptypes'); | ||
@@ -18,2 +18,7 @@ | ||
dateThing : momentPropTypes.momentObj, | ||
dateThingWithPredicate : momentPropTypes.momentObj.withPredicate( | ||
function isUTC(momentObject) { | ||
return momentObject.isUTC(); | ||
} | ||
), | ||
stringThing : momentPropTypes.momentString, | ||
@@ -30,2 +35,3 @@ durationThing: momentPropTypes.momentDurationObj, | ||
<TestClass dateThing={moment()} | ||
dateThingWithPredicate={moment.utc()} | ||
stringThing={'12-12-2014'} | ||
@@ -32,0 +38,0 @@ durationThing={moment.duration(3, 'hours')}/> |
@@ -18,4 +18,11 @@ var moment = require('moment'); | ||
function propValidator(isRequired, props, propName, componentName, location, propFullName) { | ||
function propValidator( | ||
isRequired, // Bound parameter to indicate with the propType is required | ||
predicate, // Bound parameter to allow user to add dynamic validation | ||
props, | ||
propName, | ||
componentName, | ||
location, | ||
propFullName | ||
) { | ||
if (isRequired) { | ||
@@ -55,2 +62,11 @@ var locationName = ReactPropTypeLocationNames[ location ]; | ||
if (predicate && ! predicate(propValue)) { | ||
var predicateName = predicate.name || ANONYMOUS; | ||
return new Error( | ||
'Invalid ' + location + ' `' + propName + '` of type `' + propType + '` ' + | ||
'supplied to `' + componentName + '`. Failed to succeed with predicate `' + | ||
predicateName + '`.' | ||
); | ||
} | ||
return null; | ||
@@ -60,4 +76,12 @@ | ||
var requiredPropValidator = propValidator.bind(null, false); | ||
requiredPropValidator.isRequired = propValidator.bind(null, true); | ||
var requiredPropValidator = propValidator.bind(null, false, null); | ||
requiredPropValidator.isRequired = propValidator.bind(null, true, null); | ||
requiredPropValidator.withPredicate = function predicateApplication(predicate) { | ||
if (typeof predicate !== 'function') { | ||
throw new Error('`predicate` must be a function'); | ||
} | ||
var basePropValidator = propValidator.bind(null, false, predicate); | ||
basePropValidator.isRequired = propValidator.bind(null, true, predicate); | ||
return basePropValidator; | ||
}; | ||
@@ -64,0 +88,0 @@ return requiredPropValidator; |
@@ -0,0 +0,0 @@ var moment = require('moment'); |
@@ -0,0 +0,0 @@ var jsdom = require('jsdom'); |
160
test/test.js
@@ -322,2 +322,162 @@ import React from 'react'; | ||
describe('Proptype Predicate', () => { | ||
describe('momentObj invalid predicate', () => { | ||
it('should throw', () => { | ||
let propValidator; | ||
expect(() => { | ||
propValidator = MomentPropTypes.momentObj.withPredicate(null); | ||
}).to.throw(Error); | ||
expect(propValidator).to.be.undefined; | ||
}); | ||
}); | ||
describe('momentObj named predicate', () => { | ||
function isUTC(m) { | ||
return false; | ||
} | ||
before(() => { | ||
TestClass = React.createClass({ | ||
propTypes : { | ||
testObjectNamedPredicate : MomentPropTypes.momentObj.withPredicate(isUTC), | ||
}, | ||
render() { | ||
return null; | ||
}, | ||
}); | ||
}); | ||
it('should have warn failed predicate with name', (done) => { | ||
const testProps = { testObjectNamedPredicate : moment() }; | ||
const testElement = <TestClass {...testProps} />; | ||
TestUtils.renderIntoDocument(testElement); | ||
expect(warnings).to.be.an('array', constructWarningsMessage(warnings)); | ||
expect(warnings.length).to.equal(1, constructWarningsMessage(warnings)); | ||
expect(warnings[0]).to.contain('Failed to succeed with predicate `' + isUTC.name + '`'); | ||
done(); | ||
}); | ||
}); | ||
describe('momentObj anonymous predicate', () => { | ||
before(() => { | ||
TestClass = React.createClass({ | ||
propTypes : { | ||
testObjectAnonPredicate : MomentPropTypes.momentObj | ||
.withPredicate(function() { return false; }), | ||
}, | ||
render() { | ||
return null; | ||
}, | ||
}); | ||
}); | ||
it('should have warn failed predicate without name', (done) => { | ||
const testProps = { testObjectAnonPredicate : moment() }; | ||
const testElement = <TestClass {...testProps} />; | ||
TestUtils.renderIntoDocument(testElement); | ||
expect(warnings).to.be.an('array', constructWarningsMessage(warnings)); | ||
expect(warnings.length).to.equal(1, constructWarningsMessage(warnings)); | ||
expect(warnings[0]).to.contain('Failed to succeed with predicate'); | ||
expect(warnings[0]).to.not.contain('isUTC'); | ||
done(); | ||
}); | ||
}); | ||
describe('momentObj required predicate', () => { | ||
function isUTC(m) { | ||
return false; | ||
} | ||
before(() => { | ||
TestClass = React.createClass({ | ||
propTypes : { | ||
testObjectRequiredPredicate : MomentPropTypes.momentObj | ||
.withPredicate(isUTC).isRequired, | ||
}, | ||
render() { | ||
return null; | ||
}, | ||
}); | ||
}); | ||
it('should have warn failed predicate with name', (done) => { | ||
const testProps = { testObjectRequiredPredicate : moment() }; | ||
const testElement = <TestClass {...testProps} />; | ||
TestUtils.renderIntoDocument(testElement); | ||
expect(warnings).to.be.an('array', constructWarningsMessage(warnings)); | ||
expect(warnings.length).to.equal(1, constructWarningsMessage(warnings)); | ||
expect(warnings[0]).to.contain('Failed to succeed with predicate `' + isUTC.name + '`'); | ||
done(); | ||
}); | ||
}); | ||
describe('success predicate', () => { | ||
function namedPredicate(m) { | ||
return true; | ||
} | ||
before(() => { | ||
TestClass = React.createClass({ | ||
propTypes : { | ||
testObjectNamedPredicate : MomentPropTypes.momentObj.withPredicate(namedPredicate).isRequired, | ||
testObjectAnonPredicate : MomentPropTypes.momentObj.withPredicate(() => true).isRequired, | ||
testObjectRequiredNamedPredicate : MomentPropTypes.momentObj.withPredicate(namedPredicate).isRequired, | ||
testObjectRequiredAnonPredicate : MomentPropTypes.momentObj.withPredicate(() => true).isRequired, | ||
}, | ||
render() { | ||
return null; | ||
}, | ||
}); | ||
}); | ||
it('should have warn failed predicate with name', (done) => { | ||
const testProps = { | ||
testObjectNamedPredicate : moment(), | ||
testObjectAnonPredicate : moment(), | ||
testObjectRequiredNamedPredicate : moment(), | ||
testObjectRequiredAnonPredicate : moment(), | ||
}; | ||
const testElement = <TestClass {...testProps} />; | ||
TestUtils.renderIntoDocument(testElement); | ||
expect(warnings).to.be.an('array', constructWarningsMessage(warnings)); | ||
expect(warnings.length).to.equal(0, constructWarningsMessage(warnings)); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25146
458
51