question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to test render prop with mount?

See original GitHub issue

Hey guys,

I was wondering how one should test a render prop using mount? The reason for that is because I’m using Apollo and trying to test a render prop that is wrapping Apollo Mutation render prop. I’m trying to test the loading state, but I cannot figure out how to pass properties to AuthRender and I believe that I must use mount since Apollo used component lifecycles behind the scenes. Any help would be highly appreciated. Thanks.

The test

        it('renders loading state initially', done => {
            const mocks = [
                {
                    request: {
                        query: LOGIN_MUTATION,
                        variables: { email: 'test@gmail.com', password: 'password' }
                    },
                    result: { data: registerUser }
                }
            ];

            wrap = mount(
                <MockedProvider mocks={mocks} addTypename={false}>
                    <LoginPage />
                </MockedProvider>
            );

            // How do I access AuthRender and pass props?

            expect(wrap.find(LoadingBar)).toHaveLength(1);

            done();
        });

The component I’m trying to test.

class Login extends Component {
    validateInputs(
        { email, password }: { email?: string, password?: string }
    ): string | null {
        if (!email || !password) {
            return UNFILLED_FIELDS_ERROR;
        }
        return null;
    };

    render() {
        return (
            <AuthRender>
                {({ handleSubmit, onInputChange, errorMessage, auth, setErrorMessage }: InjectedProps) => {
                    return (
                        <Layout title={'Login'}>
                            <Mutation mutation={LOGIN_MUTATION} onError={() => { }}>
                                {(mutate: any, { loading, error, data }: any) => {
                                    if (!error && data) {
                                        Router.push('/');
                                    }
                                    console.log('properties', data, loading);

                                    return (
                                        <div className="form-container d-flex flex-column justify-content-center">
                                            {loading && <LoadingBar />}
                                            {error && <Alert message={error.graphQLErrors[0].message} />}
                                            {errorMessage && <Alert message={errorMessage} />}
                                            <div className="text-center heading heading-large">Login</div>
                                            <div className="row d-flex justify-content-center">
                                                <form
                                                    className="d-flex flex-column col-10 col-sm-8 col-md-3"
                                                    onSubmit={handleSubmit.bind(
                                                        this, {
                                                            auth,
                                                            mutate,
                                                            validateInputs: this.validateInputs,
                                                            setErrorMessage
                                                        }
                                                    )}
                                                >
                                                    <InputField
                                                        name={'email'}
                                                        labelTitle={'Email'}
                                                        placeholderTitle={'Enter your email'}
                                                        onInputChange={(e: ChangeEvent<HTMLInputElement>) =>
                                                            onInputChange(e)
                                                        }
                                                    />
                                                    <InputField
                                                        name={'password'}
                                                        labelTitle={'Password'}
                                                        type="password"
                                                        placeholderTitle={'Enter your password'}
                                                        onInputChange={(e: ChangeEvent<HTMLInputElement>) =>
                                                            onInputChange(e)
                                                        }
                                                    />
                                                    <ButtonPrimary title={'Login'} />
                                                </form>
                                            </div>
                                        </div>
                                    );
                                }}
                            </Mutation>
                        </Layout>
                    )
                }}
            </AuthRender>
        );
    }
}

export default withAuthPages(Login);

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wwselleckcommented, Nov 15, 2019

I’m running into this as well, renderProp definitely exists on ReactWrapper, but the types only include it for ShallowWrapper. Where’s the best place to surface this to get it fixed @ljharb ?https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/enzyme/index.d.ts#L490

0reactions
ljharbcommented, Jun 2, 2019

Again, that’s a bug in the DT types for enzyme - something you should bypass in order to get your actual code working.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing ⚛️ components using render props - Kent C. Dodds
Let's look at how we can write tests for React components that use render props!
Read more >
Unit test a React Render Prop component - Egghead.io
[00:36] The first thing I'm going to do is test that the render prop function gets called on mount. I'll use Jest to...
Read more >
How to render elements inside render prop while testing with ...
There are two solutions: Use wrapper.dive() or wrapper.shallow() that would cause your ShallowWrapper to render child of your top-level ...
Read more >
Test a Render Prop!. aka the missing paragraphs of “Use a…
Update (2018–09–13): This article goes into the details of how to test render props in Enzyme. I've since moved away from Enzyme as...
Read more >
Test Renderer - React
import TestRenderer from 'react-test-renderer'; function Link(props) { return <a ... the tree will be updated; otherwise, it will re-mount a new tree.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found