Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get state for child components connected to redux with enzyme

I am trying to test a redux connected component. The component changes its properties on a resize event.

I want to mount my DatePicker component with enzyme, dispatch a resize event, and test that my DatePicker's prop value IS_DESKTOP_VIEWPORT === true and because of this, my DatePicker's state has changed as well.

However I cannot access the state of my DatePicker because enzyme does not let you access the state if the wrapper component is not the root:

console.log(wrapper.find('DatePicker').state())
// ReactWrapper::state() can only be called on the root

I've tried wrapping the connected DatePicker in a Provider as the docs suggest, as well as mounting it directly but passing store as a prop. Neither of these methods seem to allow me to refer to DatePicker as the root component and thus neither of these methods allow me to get the state of my component.

Here are my attempts: The output of these console logs is here: https://gist.github.com/gecko25/56fb14154585a2d06697c399685c9111

import React from 'react';
import { Provider } from 'react-redux';
import PropTypes from 'prop-types';
import { mount } from 'enzyme';
import configureDataStore from '%SRC%/store/configure';
import { windowResize } from '%CONSTANTS%/store/actions';
import  ConnectedDatePicker, { DatePicker }  from './DatePicker';

const DESKTOP = 1025;

describe('calendar open and closes', () => {

    test('connected attempt #1', ()=>{

        const store = configureDataStore();

        const options = {
          context: { store },
          childContextTypes: { store: PropTypes.object }
        }

        const wrapper = mount(<ConnectedDatePicker store={store}/>)

        store.dispatch(windowResize(DESKTOP));

        console.log('state-->', wrapper.state()) // {}
        console.log('props-->', wrapper.props()) // Doesn't include all my component specific props
        console.log(wrapper.find('DatePicker').props()) // Prints all props as expected
        console.log(wrapper.find('DatePicker').state()) // Error

        console.log('--------------------------')

    })

    test('connected attempt #2', () => {

        const store = configureDataStore();

        const options = {
          context: { store },
          childContextTypes: { store: PropTypes.object }
        }

        const wrapper = mount(<ConnectedDatePicker/>, options)

        store.dispatch(windowResize(DESKTOP));


        console.log('state-->', wrapper.state()) // {}
        console.log('props-->', wrapper.props()) // Doesn't include all my component specific props
        console.log(wrapper.find('DatePicker').props()) // Prints all props as expected
        console.log(wrapper.find('DatePicker').state()) // Error

    });

    test('connected attempt #3', () => {

        const store = configureDataStore();

        const wrapper = mount(<Provider store={store}><ConnectedDatePicker/></Provider>)

        store.dispatch(windowResize(DESKTOP));

        console.log('state-->', wrapper.state()) // null
        console.log('props-->', wrapper.props()); // Doesn't include all my component specific props
        console.log(wrapper.find('DatePicker').props()) // Prints all props as expected
        console.log(wrapper.find('DatePicker').state()) // Error
        console.log('--------------------------')

    });
});
like image 243
gecko25 Avatar asked Mar 08 '23 20:03

gecko25


1 Answers

wrapper.find('DatePicker').instance().state should work for you :)

like image 200
Divyansh jaiswal Avatar answered Apr 09 '23 15:04

Divyansh jaiswal