-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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] Fix cell slot style override #11215
Changes from 4 commits
25e4138
e873b5c
54f87da
9ca37c6
f342daf
ee5cefc
3846e3f
06d4b8f
f4f56e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,18 +10,9 @@ describe('<DataGrid /> - Components', () => { | |
|
||
const baselineProps = { | ||
rows: [ | ||
{ | ||
id: 0, | ||
brand: 'Nike', | ||
}, | ||
{ | ||
id: 1, | ||
brand: 'Adidas', | ||
}, | ||
{ | ||
id: 2, | ||
brand: 'Puma', | ||
}, | ||
{ id: 0, brand: 'Nike' }, | ||
{ id: 1, brand: 'Adidas' }, | ||
{ id: 2, brand: 'Puma' }, | ||
], | ||
columns: [{ field: 'brand' }], | ||
}; | ||
|
@@ -64,6 +55,29 @@ describe('<DataGrid /> - Components', () => { | |
expect(getCell(0, 0)).to.have.attr('data-name', 'foobar'); | ||
}); | ||
|
||
it('should not override cell dimensions when passing `slotProps.cell.style` to the cell', () => { | ||
function Test() { | ||
const [cellProps, setCellProps] = React.useState({}); | ||
return ( | ||
<div> | ||
<button onClick={() => setCellProps({ style: {} })}>Apply cell styles</button> | ||
<div style={{ width: 300, height: 500 }}> | ||
<DataGrid {...baselineProps} slotProps={{ cell: cellProps }} /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
render(<Test />); | ||
|
||
const initialCellWidth = getCell(0, 0).getBoundingClientRect().width; | ||
|
||
const button = screen.getByRole('button', { name: /Apply cell styles/i }); | ||
fireEvent.click(button); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could maybe simplify the test. One render that assert the inline style applied to the cell could be enough. But yeah, the way it's written here is more resilient to future code changes. Should we maybe check that in addition to not break styles, we can add new ones? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
|
||
expect(getCell(0, 0).getBoundingClientRect().width).to.equal(initialCellWidth); | ||
}); | ||
|
||
it('should pass the props from componentsProps.row to the row', () => { | ||
render( | ||
<div style={{ width: 300, height: 500 }}> | ||
|
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.
@romgrk Could you cherry-pick this test to the sticky headers PR?
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.
Yeah but I don't understand what it's testing? Aren't the styles empty both times? Also we're changing a bit the approach in sticky, we use CSS variables to set dimensions.
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 doesn't matter what we pass in
slotProps.cell.style
(this is why it's an empty object).What matters is that the cell shouldn't lose its dimensions which are set as inline styles (see the screenshot in #11215 (comment))
The CSS variables are passed as inline styles, so this test still makes sense there. If
slotProps.cell.style
is not properly merged with the internal inline styles, they will be overwritten 🙂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 don't like that test :| I think we should move the destructured props from this:
to this and call it a day:
Probably because I just spent weeks going through all sorts of tests, but I feel like our test suite might need some cleanup. And I don't like the idea that we keep adding tests for bugs that are very unlikely to reoccur, such as this one, because we keep paying more and more time to run it. And if we test for the
style
prop, why wouldn't we also test all the other props that are equally likely to be overriden?Lmk what you think.
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.
On a different note, about the test, IIUC this might be incorrect. We want to test that we preserve the cell dimensions but we never have a moment when
slotProps.cell.style
isundefined
, thereforeinitialCellWidth
will always return the same value, regardless if the dimensions are correct.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.
Hmm, the initial render doesn't have any cell styles passed, so the cell width is the proper one.
After the button click, the cell styles are added. Am I missing something?
Maybe
setProps
instead of button click would be more readable.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 double checked and the test fails if the fix is not applied:
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.
Fair point, let's keep it.
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've picked up this test & fix on the sticky PR.
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.
Thanks!