import { parseJsx } from '../__tests__/utils' import { getKaioTagNamesMapByComponentName, printAst, transformSource, type TransformSourceArgs, } from '../utils' import { upgradeV1Buttons } from './upgradeV1Buttons' const transformIcons = (sourceFile: TransformSourceArgs['sourceFile']): string => { const kaioTagNamesMap = getKaioTagNamesMapByComponentName(sourceFile, ['IconButton', 'Button']) return transformSource({ sourceFile, transformers: [upgradeV1Buttons(kaioTagNamesMap!)], }) } describe('upgradeV1Buttons()', () => { describe('to Button', () => { it('transforms Button v1 to Button (next) when href and component prop are not set', () => { const inputAst = parseJsx(` import { Button } from "@kaizen/components" export const TestComponent = () => `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) it('transforms IconButton to Button (next) when href and component prop are not set', () => { const inputAst = parseJsx(` import { IconButton } from "@kaizen/components" export const TestComponent = () => } label="More pls" onClick={handleClick} /> `) const outputAst = parseJsx(` import { Button } from "@kaizen/components/next" export const TestComponent = () => `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) it('transforms both IconButton and Button v1 to Button (next) in the same iteration', () => { const inputAst = parseJsx(` import { Button, IconButton } from "@kaizen/components" export const TestComponent = () => ( <> ) `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) it('transforms aliased V1 Buttons to Button when href and component prop are not set', () => { const inputAst = parseJsx(` import { IconButton as KzIconButton, Button as KzButton } from "@kaizen/components" export const TestComponent = () => ( <> ) `) const outputAst = parseJsx(` import { Button } from "@kaizen/components/next" export const TestComponent = () => ( <> ) `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) it('transforms V1 Buttons to aliased Button', () => { const inputAst = parseJsx(` import { IconButton, Button } from "@kaizen/components" import { Button as ButtonAlias } from "@kaizen/components/next" export const TestComponent = () => ( <> ) `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) it('updates V1 Buttons from @kaizen/components/v1/actions', () => { const inputAst = parseJsx(` import { Button, IconButton } from "@kaizen/components/v1/actions" export const TestComponent = () => ( <> ) `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) it('updates V1 Buttons from @kaizen/components/v2/actions', () => { const inputAst = parseJsx(` import { Button, IconButton } from "@kaizen/components/v2/actions" export const TestComponent = () => ( <> ) `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) it('updates aliased V1 Buttons to Button', () => { const inputAst = parseJsx(` import { Button as KzButton, IconButton as KzIconButton } from "@kaizen/components" export const TestComponent = () => ( <> ) `) const outputAst = parseJsx(` import { Button } from "@kaizen/components/next" export const TestComponent = () => ( <> ) `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) it('updates imports of multiple V1 Buttons from different KAIO imports', () => { const inputAst = parseJsx(` import { Button as KzButton, IconButton as KzIconButton } from "@kaizen/components" import { Button as ButtonV1, IconButton as IconButtonV1 } from "@kaizen/components/v1/actions" export const TestComponent = () => ( <> ) `) const outputAst = parseJsx(` import { Button } from "@kaizen/components/next" export const TestComponent = () => ( <> ) `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) it('does not duplicate Button import if it already exists', () => { const inputAst = parseJsx(` import { IconButton, Button as KzButton } from "@kaizen/components" import { Button } from "@kaizen/components/next" export const TestComponent = () => ( <> ) `) const outputAst = parseJsx(` import { Button } from "@kaizen/components/next" export const TestComponent = () => ( <> ) `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) it('does not add Button if aliased Button exists', () => { const inputAst = parseJsx(` import { Button, IconButton } from "@kaizen/components" import { Button as ButtonAlias } from "@kaizen/components/next" export const TestComponent = () => ( <> ) `) expect(transformIcons(inputAst)).toEqual(printAst(outputAst)) }) }) }) describe('to LinkButton', () => { it('transforms V1 Buttons to LinkButton when href prop is set', () => { const inputAst = parseJsx(` import { Button, IconButton } from "@kaizen/components" export const TestComponent = () => ( <>