-
-
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
[charts] Refactor axis band scaleType check #13295
Conversation
Deploy preview: https://deploy-preview-13295--material-ui-x.netlify.app/ |
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.
Nice refactoring. Just left nitpick comments
import { checkScaleErrors } from './checkScaleErrors'; | ||
import { DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY } from '../constants'; | ||
|
||
describe('BarChart - checkScaleErrors', () => { |
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.
For logic that impacts the rendering, I agree we can test the logic function independently because the difficulty of maintaining a test that checks element coordinates is annoying
But for errors/warnings, we could test directly the <BarChart />
such that test breaks if for example, we delete the call to checkScaleErrors
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 I just didn't invest into setting up react testing, as I didn't find any example in the charts package 😅
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.
Do you think we should go straight with playwright for this? Or run them in mocha?
const discreteAxisKey = verticalLayout ? xAxisKey : yAxisKey; | ||
const continuousAxisKey = verticalLayout ? yAxisKey : xAxisKey; | ||
|
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.
const discreteAxisKey = verticalLayout ? xAxisKey : yAxisKey; | |
const continuousAxisKey = verticalLayout ? yAxisKey : xAxisKey; | |
const discreteAxisDirection = verticalLayout ? 'x' : 'y'; | |
const continuousAxisDirection = verticalLayout ? 'y' : 'x'; | |
|
||
if (!isBandScaleConfig(discreteAxisConfig)) { | ||
throw new Error( | ||
`MUI X Charts: ${getAxisMessage(verticalLayout, discreteAxisKey)} should be of type "band" to display the bar series of id "${seriesId}".`, |
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.
Same adaptation would be needed on the two other lines
`MUI X Charts: ${getAxisMessage(verticalLayout, discreteAxisKey)} should be of type "band" to display the bar series of id "${seriesId}".`, | |
`MUI X Charts: ${getAxisMessage(discreteAxisDirection)} should be of type "band" to display the bar series of id "${seriesId}".`, |
const getAxisMessage = (isVertical: boolean, axisKey: string, isContinuous: boolean = false) => { | ||
const axisName = getXOrY(isVertical, isContinuous, 'x-axis', 'y-axis'); | ||
const axisKeyName = getXOrY(isVertical, isContinuous, 'xAxis', 'yAxis'); | ||
const axisDefaultKey = getXOrY(isVertical, isContinuous, DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY); | ||
|
||
return axisKey === axisDefaultKey | ||
? `The first \`${axisKeyName}\`` | ||
: `The ${axisName} with id "${axisKey}"`; | ||
}; |
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.
const getAxisMessage = (isVertical: boolean, axisKey: string, isContinuous: boolean = false) => { | |
const axisName = getXOrY(isVertical, isContinuous, 'x-axis', 'y-axis'); | |
const axisKeyName = getXOrY(isVertical, isContinuous, 'xAxis', 'yAxis'); | |
const axisDefaultKey = getXOrY(isVertical, isContinuous, DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY); | |
return axisKey === axisDefaultKey | |
? `The first \`${axisKeyName}\`` | |
: `The ${axisName} with id "${axisKey}"`; | |
}; | |
const getAxisMessage = (axisDirection: 'x' | 'y') => { | |
const axisName = `${axisDirection}-axis`; | |
const axisKeyName = `${axisDirection}Axis`; | |
const axisDefaultKey = axisDirection === 'x'? DEFAULT_X_AXIS_KEY : DEFAULT_Y_AXIS_KEY; | |
return axisKey === axisDefaultKey | |
? `The first \`${axisKeyName}\`` | |
: `The ${axisName} with id "${axisKey}"`; | |
}; |
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.
Issue is that we have both discrete
and continuous
, and one is the inverse of the other. We also need the proper axisKey
for the error message.
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.
The idea was to move this logic in a single place
const discreteAxisKey = verticalLayout ? xAxisKey : yAxisKey;
const continuousAxisKey = verticalLayout ? yAxisKey : xAxisKey;
+ const discreteAxisDirection = verticalLayout ? 'x' : 'y';
+ const continuousAxisDirection = verticalLayout ? 'y' : 'x';
And then if you do a test on the discreteAxisKey
you call the function with discreteAxisDirection
. And if you do a test on continuousAxisKey
you call the error message with continuousAxisDirection
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.
🤦
"scale type check"
to its own function so it is easier to reason withThis was done as part of #12537 to prepare for extended checks