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 does not set state

See original GitHub issue

Describe the bug A clear and concise description of what the bug is.

I’ve setup versions enzyme 3.5.0 and enzyme-adapter-react-16 1.3.0. I’ve created an enzyme test in this commit https://github.com/polkadot-js/apps/pull/265/commits/0d048c094b91762ac6a7812f5d4c5899b71b5af5 that mounts the component that passes. If I run a test with expect(wrapper.get(0)).toBe(1); it will show me that the component includes the correct props along with provided fixtures <Signer queue={[{"id": "test", "rpc": {"isSigned": true}, "status": "test"}]} t={[Function mockT]} />. So far the test I’ve written that checks expect(wrapper).toHaveLength(1); is passing successfully. However, I want to run a test to check that the <Modal className='ui--signer-Signer' ... (see https://github.com/polkadot-js/apps/blob/master/packages/ui-signer/src/Modal.tsx#L89) renders correctly, but it only renders when this.state.currentItem and this.state.currentItem.rpc.isSigned (see https://github.com/polkadot-js/apps/blob/master/packages/ui-signer/src/Modal.tsx#L83) are defined and true. So in the commit I created fixtures for the the currentItem state value, and wrote the following test to set the state with setState, update the component, and then check that the state has changed. But it doesn’t appear to work, because the test results report that currentItem is still undefined instead of being the value I set to the fixtureCurrentItemState variable.

  it('sets the state of the component', () => {
    wrapper.setState({ currentItem: fixtureCurrentItemState });
    wrapper = wrapper.update(); // immutable usage
    expect(wrapper.state('currentItem')).toEqual(fixtureCurrentItemState);
    console.log(wrapper.debug());
  });

Note that I tried debugging with console.log(wrapper.debug()); and console.log(wrapper.html());, which I’ve used in the past without any issues, but neither of them output anything, so as an alternative I was able to check the state by running expect(wrapper.state()).toEqual(1);, which returned {"currentItem": undefined, "password": "", "unlockError": null}

To Reproduce Steps to reproduce the behavior:

  1. Go to https://github.com/polkadot-js/apps
  2. Clone Pull Request https://github.com/polkadot-js/apps/pull/265
  3. Install dependencies and run the tests with yarn; yarn run build; yarn run test;
  4. Remove .skip from the above mentioned tests
  5. See the failing test

Expected behavior I expected setState to set the state

Screenshots Code used screenshot: screen shot 2018-08-27 at 9 36 33 am

Failing test screenshot: screen shot 2018-08-27 at 9 38 47 am

Desktop (please complete the following information):

  • OS: macOS
  • Browser: Chrome
  • Version 68.0.3440.106

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:26 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
comfroelscommented, Aug 31, 2018

Yeah I seem to be running into this same issue. I have an instance method that sets state, so I run the instance method and ensure the the correct state gets set:

wrapper.instance().reviewApp({ uid: 'some_id' });
expect(History.push).toHaveBeenCalledWith('/mobile/apps/detail/some_id');
expect(wrapper.state('adcOpen')).toBeTruthy(); //is false

Enzyme 3.5.0 enzyme-adapter-react-16 1.3.1

I changed it to manually call setState on the wrapper, but the state still isn’t set.

wrapper.setState({adcOpen: true});
expect(History.push).toHaveBeenCalledWith('/mobile/apps/detail/some_id');
expect(wrapper.state('adcOpen')).toBeTruthy(); //Still false

This still doesn’t set the state correctly. I even tried it with the callback on setState and still no-go.

wrapper.setState({adcOpen: true}, () => {
  expect(History.push).toHaveBeenCalledWith('/mobile/apps/detail/some_id');
  expect(wrapper.state('adcOpen')).toBeTruthy(); //Still false
});

EDIT: Also, I’m on React 16.4.2

5reactions
ljharbcommented, Aug 27, 2018

fwiw, wrapper = wrapper.update(); isn’t required; “immutable” just means you have to re-find from the root.

setState is async; what happens if you do:

wrapper.setState({ currentItem: fixtureCurrentItemState }, () => {
  wrapper.update();
  expect(wrapper.state('currentItem')).toEqual(fixtureCurrentItemState);
  console.log(wrapper.debug());
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

React setState not updating state - Stack Overflow
setState () is usually asynchronous, which means that at the time you console.log the state, it's not updated yet. Try putting the log...
Read more >
React setState does not immediately update the state - Medium
Think of setState() as a request to update the component. Reading state right after calling setState() a potential pitfall. useState React hook.
Read more >
Why my setState doesn't work? - Sergey Stadnik
setState operation is asynchronous in React. If you new state depends on the values of the old state, you should use a functional...
Read more >
Why React doesn't update state immediately - LogRocket Blog
State updates in React are asynchronous; when an update is requested, there is no guarantee that the updates will be made immediately. The ......
Read more >
Why React setState/useState does not update immediately
React this.setState , and useState does not make changes directly to the state object. React this.setState , and React.useState ...
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