diff --git a/packages/components/codemods/upgradeV1Buttons/transformV1ButtonAttributes.spec.ts b/packages/components/codemods/upgradeV1Buttons/transformV1ButtonAttributes.spec.ts index 08b7728649f..56f331ae843 100644 --- a/packages/components/codemods/upgradeV1Buttons/transformV1ButtonAttributes.spec.ts +++ b/packages/components/codemods/upgradeV1Buttons/transformV1ButtonAttributes.spec.ts @@ -1,15 +1,19 @@ import ts from 'typescript' import { parseJsx } from '../__tests__/utils' -import { printAst } from '../utils' -import { transformV1Buttons } from './transformV1ButtonAttributes' +import { createJsxElementWithChildren, printAst } from '../utils' +import { transformV1ButtonAttributes } from './transformV1ButtonAttributes' export const mockedTransformer = - (kaioComponentName: string, alias?: string) => + (kaioComponentName: string) => (context: ts.TransformationContext) => (rootNode: ts.Node): ts.Node => { const visit = (node: ts.Node): ts.Node => { if (ts.isJsxSelfClosingElement(node)) { - return transformV1Buttons(node, kaioComponentName, alias) + const { targetComponentName, newAttributes, childrenValue } = transformV1ButtonAttributes( + node, + kaioComponentName, + ) + return createJsxElementWithChildren(targetComponentName, newAttributes, childrenValue) } return ts.visitEachChild(node, visit, context) } @@ -19,36 +23,49 @@ export const mockedTransformer = const transformInput = ( sourceFile: ts.SourceFile, kaioComponentName: string = 'Button', - alias?: string, ): string => { - const result = ts.transform(sourceFile, [mockedTransformer(kaioComponentName, alias)]) + const result = ts.transform(sourceFile, [mockedTransformer(kaioComponentName)]) const transformedSource = result.transformed[0] as ts.SourceFile return printAst(transformedSource) } -describe('transformV1Buttons()', () => { +describe('transformV1ButtonAttributes()', () => { it('changes label to children', () => { const inputAst = parseJsx('') + const outputAst = parseJsx('') expect(transformInput(inputAst)).toEqual(printAst(outputAst)) }) it('replaces IconButton with Button and changes label to children and adds hasHiddenLabel', () => { const inputAst = parseJsx('') - const outputAst = parseJsx('') + const outputAst = parseJsx( + '', + ) expect(transformInput(inputAst, 'IconButton')).toEqual(printAst(outputAst)) }) - it('uses alias if it is defined', () => { - const inputAst = parseJsx('') + const outputAst = parseJsx( + '', + ) expect(transformInput(inputAst)).toEqual(printAst(outputAst)) }) @@ -61,8 +78,8 @@ describe('transformV1Buttons()', () => { `) const outputAst = parseJsx(` <> - - + + `) expect(transformInput(inputAst)).toEqual(printAst(outputAst)) @@ -70,13 +87,17 @@ describe('transformV1Buttons()', () => { it('changes classNameOverride to className', () => { const inputAst = parseJsx('') + const outputAst = parseJsx( + '', + ) expect(transformInput(inputAst)).toEqual(printAst(outputAst)) }) it('changes data-automation-id to data-testid', () => { const inputAst = parseJsx('') + const outputAst = parseJsx( + '', + ) expect(transformInput(inputAst)).toEqual(printAst(outputAst)) }) @@ -89,8 +110,8 @@ describe('transformV1Buttons()', () => { `) const outputAst = parseJsx(` <> - - + + `) expect(transformInput(inputAst)).toEqual(printAst(outputAst)) @@ -101,7 +122,7 @@ describe('transformV1Buttons()', () => { '', + '', ) expect(transformInput(inputAst)).toEqual(printAst(outputAst)) }) @@ -155,25 +176,25 @@ describe('transformV1Buttons()', () => { describe('transform size', () => { it('changes default (undefined) to large', () => { const inputAst = parseJsx('') + const outputAst = parseJsx('') expect(transformInput(inputAst)).toEqual(printAst(outputAst)) }) it('changes small to medium', () => { const inputAst = parseJsx('') + const outputAst = parseJsx('') expect(transformInput(inputAst)).toEqual(printAst(outputAst)) }) it('changes regular to large', () => { const inputAst = parseJsx('') + const outputAst = parseJsx('') expect(transformInput(inputAst)).toEqual(printAst(outputAst)) }) it('does not change a non-string value', () => { const inputAst = parseJsx('') + const outputAst = parseJsx('') expect(transformInput(inputAst)).toEqual(printAst(outputAst)) }) }) diff --git a/packages/components/codemods/upgradeV1Buttons/transformV1ButtonAttributes.ts b/packages/components/codemods/upgradeV1Buttons/transformV1ButtonAttributes.ts index 9b2861cd375..8007e65b3f5 100644 --- a/packages/components/codemods/upgradeV1Buttons/transformV1ButtonAttributes.ts +++ b/packages/components/codemods/upgradeV1Buttons/transformV1ButtonAttributes.ts @@ -1,12 +1,7 @@ import ts from 'typescript' import { type ButtonProps as V1ButtonProps } from '~components/Button' import { type ButtonProps as RCButtonProps } from '~components/__rc__/Button' -import { - createJsxElementWithChildren, - createProp, - createStringProp, - getPropValueText, -} from '../utils' +import { createProp, createStringProp, getPropValueText } from '../utils' const getNewSizeValue = ( oldValue: Exclude, @@ -68,7 +63,6 @@ type TransformedButtonAttributes = { export const transformV1ButtonAttributes = ( node: ts.JsxSelfClosingElement, kaioComponentName: string, - // tagName: string = 'Button', ): TransformedButtonAttributes => { let childrenValue: ts.JsxAttributeValue | undefined let hasSizeProp = false @@ -135,5 +129,4 @@ export const transformV1ButtonAttributes = ( newAttributes, childrenValue, } - // return createJsxElementWithChildren(tagName, newAttributes, childrenValue) }