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

[test] Warn when focusing cells without syncing the state #3486

Merged
merged 10 commits into from Jan 25, 2022

Conversation

m4theushw
Copy link
Member

@m4theushw m4theushw commented Dec 21, 2021

Previously, every time we had to fireEvent.keyDown in a cell, we would usually focus the cell with cell.focus() first. This approach has the problem that it doesn't sync the focus state, because the grid doesn't listen to the focus event, but only mouseup and keyup. For some simple test cases this is not a problem, however, if the focus moves from one cell to another this might cause an unexpected behavior. Take as example the test I had to modify in #3484:

const { setProps } = render(<NavigationTestCaseNoScrollX />);

getCell(8, 1).focus();
expect(getActiveCell()).to.equal('8-1');
fireEvent.keyDown(document.activeElement!, { key: 'End', ctrlKey: true });
expect(getActiveCell()).to.equal('9-2'); // The focused cell according to the state is 9-2

getCell(8, 1).focus(); // Try to focus another cell
expect(getActiveCell()).to.equal('8-1');
setProps({ foo: 'bar' }); // Simulates a rerender
expect(getActiveCell()).to.equal('8-1'); // It fails

Above we're navigating with the keyboard to the cell 9-2. After the keyboard event, the state is correctly updated to store that 9-2 is the focused cell. The problem comes if we try to focus another cell. For a brief moment it works, but if a rerender occurs in the meantime, the focus gets reset to the value in the state. This PR shows a warning every time that a cell receives focus while the state is not correctly synced. To fix the test above, we can drop cell.focus() and simulate a click:

-cell.focus();
+fireEvent.mouseUp(cell);
+fireEvent.click(cell);

If you also need to ensure that a row is not selected when clicking, use disableSelectionOnClick or first focus on a column header and navigate with the arrow keys to the desired cell. The alternative to this PR would be to update the focus on the focus event, but I don't know if it's worth it to change this now. The focus is always preceded by a click or keyboard event and we already use these events.

@m4theushw m4theushw added the test label Dec 21, 2021
@m4theushw m4theushw self-assigned this Dec 21, 2021
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 23, 2021
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 24, 2021
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Dec 29, 2021
@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Jan 3, 2022
@github-actions
Copy link

github-actions bot commented Jan 5, 2022

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Jan 5, 2022
@github-actions
Copy link

github-actions bot commented Jan 7, 2022

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot added PR: out-of-date The pull request has merge conflicts and can't be merged and removed PR: out-of-date The pull request has merge conflicts and can't be merged labels Jan 8, 2022
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 17, 2022
@mui-bot
Copy link

mui-bot commented Jan 17, 2022

These are the results for the performance tests:

Test case Unit Min Max Median Mean σ
Filter 100k rows ms 192.5 364.4 264.3 270.04 59.704
Sort 100k rows ms 401.9 822.7 696.7 645.42 141.224
Select 100k rows ms 130.1 296.9 186.1 209.86 69.354
Deselect 100k rows ms 118.7 191 135.1 148.66 26.088

Generated by 🚫 dangerJS against d38d69a

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 19, 2022
@github-actions
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Jan 19, 2022
Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

A big thanks for this PR, it explains why I was struggling to write tests on cell focus yesterday 😅

Comment on lines 327 to 328
fireEvent.mouseUp(cell);
fireEvent.click(cell);
Copy link
Member

Choose a reason for hiding this comment

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

why not fireClickEvent(cell)?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could use mousePress utility method from the core

Copy link
Member Author

Choose a reason for hiding this comment

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

mousePress doesn't work. Some tests fail but I don't know why.

@@ -104,6 +104,7 @@ describe('<DataGridPro /> - Edit Components', () => {
);

const cell = getCell(0, 0);
fireEvent.mouseUp(cell);
Copy link
Member

Choose a reason for hiding this comment

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

Is it necessary?
In cellEditing.DataGridPro.test.tsx you remove all the cell.focus() but do not add fireEvent.mouseUp()

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, even though it's a double click, it uses the cell that received mouseUp to update the state. Below, cellParams is assigned by the mouseUp listener. If we don't fire it, the focus is never updated.

https://github.com/mui-org/material-ui-x/blob/9285a97a5e19eca7d06cb8f3d06e0ee1a1f05823/packages/grid/_modules_/grid/hooks/features/focus/useGridFocus.ts#L167-L168

@m4theushw m4theushw merged commit dc59fd5 into mui:master Jan 25, 2022
@m4theushw m4theushw deleted the warn-focus-out-of-sync branch January 25, 2022 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants