[code-infra] Set up eslint-plugin-testing-library#14232
Conversation
|
Deploy preview: https://deploy-preview-14232--material-ui-x.netlify.app/ |
| expect(getCell(7, 1).textContent).to.equal('p71'); | ||
| expect(getCell(7, 3).textContent).to.equal('p73'); | ||
| }); | ||
| expect(getCell(6, 2).textContent).to.equal('p62'); |
There was a problem hiding this comment.
waitFor allows only one expect to be inside of it and it makes sense—anything after it can be outside of the awaited waitFor. 👌
|
|
||
| const getPickerDay = (name: string, picker = 'January 2018') => | ||
| getByRole(screen.getByRole('grid', { name: picker }), 'gridcell', { name }); | ||
| rtlGetByRole(screen.getByRole('grid', { name: picker }), 'gridcell', { name }); |
There was a problem hiding this comment.
Using an aliased method avoids triggering the ESLint error and possibly improves readability. 🤔
There was a problem hiding this comment.
rtl confused me at first, because I thought "Why would we need a different getByRole for Right-to-Left"? 😅
But since rtl stands for React Testing Library, isn't the naming still misleading given that we don't import it from the RTL package?
On a different note, why avoiding ESLint errors here?
There was a problem hiding this comment.
On a different note, why avoiding ESLint errors here?
Would you suggest adding an ESLint ignore comment?
In this case, we want to use a root level getter and provide a specific container for it.
In other words, we are knowingly using the root getter instead of the one provided by screen.
There was a problem hiding this comment.
rtlconfused me at first, because I thought "Why would we need a differentgetByRolefor Right-to-Left"? 😅
But since rtl stands for React Testing Library, isn't the naming still misleading given that we don't import it from the RTL package?
Agreed, I didn't think of rtl like that initially. 🙈
Will alias it as rootGetByRole instead. 👍
There was a problem hiding this comment.
Perhaps use within instead?
- rtlGetByRole(screen.getByRole('grid', { name: picker }), 'gridcell', { name });
+ within(screen.getByRole('grid', { name: picker })).getByRole('gridcell', { name });There was a problem hiding this comment.
@cherniavskii Thanks for the reminder, I completely forgot about within. 👍 🙈 🤷
| it('should clear all the sections when all sections are selected and all sections are completed', () => { | ||
| // Test with v7 input | ||
| const v7Response = renderWithProps({ | ||
| let view = renderWithProps({ |
There was a problem hiding this comment.
Avoids the following error:
`v7Response` is not a recommended name for `render` returned value.
Instead, you should destructure it, or name it using one of: `view`, or `utils`.
eslint(testing-library/render-result-naming-convention)
Destructuring in these cases seemed excessive as we need a lot of methods. 🙈
| getData: () => pastedValue, | ||
| }; | ||
| let canContinue = true; | ||
| act(() => { |
There was a problem hiding this comment.
Only the dispatching of an event needed the act wrapping.
Signed-off-by: Lukas Tyla <llukas.tyla@gmail.com>
| @@ -409,8 +409,8 @@ describe('<DataGridPremium /> - Aggregation', () => { | |||
|
|
|||
| act(() => apiRef.current.showColumnMenu('id')); | |||
There was a problem hiding this comment.
Turning these async can also be done in follow up.
There was a problem hiding this comment.
Do you mean adding an await before act?
The plugin didn't complain about this. 🙈 🤷
There was a problem hiding this comment.
Do you mean adding an await before act?
also making the function async
The plugin didn't complain about this. 🙈 🤷
...yet 😄 testing-library/eslint-plugin-testing-library#915
More info in this issue
There was a problem hiding this comment.
Yeah... Well, it looks like it would be great to get the rule and then update the tests with that rule in action. 🙈
There was a problem hiding this comment.
If that's still the intention, I'd add a @deprecated comment to those exports with a note to use @testing-library/user-event instead.
perhaps with todo comment as well to update the repo to use @testing-library/user-event
There was a problem hiding this comment.
I have deprecated the exports.
I had to go with an index re-export to keep the imports as is and reliably deprecate the exports. 👍
83e9b65
| import { fireEvent } from '@mui/internal-test-utils/createRenderer'; | ||
|
|
||
| function touch(target: Element): void { | ||
| fireEvent.touchStart(target); | ||
| fireEvent.touchEnd(target); | ||
| } | ||
|
|
||
| const mousePress: (...args: Parameters<(typeof fireEvent)['mouseUp']>) => void = ( | ||
| target, | ||
| options, | ||
| ) => { | ||
| fireEvent.mouseDown(target, options); | ||
| fireEvent.mouseUp(target, options); | ||
| fireEvent.click(target, options); | ||
| }; | ||
|
|
||
| function keyPress(target: Element, options: { key: string; [key: string]: any }): void { | ||
| fireEvent.keyDown(target, options); | ||
| fireEvent.keyUp(target, options); | ||
| } | ||
|
|
||
| export const fireUserEvent = { | ||
| touch, | ||
| mousePress, | ||
| keyPress, | ||
| }; |
There was a problem hiding this comment.
Can't we use https://testing-library.com/docs/user-event/convenience?
Otherwise I would rename these to something that is less confusing, like fireCustomEvent or something similar.
There was a problem hiding this comment.
Can't we use https://testing-library.com/docs/user-event/convenience?
That would be the ultimate goal, but after preliminary testing—it's not plug&play, we need more time and effort to migrate tests to using those APIs.
Otherwise I would rename these to something that is less confusing, like
fireCustomEventor something similar.
I have deprecated the exports to improve the clarity.
I don't think renaming it gives much value given that we'd want to remove those imports for the mentioned convenience API anyway eventually. 🙈
cherniavskii
left a comment
There was a problem hiding this comment.
Thanks for working on this!
I tried upgrading @mui/internal-test-utils in #14142, but then realized that the userEvent is no longer exported 😅 Should we upgrade it in this PR?
|
|
||
| const getPickerDay = (name: string, picker = 'January 2018') => | ||
| getByRole(screen.getByRole('grid', { name: picker }), 'gridcell', { name }); | ||
| rtlGetByRole(screen.getByRole('grid', { name: picker }), 'gridcell', { name }); |
There was a problem hiding this comment.
rtl confused me at first, because I thought "Why would we need a different getByRole for Right-to-Left"? 😅
But since rtl stands for React Testing Library, isn't the naming still misleading given that we don't import it from the RTL package?
On a different note, why avoiding ESLint errors here?
Related to mui/material-ui#42909.
It needs changes from mui/material-ui#43313 because the plugin treatsuserEvent.<function>calls as ones from@testing-library/user-eventand triesawaiting them when it is not necessary.Decided to remove the exported
userEventfrom@mui/internal-test-utilsand move it only tomui-xas no other package is using it. 🙈The existing
userEventnaming is also confusing given that it is the same as the mentioned library, which we also now use.no-containerrule to allow doingcontainer.querySelectorresponsefromrenderfunctions toviewas only it andutilsare allowed as per the plugin rule