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

shallow wrapper instance is null #2277

Open
2 tasks
gegham1 opened this issue Oct 31, 2019 · 1 comment
Open
2 tasks

shallow wrapper instance is null #2277

gegham1 opened this issue Oct 31, 2019 · 1 comment

Comments

@gegham1
Copy link

gegham1 commented Oct 31, 2019

import React from 'react';
import { BrowserRouter } from "react-router-dom";
import { Provider } from "react-redux";
import { shallow } from 'enzyme';
import renderer from "react-test-renderer";

import Logout from './Logout';
import mockStore from "../../../../test/mockStoreHelper";

describe('<Logout />', () => {
    const defaultProps = {
        actions: { signOut: jest.fn() },
        history: { push: jest.fn() },
    };
    const renderShallow = (props = {}) =>
        shallow((
            <Provider store={mockStore}>
                <BrowserRouter basename={'/'}>
                    <Logout {...defaultProps} {...props} />
                </BrowserRouter>
            </Provider>
        ));

    it('renders', () => {
        const jsonTree = renderer
            .create(renderShallow())
            .toJSON();

        expect(jsonTree).toMatchSnapshot();
    });

    it('checks redirectLogin', () => {
        const wrapper = renderShallow();
        const instance = wrapper.instance();

        instance.redirectLogin();

        expect(defaultProps.actions.signOut).toBeCalled();
        expect(defaultProps.history.push).toBeCalledWith('/login');
    });
});

Current behavior

I'm getting this error - TypeError: Cannot read property 'redirectLogin' of null
Instance is always null

Your environment

Windows

API

  • shallow

Version

library version
enzyme 3.9.0
enzyme-adapter-react-16 1.12.1
react 16.8.4
react-dom 16.8.4
react-test-renderer 16.8.6

Adapter

  • enzyme-adapter-react-16
@ljharb
Copy link
Member

ljharb commented Nov 1, 2019

Provider is probably an SFC, which don't have instances.

However, try upgrading to enzyme v3.10, and then use the wrappingComponent options to render Provider and BrowserRouter - then see what wrapper.debug() and wrapper.instance() is.

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

2 participants