-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simulating click does not honor the disabled attribute #386
Comments
@djskinner your second test uses |
My bad. |
Thank for verifying! |
I think it is again active :) |
I can confirm this. |
Any chance this is reproducible in v16.2.0.? When I simulate click on disabled button I`m expecting (fn.mock.calls.length).toBe(0); but with shallow I get 2, with mount - 1. Seems it still calls the function. |
I'm noticing this bug with react 15.5.4 + enzyme 3.1.0, specifically with a shallow component. For reference, the component has 3 buttons in it, I'm finding them with wrapper.find('button') with has a length 3, and then attempting to simulate a click with: const fooMock = jest.fn();
const wrapper = shallow(<MyComponent foo={fooMock} />); // the button, when clicked, should call foo();
console.log(wrapper.find('button').at(0).getProps().disabled); // outputs 'disabled', as expected
wrapper.find('button').at(0).simulate('click');
expect(fooMock).toHaveBeenCalledTimes(0); And the test fails, because fooMock was called once - despite the button being disabled. Is there any way around this? |
@zachfejes react 15 only goes up to 15.6; there's no 15.9?
|
@ljharb thanks for the response - I've corrected the version number in my comment to clarify, react 15.5.4. |
if someone decided to copy-paste code from @zachfejes here is working
|
Hi, I am facing the same issue as @zachfejes. const onSubmitSpy = sinon.spy();
const mountedWrapper = mount(
<Provider store={store}>
<reduxForm {...props} handleSubmit={onSubmitSpy} />
</Provider>
);
mountedWrapper.find("button").prop("onClick")();
expect(mountedWrapper.find("button").prop("disabled")).toBe(true); //true
expect(onSubmitSpy.called).toBe(true); // true |
Hi! Any updates on this issue? |
Looks like the issue still exists for version |
@mattvalli the plan is in v4, to remove simulate entirely. It's a bad API, and it doesn't faithfully simulate anything. Nobody should be using |
@ljharb So how do you simulate a click event (or any event) in v4? |
You don't simulate a click event - that'd be testing react itself, or the browser, and that's not your purview. You assert that the right thing ended up in |
@ljharb can you show an example of this? I can do the latter part, but not sure how to assert that if the onClick fn is just a local fn in my functional component. I guess I'd have to expose overriding it as a prop |
@butterywombat in your example, what does that local function do? You can extract it with |
So what's the issue about nor? The disabled not being honoured on shallow, on mount or on both? It works on mount for me but not for shallow, not sure if that still is the desired case. |
If there's a difference between shallow and mount, we should fix shallow to match mount whenever possible. If someone can provide a PR, or a link to a branch/commit, that includes a test case that passes for mount and fails for shallow, that would make it much easier to work on a fix. |
There seems to be a difference in behaviour between shallow/mount and react-addons-test-utils when it comes to simulating a click on an element with the
disabled
attribute. React and react-addons-test-utils does not call the onClick handler when the disabled attribute is present but shallow/mount does.Component under test:
All tests pass using
react-addons-test-utils
:Using
shallow
the second test will fail:Using
mount
the second test will fail:The text was updated successfully, but these errors were encountered: