enzymejs / enzyme

JavaScript Testing utilities for React
https://enzymejs.github.io/enzyme/
MIT License
19.95k stars 2.01k forks source link

shallow wrapper instance is null #2277

Open gegham1 opened 5 years ago

gegham1 commented 5 years ago
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

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

ljharb commented 5 years ago

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.