Skip to content

Commit

Permalink
Merge pull request #15 from typicode/allow-multiple-components
Browse files Browse the repository at this point in the history
Add `all` option
  • Loading branch information
typicode committed Dec 2, 2018
2 parents 65b0ac0 + f3a4d96 commit 280f190
Show file tree
Hide file tree
Showing 8 changed files with 5,174 additions and 2,932 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
## Install

```sh
yarn add react-fake-props --dev
npm install react-fake-props --save-dev
```

```sh
npm install react-fake-props --save-dev
yarn add react-fake-props --dev
```

## Example
Expand Down Expand Up @@ -76,9 +76,19 @@ Please note:
- `custom` validators and `PropTypes.instanceOf` aren't supported, you'll still need to set them manually.
- `react-fake-props` requires the component path to be passed, instead of the component itself, to be able to support Flow and PropTypes.

### For multiple components in single file

By passing `{ all: true }`, `fakeProps` will return an array of all components found in `componentPath` with corresponding fake props. Works even for the ones that aren't exported.

```js
// Pick the component you want to get fake props using displayName
const components = fakeProps(componentPath, { all: true })
const { props } = components.find({ displayName } => displayName === 'SomeComponent')
```

## API

`fakeProps(componentPath[, { optional: false } ])`
`fakeProps(componentPath[, { optional: false, all: false } ])`

## Tip

Expand Down
8 changes: 8 additions & 0 deletions __tests__/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fakeProps from '../src'
const ComponentFile = path.join(__dirname, '../fixtures/flow/Component.jsx')
const SimpleFile = path.join(__dirname, '../fixtures/flow/Simple.jsx')
const NoProps = require.resolve('../fixtures/flow/NoProps.jsx')
const Multiple = require.resolve('../fixtures/flow/Multiple.jsx')

describe('fakeProps', () => {
it('should return an object with no props (snapshot)', () => {
Expand Down Expand Up @@ -55,4 +56,11 @@ describe('fakeProps', () => {
'requiredObject.requiredString'
)
})

it('should work with multiple components', () => {
const component = fakeProps(Multiple, { all: true }).find(
({ displayName }) => displayName === 'First'
)
expect(component.props.requiredString).toBe('requiredString')
})
})
8 changes: 8 additions & 0 deletions __tests__/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const ComponentFile = path.join(
)
const SimpleFile = path.join(__dirname, '../fixtures/propTypes/Simple.jsx')
const NoProps = require.resolve('../fixtures/propTypes/NoProps.jsx')
const Multiple = require.resolve('../fixtures/propTypes/Multiple.jsx')

describe('fakeProps', () => {
it('should return an object with no props (snapshot)', () => {
Expand Down Expand Up @@ -49,4 +50,11 @@ describe('fakeProps', () => {
'requiredShape.requiredString'
)
})

it('should work with multiple components', () => {
const component = fakeProps(Multiple, { all: true }).find(
({ displayName }) => displayName === 'First'
)
expect(component.props.requiredString).toBe('requiredString')
})
})
22 changes: 22 additions & 0 deletions fixtures/flow/Multiple.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @flow
import React from 'react'

function connect(Component) {
return <div><Component /></div>
}

type Props = {
requiredString: string
}

class First extends React.Component<void, Props, void> {
render () {
// ...
}
}

export function Second() {
return <div />
}

export default connect(First)
19 changes: 19 additions & 0 deletions fixtures/propTypes/Multiple.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import PropTypes from 'prop-types'

function connect (Component) {
return <div><Component /></div>
}

function First () {
return <div />
}

First.propTypes = {
requiredString: PropTypes.string.isRequired
}

export function Second () {
return <div />
}

export default connect(First)
Loading

0 comments on commit 280f190

Please sign in to comment.