Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not use propTypes directly? #1

Closed
bali182 opened this issue Jun 21, 2017 · 4 comments
Closed

Why not use propTypes directly? #1

bali182 opened this issue Jun 21, 2017 · 4 comments

Comments

@bali182
Copy link

bali182 commented Jun 21, 2017

Just stumbled upon this library, forgive me if I'm missing something obvious. But since the component to be tested has to be in scope already, why not just use it directly to get propTypes? Or was the data structure that react-docgen provides easier to inspect?

I mean:

module.exports = function fakeProps(Component, { optional = false } = {}) {
  assert(Component.propTypes)
  return restOfWhatYouDidBefore(Component.propTypes, optional)
}

And when using it I think it would be much more obvious to do something like this:

import MyComponent from './myComponent'
import fakeProps from 'react-fake-props'

const myProps = fakeProps(MyComponent)
const vDom = <MyComponent {...myProps} />
@typicode
Copy link
Owner

typicode commented Jun 21, 2017

Hi @bali182,

I would've preferred this kind of API too ( fakeProps(MyComponent) but it was kind of complicated to get all the PropTypes. Not for basic PropTypes like PropTypes.string, but for nested ones like PropTypes.shape({ ... }) which return a function. Maybe there's a way.

But react-docgen is really great and battle tested, plus it makes it possible to support Flow too which is getting quite popular for React projects.

@bali182
Copy link
Author

bali182 commented Jun 21, 2017

Ah that's understandable! Was worth asking though :)

@bali182 bali182 closed this as completed Jun 21, 2017
@typicode
Copy link
Owner

Was worth asking though :)

Definitely :)
I've updated the README too, thanks for the question.

@AndersDJohnson
Copy link

I think this would still be valuable for non-Flow users. One advantage is it would allow use of real requires/imports, which may be needed for Jest's --findRelatedTests functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants