0
I created a component Button
which has a function onClick
and a prop disabled
. The function defines a state isLoading
as true
, and prop receives the value of this state, that is, while the page is pressing the button will be disabled.
However, when simulating the click, the state and prop are not updated, so I can’t test to see if the button has actually been disabled.
index.jsx
<Button
onClick={handleClick}
data-testid="backward-button"
disabled={this.state.isLoading}
/>
index.testjs.
const wrapper = shallow(<ParentComponent />);
const button = wrapper.find('[data-testid="backward-button"]');
button.simulate('click');
wrapper.instance().forceUpdate();
expect(button.prop('disabled')).toBeTruthy();
The question is: how can I verify the value of the prop disabled
after the state is changed?
I’m using Jest and Enzyme for the tests.
Did not work this test? presents some error?
– novic
expect(button.prop('disabled')).toBe(true);
have tried?– novic
I hadn’t tried yet, thanks for the help !
– Pedro
Yes, he says the test failed, That way you propose it didn’t work, it keeps giving false
– Pedro
It is not that it failed is that the state of the component has not changed, so it must something in this type ... failure in this case is not the problem, the problem you want a component change that is not occurring.
expect(button.prop('disabled')).toBe(false);
placefalse
to see if it happens!– novic