Enzyme: `setState` not causing `render` or updating the state with `mount` and `react-native`

Created on 4 Apr 2017  路  3Comments  路  Source: enzymejs/enzyme

In my tests suit I have a component that is mounted using mount and I am trying to update the state with the setState and it is not updating the component.

...
import configureStore from 'redux-mock-store';

const middlewares = [];
const mockStore = configureStore(middlewares);
const initialState = {};

describe('<MyCustomComponent /> (browser)', () => {
  let component;

  beforeEach(() => {
    component = mount(<MyCustomComponent />, {
      context: {
        store: mockStore(initialState)
      },
    });
  });

  describe('render', () => {
    it('should render the MyCustomComponent', () => {
      component.setState({ items: ['foo'] });

      component.render();

      expect(component.find('MyCustomComponent')).to.have.length(1);
    });
  });
});

If I also call component.instance(...).onMyMethod I get the following error: TypeError: component.instance(...).onMyMethod is not a function.

Any thoughts on how to fix this? Or what is causing this issue? Is this somehow related with redux-mock-store?

question

Most helpful comment

It looks like MyCustomComponent is probably wrapped in a HOC like Provider. If so, you likley need to find the component it's wrapping which you're trying to set state on.

All 3 comments

It looks like MyCustomComponent is probably wrapped in a HOC like Provider. If so, you likley need to find the component it's wrapping which you're trying to set state on.

(or .dive() to it)

@aweary and @ljharb thanks for the help. Using .dive() and cons componentUpdate = component.setState... works.

Was this page helpful?
0 / 5 - 0 ratings