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

State doesn't get updated even when I do component.update() after simulating #1570

Closed
8 tasks
smoholkar opened this issue Mar 12, 2018 · 11 comments
Closed
8 tasks

Comments

@smoholkar
Copy link

smoholkar commented Mar 12, 2018

While migrating existing v2 tests.
Seems to be simulating correctly but I dont see the state updated correctly.
Am I missing something ?

const wrapper = mount(<TestComponent {...properties} />);
wrapper.setState({
   focusedColumn: 4
});
wrapper.find('div.base-columns').simulate('mouseleave');
wrapper.update();
expect(wrapper.state('focusedColumn')).toEqual(null); // equals 4 instead 

Similar thing is happening in this case

const wrapper = mount(<TestComponent {...properties} />);
 wrapper.setState({
     focusedColumn: 1
 });
 wrapper.instance().changeFocus({ col: 2 });
// ideally this should've updated the state. IDK
 wrapper.update();
 expect(wrapper.state('focusedColumn')).toEqual(2);

Same here

 const component = mount(<XYZComponent {...properties} />);
 const captionIcon = component.find('.xyz-caption > i');
 captionIcon.simulate('click');
 component.instance().forceUpdate();
  component.update();
 expect(captionIcon.prop('className')).toBe('correctclass'); // but I get the incorrect class coz update isn't updating it . 

API

  • shallow
  • [ X] mount
  • render

Version

library version
Enzyme latest
React 15.5.4

Adapter

  • enzyme-adapter-react-16
  • [X ] enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )
@smoholkar smoholkar changed the title State doesn't get updated even when I do component.update() after simulating mouseleave State doesn't get updated even when I do component.update() after simulating Mar 12, 2018
@koba04
Copy link
Contributor

koba04 commented Mar 13, 2018

@smoholkar In my environment, the state is updated correctly.
Could you create a repository to reproduce it?

@Sinha06
Copy link

Sinha06 commented May 30, 2018

I find similar issue with where i'm not able to get the state value updated:

import React from 'react';
import { connect } from 'react-redux';
import {bindActionCreators} from 'redux';

import * as getData from '../../services/DataService';

class Container extends React.Component {

    constructor(props){
        super(props) 
        this.props.actions.getTemplates(1);
        this.state = {
            value: 1
        }

    }

    handlelistOfTemplates = (value, index) => {
        this.setState({ value });
    };

    componentDidMount() {
    }

    render() {
        return(

                <ListOfTemplates listOfTemplates={this.props.listOfTemplates} value={this.state.value} onChange={this.handlelistOfTemplates}/>
            );
    }
}
function mapStateToProps({state}) {
    return {
        listOfTemplates: state.listOfTemplates
    }
}
function mapDispatchToProps(dispatch) {
    return {
    actions: bindActionCreators(getData, dispatch)
    };
   }
module.exports = connect(mapStateToProps, mapDispatchToProps)(Container);

And its test :

import React from 'react';
import sinon from 'sinon';
import expect from 'expect';
import { shallow } from 'enzyme';
import PropTypes from 'prop-types';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import { createMockStore, createMockDispatch } from 'redux-test-utils';

import Container from './Container';
const shallowWithStore = (component, store) => {
    const context = {
        store,
        muiTheme: getMuiTheme(),
    };

    const childContextTypes = {
        muiTheme: React.PropTypes.object.isRequired,
        store: PropTypes.object.isRequired
    }
    return shallow(component, { context, childContextTypes });
};
let store;
const loadComponent = (testState) => {

    const props = {
        actions: {
            getTemplates: () => {return Promise.resolve()}
        }
    }
    store = createMockStore(testState)
    return shallowWithStore(<Container {...props}/>, store);
}

const getFakeState = () => {
    return {
        listOfTemplates: [],

    };
}


export default shallowWithStore;

describe('Container', () => {
    let testState, component;


    describe("when Appeal template is selected from select template dropdown", () => {
        beforeAll(() => {
            testState = getFakeState();
            component = loadComponent(testState);

        });

        fit('should update the content in editor', (done) => {
            component.dive().find('ListOfTemplates').props().onChange(2, 1);
            component.dive().instance().forceUpdate();
            component.dive().update();

            expect(component.dive().state().value).toEqual(2); // it return 1 instead of 2
            done();

        });
    });
});

Am i missing something?

@ljharb
Copy link
Member

ljharb commented Jul 7, 2018

@Sinha06 every time you call .dive() you create a new wrapper. You'll need to do const wrapper = component.dive(), and then all all those methods on wrapper instead.

@ljharb
Copy link
Member

ljharb commented Jul 7, 2018

Happy to reopen if there's a need.

@venikx

This comment has been minimized.

@koba04

This comment has been minimized.

@venikx

This comment has been minimized.

@koba04

This comment has been minimized.

@venikx

This comment has been minimized.

@ArthurNameless
Copy link

@Sinha06 every time you call .dive() you create a new wrapper. You'll need to do const wrapper = component.dive(), and then all all those methods on wrapper instead.

You are damn genius!

@layagollapudi
Copy link

Hi, as per @smoholkar question from a while back, I'm facing a similar issue when mounting my component for testing as well. I can't seem to find anyone else other than this post who is facing the issue of states not updating other than this post - is there a solution that I'm missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants