-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[DataGrid] Disable Hide button if there's only one visible column #3607
[DataGrid] Disable Hide button if there's only one visible column #3607
Conversation
expect(getColumnHeadersTextContent()).to.deep.equal(['id']); | ||
|
||
fireEvent.click(getAllByLabelText('Menu')[0]); | ||
fireEvent.click(getByRole('menuitem', { name: 'Hide' })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm struggling to make this test pass.
When clicking manually in the browser, disabled Hide
button doesn't trigger click
event, but in this test it does.
Hide
button is a ButtonBase
component rendered as a li
element.
Since it's not a button
, it doesn't have a disabled
HTML attribute when disabled. Instead, it has aria-disabled
+ pointer-events: none
.
I've noticed that imperative .click()
called on disabled li
ButtonBase
does trigger click
as well.
I believe same happens in tests.
Here's a minimal reproduction: https://codesandbox.io/s/disabled-buttonbase-click-c91hx?file=/src/App.js
Steps to reproduce:
- Click
Trigger .click() on ButtonBase imperatively
- Nothing happens
- Click
Change ButtonBase component to "li"
- Click
Trigger .click() on ButtonBase imperatively
again - onClick is called, even though
ButtonBase
isdisabled
I'm not sure if we should consider this as a bug.
Any ideas how to make the test pass?
I've tried to use userEvent.touch
instead of fireEvent.click
, but it doesn't feel right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it seems the problem is from ButtonBase
, the easiest way is probably to not test by click
but only check that the button has aria-disabled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexfauquette Thanks for looking into it!
the easiest way is probably to not test by click but only check that the button has aria-disabled
The downside I see here is that aria-disabled
is an implementation detail, so if we change from li
to button
the test will fail.
Also, in the test we trust that if aria-disabled
is present, onClick won't be triggered.
It would do the job for now though, so I think it's fine. Will add a comment explaining the decision.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a comment to explain the decision is a good idea 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know if the test only fails in JSDOM? It could be some limitation of JSDOM. Tests running with yarn test:karma
use a real browser so it should behave in the same way as when manually clicking. Another thing you could do is to check if the button is disabled inside the onClick
handler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know if the test only fails in JSDOM?
It fails in test:karma
Another thing you could do is to check if the button is disabled inside the onClick handler
Yeah, that would allow to keep fireEvent.click
in the test
packages/grid/x-data-grid/src/tests/columnHeaders.DataGrid.test.tsx
Outdated
Show resolved
Hide resolved
packages/grid/x-data-grid/src/tests/columnHeaders.DataGrid.test.tsx
Outdated
Show resolved
Hide resolved
* It doesn't happen with user click in browser. | ||
* So instead of firing click event, we only check `aria-disabled` here. | ||
*/ | ||
expect(hideButton.getAttribute('aria-disabled')).to.equal('true'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It gives a more friendly message when it fails.
expect(hideButton.getAttribute('aria-disabled')).to.equal('true'); | |
expect(hideButton).to.have.attribute('aria-disabled', 'true'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, turns out the message is not friendly at all when it fails:
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'HTMLLIElement'
| property '__reactFiber$x0ublx5qav' -> object with constructor 'FiberNode'
--- property 'stateNode' closes the circle
at JSON.stringify (<anonymous>)
at processAssertionError (node_modules/karma-mocha/lib/adapter.js:59:41)
at Runner.<anonymous> (node_modules/karma-mocha/lib/adapter.js:164:28)
I tried another test in the codebase that uses this notation and made it fail - got the same output.
For example here:
https://github.com/mui-org/material-ui-x/blob/7cc6c3c68e139836fec65505254a311534f1e929/packages/grid/x-data-grid/src/tests/selection.DataGrid.test.tsx#L327
If assertion fails - you'll see TypeError: Converting circular structure to JSON
Looks like an issue with mocha, I'll look into it later
packages/grid/_modules_/grid/components/menu/columnMenu/HideGridColMenuItem.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Closes #2718
Preview: https://deploy-preview-3607--material-ui-x.netlify.app/components/data-grid/
Hide.mov