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

chore(useClickableProps): Codemod for changing dataTestId to data-testid #2406

Merged
merged 3 commits into from
Sep 2, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import transform from "../use-clickable-props-hook-migration";
import { defineInlineTest } from "jscodeshift/src/testUtils";

function prependImport(source: string): string {
return `
import { useClickableProps } from "monday-ui-react-core";
${source}
`;
}

describe("useClickableProps prop migration", () => {
defineInlineTest(
transform,
{},
prependImport(`
useClickableProps({
onClick: onClickCallback,
onMouseDown,
disabled,
id,
dataTestId: componentDataTestId,
ariaLabel: overrideAriaLabel,
ariaHidden: false,
ariaHasPopup: false,
ariaExpanded: false
});
`),
prependImport(`
useClickableProps({
onClick: onClickCallback,
onMouseDown,
disabled,
id,
"data-testid": componentDataTestId,
ariaLabel: overrideAriaLabel,
ariaHidden: false,
ariaHasPopup: false,
ariaExpanded: false
});
`),
"should rename 'dataTestId' to 'data-testid'"
);

defineInlineTest(
transform,
{},
prependImport(`
useClickableProps({
dataTestId: componentDataTestId,
onClick: onClickCallback
});
`),
prependImport(`
useClickableProps({
"data-testid": componentDataTestId,
onClick: onClickCallback
});
`),
"should rename 'dataTestId' to 'data-testid' when it's the first property"
);

defineInlineTest(
transform,
{},
prependImport(`
useClickableProps({
dataTestId: componentDataTestId,
onClick: onClickCallback
});
`),
prependImport(`
useClickableProps({
"data-testid": componentDataTestId,
onClick: onClickCallback
});
`),
"should rename 'dataTestId' to 'data-testid' when it's the first property"
);

defineInlineTest(
transform,
{},
prependImport(`
useClickableProps({
onClick: onClickCallback
});
`),
prependImport(`
useClickableProps({
onClick: onClickCallback
});
`),
"should not change anything when 'dataTestId' is not present"
);

defineInlineTest(
transform,
{},
`
import { useClickableProps } from "other-library";
useClickableProps({
dataTestId: componentDataTestId,
onClick: onClickCallback
});
`,
`
import { useClickableProps } from "other-library";
useClickableProps({
dataTestId: componentDataTestId,
onClick: onClickCallback
});
`,
"should not rename 'dataTestId' to 'data-testid' when not a vibe component"
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getComponentNameOrAliasFromImports, getCoreImportsForFile, wrap } from "../../../src/utils";
import { TransformationContext } from "../../../types";

/**
* 1. Update the 'dataTestId' prop to 'data-testid'
*/
function transform({ j, root }: TransformationContext) {
const imports = getCoreImportsForFile(root);
const hookName = getComponentNameOrAliasFromImports(j, imports, "useClickableProps");
if (!hookName) return;

root
.find(j.CallExpression, {
callee: {
type: "Identifier",
name: hookName
}
})
.find(j.ObjectExpression)
.find(j.Property, {
key: {
type: "Identifier",
name: "dataTestId"
}
})
.forEach(propPath => {
propPath.node.key = j.literal("data-testid");
});
}

export default wrap(transform);
2 changes: 1 addition & 1 deletion packages/core/docs/vibe-3-changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ codemod: `color-picker-content-import-migration`

### useClickableProps

- `dataTestId` -> `data-testid` [codemod]
- `dataTestId` -> `data-testid` [codemod]

## monday-ui-style

Expand Down