Skip to content
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

Merged

Conversation

cherniavskii
Copy link
Member

@cherniavskii cherniavskii commented Jan 12, 2022

expect(getColumnHeadersTextContent()).to.deep.equal(['id']);

fireEvent.click(getAllByLabelText('Menu')[0]);
fireEvent.click(getByRole('menuitem', { name: 'Hide' }));
Copy link
Member Author

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:

  1. Click Trigger .click() on ButtonBase imperatively
  2. Nothing happens
  3. Click Change ButtonBase component to "li"
  4. Click Trigger .click() on ButtonBase imperatively again
  5. onClick is called, even though ButtonBase is disabled

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.

Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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 👍

Copy link
Member

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.

Copy link
Member Author

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

@cherniavskii cherniavskii requested a review from a team January 12, 2022 18:15
@cherniavskii cherniavskii added the component: data grid This is the name of the generic UI component, not the React module! label Jan 12, 2022
* 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');
Copy link
Member

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.

Suggested change
expect(hideButton.getAttribute('aria-disabled')).to.equal('true');
expect(hideButton).to.have.attribute('aria-disabled', 'true');

Copy link
Member Author

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

Copy link
Member

@m4theushw m4theushw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Unable to show columns again after all columns were hidden
4 participants