expect-proptypes
Advanced tools
+3
-2
| { | ||
| "name": "expect-proptypes", | ||
| "version": "0.1.0", | ||
| "version": "0.2.0", | ||
| "description": "Allows expect to use", | ||
@@ -16,3 +16,4 @@ "main": "src/index.js", | ||
| "extended-proptypes": "^1.1.5", | ||
| "mocha": "^3.2.0" | ||
| "mocha": "^3.2.0", | ||
| "proptypes": "^0.14.4" | ||
| }, | ||
@@ -19,0 +20,0 @@ "peerDependencies": { |
+4
-1
| # expect-proptypes | ||
| This library allows assertions about object shapes using React's PropTypes library or the standalone [proptypes library](https://www.npmjs.com/package/proptypes) on npm. | ||
| This library extends [expect](https://github.com/mjackson/expect) to allow for assertions about object shapes, using React's PropTypes library or the standalone [proptypes library](https://www.npmjs.com/package/proptypes) on npm. | ||
@@ -32,3 +32,6 @@ ## Usage | ||
| ## How can I get better error logging? | ||
| If your proptypes have a property `__name__` which is a string, this will be used in the error logs. | ||
| ## What if I want more detailed types? | ||
| Consider using my library [extended-proptypes](https://www.npmjs.com/package/extended-proptypes), which adds many useful validators, such as dates,constants, constants and regex matching for strings and object keys. |
+14
-2
@@ -14,2 +14,10 @@ const util = require("util"); | ||
| let name = "expectation"; | ||
| if (expectedPropTypes.__name__ && typeof expectedPropTypes.__name__ === "string") { | ||
| name = expectedPropTypes.__name__; | ||
| // clone and delete name prop | ||
| expectedPropTypes = Object.assign({}, expectedPropTypes); | ||
| delete expectedPropTypes.__name__; | ||
| } | ||
| if (!allowExtraProps) { | ||
@@ -19,3 +27,3 @@ const extraProps = Object.keys(input) | ||
| if (extraProps.length > 0) { | ||
| throw new Error(`object has extra props: ${extraProps.join(", ")}`) | ||
| throw new Error(`input has extra props: ${extraProps.join(", ")}`) | ||
| } | ||
@@ -27,3 +35,7 @@ } | ||
| .map((prop) => { | ||
| const err = expectedPropTypes[prop](input, prop, expectedPropTypes.name || "expect", "prop") | ||
| const validator = expectedPropTypes[prop]; | ||
| if (typeof validator !== "function") { | ||
| throw new Error(`invalid validator (not a function): ${validator}`) | ||
| } | ||
| const err = validator(input, prop, name, "prop"); | ||
| if (!err) return null; | ||
@@ -30,0 +42,0 @@ return `\n - ${err.message}`; |
+54
-7
@@ -61,2 +61,9 @@ const expect = require("expect"); | ||
| it("fails if a validator is not a function", () => { | ||
| shouldThrow( | ||
| () => expect({}).toHaveProps({foo: "bar"}), | ||
| "invalid validator (not a function): bar" | ||
| ); | ||
| }); | ||
| it("fails if actual value does not match expected shape", () => { | ||
@@ -73,5 +80,5 @@ const input = { | ||
| `invalid propTypes | ||
| - Invalid prop \`num\` of type \`string\` supplied to \`expect\`, expected \`number\`. | ||
| - Required prop \`str\` was not specified in \`expect\`. | ||
| - Invalid prop \`obj.arr[2]\` of type \`boolean\` supplied to \`expect\`, expected \`string\`.` | ||
| - Invalid prop \`num\` of type \`string\` supplied to \`expectation\`, expected \`number\`. | ||
| - Required prop \`str\` was not specified in \`expectation\`. | ||
| - Invalid prop \`obj.arr[2]\` of type \`boolean\` supplied to \`expectation\`, expected \`string\`.` | ||
| ); | ||
@@ -93,6 +100,22 @@ }); | ||
| () => expect(input).toHaveProps(shape), | ||
| `object has extra props: extraProperty` | ||
| `input has extra props: extraProperty` | ||
| ); | ||
| }); | ||
| it("can add shape name to errors", () => { | ||
| const namedShape = { | ||
| __name__: "My Shape", | ||
| num: pt.number, | ||
| }; | ||
| const input = { | ||
| num: "1", | ||
| }; | ||
| shouldThrow( | ||
| () => expect(input).toHaveProps(namedShape), | ||
| `invalid propTypes | ||
| - Invalid prop \`num\` of type \`string\` supplied to \`My Shape\`, expected \`number\`.` | ||
| ); | ||
| }); | ||
| it("passes if actual value matches expected shape", () => { | ||
@@ -142,2 +165,9 @@ const input = { | ||
| it("fails if a validator is not a function", () => { | ||
| shouldThrow( | ||
| () => expect({}).toContainProps({foo: "bar"}), | ||
| "invalid validator (not a function): bar" | ||
| ); | ||
| }); | ||
| it("fails if actual value does not match expected shape", () => { | ||
@@ -154,5 +184,5 @@ const input = { | ||
| `invalid propTypes | ||
| - Invalid prop \`num\` of type \`string\` supplied to \`expect\`, expected \`number\`. | ||
| - Required prop \`str\` was not specified in \`expect\`. | ||
| - Invalid prop \`obj.arr[2]\` of type \`boolean\` supplied to \`expect\`, expected \`string\`.` | ||
| - Invalid prop \`num\` of type \`string\` supplied to \`expectation\`, expected \`number\`. | ||
| - Required prop \`str\` was not specified in \`expectation\`. | ||
| - Invalid prop \`obj.arr[2]\` of type \`boolean\` supplied to \`expectation\`, expected \`string\`.` | ||
| ); | ||
@@ -174,2 +204,19 @@ }); | ||
| it("can add shape name to errors", () => { | ||
| const namedShape = { | ||
| __name__: "My Shape", | ||
| num: pt.number, | ||
| }; | ||
| const input = { | ||
| num: "1", | ||
| foo: "bar", | ||
| }; | ||
| shouldThrow( | ||
| () => expect(input).toContainProps(namedShape), | ||
| `invalid propTypes | ||
| - Invalid prop \`num\` of type \`string\` supplied to \`My Shape\`, expected \`number\`.` | ||
| ); | ||
| }); | ||
| it("passes if actual value matches expected shape", () => { | ||
@@ -176,0 +223,0 @@ const input = { |
8967
25.38%247
26.67%36
9.09%6
20%