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
?
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.
Most helpful comment
It looks like
MyCustomComponent
is probably wrapped in a HOC likeProvider
. If so, you likley need tofind
the component it's wrapping which you're trying to set state on.