typicode / react-fake-props

🔮 Magically generate fake props for your React tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support stateless components

Duskfall opened this issue · comments

Will you support stateless components?

const Foo = ({prop1,prop2}) => <div></div>

Foo.propTypes = {
    prop1:PropTypes.number.isRequired,
    prop2:PropTypes.number.isRequired,
}
export Foo

Hi @Duskfall,

Could you give another try with v0.1.3?

I've changed one of the components used in tests to be a stateless one:
https://github.com/typicode/react-fake-props/blob/master/fixtures/propTypes/Simple.jsx

It seems to work, let me know if you still have issues.

Thanks @typicode ! It works for simple stateless components now.
There is another test case though which is a more complex one. If we use HOC in a stateless component to encapsulate functionality then it breaks, because fakeProps doesn't find the PropTypes project in the object which is the stateless function at first.

TypeError: Cannot convert undefined or null to object
        at Function.keys (<anonymous>)
      
      at fakeDataForProps (node_modules/react-fake-props/lib/index.js:235:17)
      at Object.<anonymous>.module.exports (node_modules/react-fake-props/lib/index.js:253:10)

Example component 1 with recompose:

import { compose, withHandlers, withState } from 'recompose';

const WithToggle = compose(
    withState('toggledOn', 'toggle', false),
    withHandlers({
      show: ({ toggle }) => () => toggle(true),
      hide: ({ toggle }) => () => toggle(false),
      toggle: ({ toggle }) => () => toggle((current) => !current)
    })
);

const Simple = WithToggle({ show,hide,toggle}) =><div></div>

Example component 2

export const AnotherHOC = ComposedComponent => class extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      extraStuff: true
    };
  }

  render() {
    return <ComposedComponent {...this.props} extraStuff={this.state.extraStuff}/>;
  }
};

const Simple = AnotherHOC ({ extraStuff}) =><div></div>

Closing as v0.3.1 should support multiple components in single file and should work better with HOC. Feel free to re-open if needed.