Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## [Unreleased]
<details>
<summary>
Changes that have landed in master but are not yet released.
</summary>

* Generators are now deterministic by default. Previously, snapshot tests might break if `oneOf` or `oneOfType` were used.
* `opts` can now be passed as the second arg to `generateProps` to generate or omit required or optional types. Default: `{ required: true, optional: false }`
* Generators can be overridden via the `opts` argument. Syntax: `generateProps(Component, { generators: { bool: () => false } })`
* `string`, `any`, and `node` generators now return exactly those strings. (`'string'`, `'any'`, or `'node'`)
* React 16 and the `prop-types` package are now supported.

</details>

## 0.3.0 (October 5, 2017)
## 0.2.0 (October 18, 2016)
## 0.1.0 (October 18, 2016)
## 0.0.1 (July 14, 2016)
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"src/*"
],
"dependencies": {
"lodash": "^4.13.1",
"sinon": "^1.17.4"
"lodash": "^4.13.1"
},
"devDependencies": {
"babel-cli": "^6.16.0",
Expand All @@ -25,7 +24,7 @@
"react": "^15.3.2"
},
"peerDependencies": {
"react": "0.13.x || 0.14.x || ^15.0.0-0 || 15.x"
"react": "0.13.x || 0.14.x || ^15.0.0-0 || 15.x || 16.x"
},
"scripts": {
"build": "babel src -d dist",
Expand All @@ -35,10 +34,11 @@
"coveralls": "cat ./coverage/lcov.info | coveralls",
"postcoveralls": "rm -rf ./coverage",
"test": "npm run react:13 && mocha && npm run react:14 && mocha && npm run react:15 && mocha",
"react:clean": "rm -rf node_modules/react node_modules/react-dom node_modules/react-addons-test-utils",
"react:clean": "rm -rf node_modules/react node_modules/react-dom node_modules/react-addons-test-utils node_modules/prop-types",
"react:13": "npm run react:clean && npm i react@0.13",
"react:14": "npm run react:clean && npm i react@0.14 react-dom@0.14 react-addons-test-utils@0.14",
"react:15": "npm run react:clean && npm i react@15 react-dom@15 react-addons-test-utils@15"
"react:15": "npm run react:clean && npm i react@15 react-dom@15 react-addons-test-utils@15",
"react:16": "npm run react:clean && npm i react@16 prop-types"
},
"repository": {
"type": "git",
Expand Down
40 changes: 25 additions & 15 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const _ = require('lodash')
const sinon = require('sinon')
const React = require('react')
const { PropTypes } = React
const PropTypes = require('./prop-types')

// TODO: Options API
const options = {
required: true
}
let options

const wrapPropTypes = () => {
// Adds a .type key which allows the type to be derived during the
Expand Down Expand Up @@ -37,26 +33,25 @@ const GENERATORS = {
// Simple types
array: () => [],
bool: () => true,
func: () => sinon.spy(),
func: () => () => {},
number: () => 1,
object: () => ({}),
string: () => 'A String',
any: () => 'Any',
string: () => 'string',
any: () => 'any',
element: () => React.createElement('div'),
node: () => [React.createElement('div'), React.createElement('div')],
node: () => 'node',

// Complex types
arrayOf: (type) => [generateOneProp(type)],
instanceOf: (klass) => new klass(),
objectOf: (type) => ({ key: generateOneProp(type) }),
oneOf: (values) => _.sample(values),
oneOfType: (types) => generateOneProp(_.extend(_.sample(types), { forceGeneration: true })),
oneOf: (values) => _.first(values),
oneOfType: (types) => forceGenerateOneProp(_.first(types)),
shape: (shape) => generateProps(shape)
}

const shouldGenerate = (propType) => {
return (
propType.forceGeneration ||
// Generate required props, and this is the required version
(options.required && !propType.isRequired) ||
// Generate optional props, and this is the optional version
Expand All @@ -65,7 +60,7 @@ const shouldGenerate = (propType) => {
}

const generateOneProp = (propType, propName) => {
const generate = GENERATORS[propType.type]
const generate = options.generators[propType.type]
const arg = propType.arg
if (generate) {
if (shouldGenerate(propType)) {
Expand All @@ -78,7 +73,22 @@ const generateOneProp = (propType, propName) => {
}
}

const generateProps = (arg) => {
const forceGenerateOneProp = (propType) => {
const generate = GENERATORS[propType.type]
const arg = propType.arg
if (generate) {
return generate(arg)
}
}

const generateProps = (arg, opts) => {
options = _.defaults({}, opts, { required: true, optional: false })
if (opts && opts.generators) {
options.generators = _.defaults({}, opts.generators, GENERATORS)
} else {
options.generators = GENERATORS
}

let propTypes

if (!arg) {
Expand Down
6 changes: 6 additions & 0 deletions src/prop-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let PropTypes

try { PropTypes = require('prop-types') }
catch (e) { PropTypes = require('react').PropTypes }

module.exports = PropTypes
Loading