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

[material-ui][Tabs] ScrollbarSize.setMeasurements performs a null-check for nodeRef before setting scrollbarHeight #42512

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/mui-material/src/Tabs/ScrollbarSize.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export default function ScrollbarSize(props) {
const nodeRef = React.useRef(null);

const setMeasurements = () => {
scrollbarHeight.current = nodeRef.current.offsetHeight - nodeRef.current.clientHeight;
if (nodeRef.current) {
scrollbarHeight.current = nodeRef.current.offsetHeight - nodeRef.current.clientHeight;
}
};

useEnhancedEffect(() => {
Expand Down
91 changes: 91 additions & 0 deletions packages/mui-material/src/Tabs/Tabs.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import userEvent from '@testing-library/user-event';
Copy link
Member

@oliviertassinari oliviertassinari Jun 23, 2024

Choose a reason for hiding this comment

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

We don't allow this dependency, I mean to the best of my knowledge, we trigger each event instead.

import {
act,
createRenderer,
Expand All @@ -9,6 +10,8 @@ import {
strictModeDoubleLoggingSuppressed,
waitFor,
} from '@mui/internal-test-utils';
import Box from '@mui/material/Box';
import Container from '@mui/material/Container';
Comment on lines +13 to +14
Copy link
Member

@oliviertassinari oliviertassinari Jun 23, 2024

Choose a reason for hiding this comment

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

To simplify

Suggested change
import Box from '@mui/material/Box';
import Container from '@mui/material/Container';

import Tab from '@mui/material/Tab';
import Tabs, { tabsClasses as classes } from '@mui/material/Tabs';
import { svgIconClasses } from '@mui/material/SvgIcon';
Expand Down Expand Up @@ -1438,5 +1441,93 @@ describe('<Tabs />', () => {
expect(hasLeftScrollButton(container)).to.equal(false);
expect(hasRightScrollButton(container)).to.equal(false);
});

// https://github.com/mui/material-ui/issues/41388
it('should allow a user to click a scrollable Tab without an error', async function test() {
Copy link
Member

Choose a reason for hiding this comment

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

This test passes in next branch HEAD, which means it isn't testing what's needed. It should fail in base branch.

function DynamicScrollableTabs() {
Copy link
Author

Choose a reason for hiding this comment

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

Analogous to DynamicScrollableTabs used in the live example.

const [value, setValue] = React.useState(0);
const handleChange = (_, newValue) => {
setValue(newValue);
};
const tabLabels = [
'Item One',
'Item Two',
'Item Three',
'Item Four',
'Item Five',
'Item Six',
'Item Seven',
'Item Eight',
'Item Nine',
'Item Ten',
];
return (
<Container maxWidth="sm">
Copy link
Member

Choose a reason for hiding this comment

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

to simplify

<Box sx={{ width: '100%', m: 2 }}>
<Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
<Tabs
onChange={handleChange}
value={value}
variant="scrollable"
scrollButtons
allowScrollButtonsMobile
selectionFollowsFocus
>
{tabLabels.map((label, index) => (
<Tab
key={`tab-${index}`}
label={label}
id={`dynamic-tab-${index}`}
aria-controls={`dynamic-tabpanel-${index}`}
/>
))}
</Tabs>
</Box>
{tabLabels.map((label, index) => {
return (
<Box
key={`tab-panel-${index}`}
role="tabpanel"
hidden={value !== index}
id={`dynamic-tabpanel-${index}`}
aria-labelledby={`dynamic-tab-${index}`}
>
{value === index && (
<Box sx={{ p: 3 }}>
<p>{label}</p>
</Box>
)}
</Box>
);
})}
</Box>
</Container>
);
}

const { getByRole } = render(<DynamicScrollableTabs />);

const user = userEvent.setup();

// Tab and TabPanel for Item One should be visible
const tab1 = getByRole('tab', { name: 'Item One' });
expect(tab1).toBeVisible();
const tabPanel1 = getByRole('tabpanel', { name: 'Item One' });
expect(tabPanel1).toBeVisible();

// Tab for Item Seven should be visible
const tab7 = getByRole('tab', { name: 'Item Seven' });
expect(tab7).toBeVisible();

// Click on the Item Seven Tab
// In the test fixture, this fails with
// TypeError: Cannot read properties of null (reading 'offsetHeight')
await user.click(tab7);
Copy link
Author

Choose a reason for hiding this comment

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

This test is analogous to https://github.com/nicholeuf/mui-test-fixture-41388/blob/main/app/page.test.tsx. The user click action fails in the live example test fixture and succeeds here.


// TabPanel for Item Seven should be visible
const tabPanel7 = getByRole('tabpanel', { name: 'Item Seven' });
expect(tabPanel7).toBeVisible();
expect(tabPanel7).text('Item Seven');
});
});
});
Loading