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 = () =>
`)
const outputAst = parseJsx(`
import { Button } from "@kaizen/components/next"
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 = () => } onPress={handleClick} variant="tertiary" size="large" hasHiddenLabel>More pls
`)
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 = () => (
<>
} label="More pls" />
>
)
`)
const outputAst = parseJsx(`
import { Button } from "@kaizen/components/next"
export const TestComponent = () => (
<>
} variant="tertiary" size="large" hasHiddenLabel>More pls
>
)
`)
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 = () => (
<>
Toast
>
)
`)
const outputAst = parseJsx(`
import { Button as ButtonAlias } from "@kaizen/components/next"
export const TestComponent = () => (
<>
Waffle
Pancake
Toast
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
describe('import statements', () => {
it('updates V1 Buttons from @kaizen/components', () => {
const inputAst = parseJsx(`
import { Button, IconButton } 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 V1 Buttons from @kaizen/components/v1/actions', () => {
const inputAst = parseJsx(`
import { Button, IconButton } 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('updates V1 Buttons from @kaizen/components/v2/actions', () => {
const inputAst = parseJsx(`
import { Button, IconButton } from "@kaizen/components/v2/actions"
export const TestComponent = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { Button } from "@kaizen/components/next"
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 = () => (
<>
Waffles
>
)
`)
const outputAst = parseJsx(`
import { Button as ButtonAlias } from "@kaizen/components/next"
export const TestComponent = () => (
<>
Pancakes
Scones
Waffles
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
it('does not update import of irrelevant KAIO components', () => {
const inputAst = parseJsx(`
import { IconButton, FilterButton } from "@kaizen/components"
export const TestComponent = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { FilterButton } from "@kaizen/components"
import { Button } 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 = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Pancakes
Waffles
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
it('transforms V1 Buttons to LinkButton when component prop is set', () => {
const inputAst = parseJsx(`
import { Button, IconButton } from "@kaizen/components"
export const TestComponent = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Pancakes
Waffles
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
it('transforms both IconButton and Button to LinkButton in the same iteration', () => {
const inputAst = parseJsx(`
import { Button, IconButton } from "@kaizen/components"
export const TestComponent = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
it('transforms aliased V1 Buttons to LinkButton when href or component prop are set', () => {
const inputAst = parseJsx(`
import { IconButton as KzIconButton, Button as KzButton } from "@kaizen/components"
export const TestComponent = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
it('transforms V1 Buttons to aliased LinkButton', () => {
const inputAst = parseJsx(`
import { IconButton, Button } from "@kaizen/components"
import { LinkButton as LinkButtonAlias } from "@kaizen/components"
export const TestComponent = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { LinkButton as LinkButtonAlias } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
describe('import statements', () => {
it('updates V1 Buttons from @kaizen/components', () => {
const inputAst = parseJsx(`
import { Button, IconButton } from "@kaizen/components"
export const TestComponent = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
>
)
`)
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 = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
>
)
`)
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 = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
it('updates aliased V1 Buttons to LinkButton', () => {
const inputAst = parseJsx(`
import { Button as KzButton, IconButton as KzIconButton } from "@kaizen/components"
export const TestComponent = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
>
)
`)
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 { LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
Summer
Autumn
Winter
Spring
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
it('does not duplicate LinkButton import if it already exists', () => {
const inputAst = parseJsx(`
import { IconButton, Button, LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Waffles
>
)
`)
const outputAst = parseJsx(`
import { LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
Waffles
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
it('does not add LinkButton if aliased LinkButton exists', () => {
const inputAst = parseJsx(`
import { Button, IconButton, LinkButton as LinkButtonAlias } from "@kaizen/components"
export const TestComponent = () => (
<>
Waffles
>
)
`)
const outputAst = parseJsx(`
import { LinkButton as LinkButtonAlias } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
Waffles
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
it('does not update import of irrelevant KAIO components', () => {
const inputAst = parseJsx(`
import { Button, IconButton, FilterButton } from "@kaizen/components"
export const TestComponent = () => (
<>
>
)
`)
const outputAst = parseJsx(`
import { FilterButton, LinkButton } from "@kaizen/components"
export const TestComponent = () => (
<>
Summer
Autumn
Winter
Spring
>
)
`)
expect(transformIcons(inputAst)).toEqual(printAst(outputAst))
})
})
})
})