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

[charts] Refactor axis band scaleType check #13295

Merged
merged 3 commits into from
May 29, 2024

Conversation

JCQuintas
Copy link
Member

  • Move "scale type check" to its own function so it is easier to reason with
  • Added tests in order to properly refactor code

This was done as part of #12537 to prepare for extended checks

@JCQuintas JCQuintas requested a review from alexfauquette May 29, 2024 10:44
@JCQuintas JCQuintas self-assigned this May 29, 2024
@mui-bot
Copy link

mui-bot commented May 29, 2024

Deploy preview: https://deploy-preview-13295--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against 5b3d3f3

@JCQuintas JCQuintas added the enhancement This is not a bug, nor a new feature label May 29, 2024
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.

Nice refactoring. Just left nitpick comments

import { checkScaleErrors } from './checkScaleErrors';
import { DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY } from '../constants';

describe('BarChart - checkScaleErrors', () => {
Copy link
Member

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

Copy link
Member Author

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 😅

Copy link
Member Author

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?

Comment on lines +36 to +38
const discreteAxisKey = verticalLayout ? xAxisKey : yAxisKey;
const continuousAxisKey = verticalLayout ? yAxisKey : xAxisKey;

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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}".`,
Copy link
Member

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

Suggested change
`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}".`,

Comment on lines +12 to +20
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}"`;
};
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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}"`;
};

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

🤦

@alexfauquette alexfauquette changed the title Refactor band type check [charts] Refactor axis band scaleType check May 29, 2024
@JCQuintas JCQuintas merged commit ca058d7 into mui:master May 29, 2024
17 of 18 checks passed
@JCQuintas JCQuintas deleted the refactor-band-type-check branch May 29, 2024 14:24
@JCQuintas JCQuintas added the component: charts This is the name of the generic UI component, not the React module! label May 30, 2024
DungTiger pushed a commit to DungTiger/mui-x that referenced this pull request Jul 23, 2024
thomasmoon pushed a commit to thomasmoon/mui-x that referenced this pull request Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: charts This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants