Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Props not validated when stateless component is called as a function #107

@presence1

Description

@presence1

If I have a stateless functional component such as this:

const SlideView = ({text}) => {   
...
 return (
        <div className="example-slide-view">
             {text}            
        </div>
    );
};

SlideView.propTypes = {
    text: PropTypes.string.isRequired
};
...

If I test it like this:

context('SlideView...', () => {
            context('when called without a "text" prop', () => {
                shallow(<SlideView text={undefined} />);
                it('should throw a missing prop error', () => {
                    testUtils.expectMissingPropError('text', 'SlideView');
                });
            });
        });

The test passes.

However, if instead I test the SlideView like this:

context('SlideView...', () => {
            context('when called without a "text" prop', () => {
               SlideView({text: undefined});
               it('should throw a missing prop error', () => {
                    testUtils.expectMissingPropError('text', 'SlideView');
                });
            });
        });

No prop-types error is logged to the console, so the test fails.

I didn't expect this behaviour. I think it would be useful if the props were validated in this case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions