question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setState for connected components.

See original GitHub issue

Browser.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:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dabit1commented, Feb 1, 2018

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:

const wrapper = mount(shallow(<MyComponent />).get(0))

Works like a charm 😃

1reaction
ljharbcommented, May 17, 2017

Shallow-render the wrapper, and use .dive() - then you have an enzyme wrapper around the wrapped component.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found