setState for connected components.
See original GitHub issueBrowser.tsx
export class UBrowserLoader extends React.Component<IProps, IState> {
public state = {
loading: true;
}
public render() {
const {errorState} = this.props;
if (errorState) {
// some code
return <div className="error"></div>
}
if (this.state.loading) {
// some code
return <div className="loading"></div>.
}
return <div className="browser-body">Hello</div>
}
export const BrowserLoader = connect(mapStateToProps, mapDispatchToProps)(UBrowserLoader)
My tests look like this
describe("Browser tests", function() {
test("test the browser loader for error case", () => {
const mockStore = configureMockStore([thunk]);
const store = mockStore({
errorState: {
error: "internal error"
}
});
const wrapper = mount(<Provider store={store}><BrowserLoader/></Provider>);
expect(wrapper.find('.error')).toHaveLength(1);
expect(wrapper.find('.browser-body')).toHaveLength(0);
});
test("tests the loader on the browser", () => {
const mockStore = configureMockStore([thunk]);
const store = mockStore({errorState: {}});
const wrapper = mount(<Provider store={store}><BrowserLoader/></Provider>);
expect(wrapper.find('.loading')).toHaveLength(1); // this works
expect(wrapper.find('.error')).toHaveLength(1); // this works
wrapper.setState({loading: false})
expect(wrapper.find('.loading')).toHaveLength(0); // does not work!!!!! it does not setState on the child component
How do I get this to work? Any ideas? Any help will be appreciated!
cc @lelandrichardson - I saw your comments regarding this on another github issue - where you said to test the flux cycle one can use mount. But I don’t understand how to do it in this case.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Set state of redux connected react component using enzyme
I tried updating using wrapper.setState({displayMenu:true}) but it was not updated because I think wrapper is wrap of component with Provider, ...
Read more >Connected Components and Performance - Practical Redux
setState () , the entire component tree will re-render. It's very likely that the majority of components in the tree would receive the...
Read more >Connect: Extracting Data with mapStateToProps - React Redux
It's frequently referred to as just mapState for short. It is called every time the store state changes.
Read more >Lab 25: Redux with React | Hands on React
Refactor the Page (container) component to be a Redux connected component ... Replace setState and API calls with calls to action creators passed...
Read more >React Redux connect(): When and how to use it
React provides two major mechanisms for providing data to components, props and state. Props are read-only and allow a parent component to pass ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I finally found a very good solution to get a wrapper from a decorated component. For shallowed components you can use
dive()
but there is no similar method for mounted components. This is the solution I did:Works like a charm 😃
Shallow-render the wrapper, and use
.dive()
- then you have an enzyme wrapper around the wrapped component.