import { parseJsx } from '../__tests__/utils' import { getKaioTagNamesMapByComponentName, printAst, transformSource, type TransformSourceArgs, } from '../utils' import { migrateGuidanceBlockActionsToActionsSlot } from './migrateGuidanceBlockActionsToActionsSlot' const transformGuidanceBlock = (sourceFile: TransformSourceArgs['sourceFile']): string => { const kaioTagNamesMap = getKaioTagNamesMapByComponentName(sourceFile, ['GuidanceBlock']) return transformSource({ sourceFile, transformers: [migrateGuidanceBlockActionsToActionsSlot(kaioTagNamesMap!)], }) } describe('migrateGuidanceBlockActionsToActionsSlot()', () => { describe('basic transformation', () => { it('imports Button and transforms all button-like actions prop into a Button component', () => { const inputAst = parseJsx(` import { GuidanceBlock } from "@kaizen/components" } content={
Test
} actions={{ primary: { label: 'Primary action', onClick: () => alert('click 1'), }, secondary: { label: 'Secondary action', onClick: () => alert('click 2'), }, }} />`) const outputAst = parseJsx(` import { GuidanceBlock, Button, Icon } from "@kaizen/components" } content={
Test
} actionsSlot={<>} /> `) expect(transformGuidanceBlock(inputAst)).toBe(printAst(outputAst)) }) it('imports and transforms all button-like and link-like actions prop into a Button and LinkButton', () => { const inputAst = parseJsx(` import { GuidanceBlock } from "@kaizen/components" } content={
Test
} actions={{ primary: { label: 'Primary action', onClick: () => alert('click 1'), }, secondary: { label: 'Secondary action', href: "#secondary" }, }} />`) const outputAst = parseJsx(` import {GuidanceBlock, Button, LinkButton, Icon } from "@kaizen/components" } content={
Test
} actionsSlot={<>Secondary action} /> `) expect(transformGuidanceBlock(inputAst)).toBe(printAst(outputAst)) }) it('does not redeclare imports if found', () => { const inputAst = parseJsx(` import { Button, GuidanceBlock } from "@kaizen/components" const MockLayout = () => ( <> } content={
Test
} actions={{ primary: { label: 'Primary action', onClick: () => alert('click 1'), }, secondary: { label: 'Secondary action', href: "#secondary" }, }} /> )`) const outputAst = parseJsx(` import { Button, GuidanceBlock, LinkButton, Icon } from "@kaizen/components" const MockLayout = () => ( <> } content={
Test
} actionsSlot={<>Secondary action} /> )`) expect(transformGuidanceBlock(inputAst)).toBe(printAst(outputAst)) }) it('preserves custom icon components without adding Icon import', () => { const inputAst = parseJsx(` import { GuidanceBlock } from "@kaizen/components" {}, icon: , iconPosition: 'end', }, }} />`) const outputAst = parseJsx(` import { GuidanceBlock, Button } from "@kaizen/components" } /> `) expect(transformGuidanceBlock(inputAst)).toBe(printAst(outputAst)) }) describe('arrow icon handling', () => { // When withActionButtonArrow not specified, add arrow icon actionsSlot secondary Button it('adds arrow icon to actionsSlot secondary Button when withActionButtonArrow is not explicitly set', () => { const inputAst = parseJsx(` import { GuidanceBlock, LinkButton } from "@kaizen/components" } content={
Test
} actions={{ primary: { label: 'Primary action', onClick: () => alert('click 1'), }, secondary: { label: 'Secondary action', href: "#secondary" }, }} />`) const outputAst = parseJsx(` import { GuidanceBlock, LinkButton, Button, Icon } from "@kaizen/components" } content={
Test
} actionsSlot={<>Secondary action} /> `) expect(transformGuidanceBlock(inputAst)).toBe(printAst(outputAst)) }) // When withActionButtonArrow is set to true, add arrow icon to actionsSlot secondary Button it('adds arrow icon to actionsSlot secondary Button when withActionButtonArrow is true', () => { const inputAst = parseJsx(` import { GuidanceBlock } from "@kaizen/components" } content={
Test
} withActionButtonArrow={true} actions={{ primary: { label: 'Primary action', onClick: () => alert('click 1'), }, secondary: { label: 'Secondary action', href: "#secondary" }, }} />`) const outputAst = parseJsx(` import { GuidanceBlock, Button, LinkButton, Icon } from "@kaizen/components" } content={
Test
} actionsSlot={<>Secondary action} /> `) expect(transformGuidanceBlock(inputAst)).toBe(printAst(outputAst)) }) // When withActionButtonArrow is explicitly false, do not add arrow icon to actionsSlot secondary Button it('does not add arrow icon to actionsSlot secondary Button when withActionButtonArrow is false', () => { const inputAst = parseJsx(` import { GuidanceBlock } from "@kaizen/components" } content={
Test
} withActionButtonArrow={false} actions={{ primary: { label: 'Primary action', onClick: () => alert('click 1'), }, secondary: { label: 'Secondary action', href: "#secondary" }, }} />`) const outputAst = parseJsx(` import { GuidanceBlock, Button, LinkButton } from "@kaizen/components" } content={
Test
} actionsSlot={<>Secondary action} /> `) expect(transformGuidanceBlock(inputAst)).toBe(printAst(outputAst)) }) }) describe('removes deprecated props', () => { it('removes withActionButtonArrow prop', () => { const inputAst = parseJsx(` import { GuidanceBlock } from "@kaizen/components" } content={
Test
} withActionButtonArrow={true} actions={{ primary: { label: 'Primary action', onClick: () => alert('click 1'), }, }} /> `) const outputAst = parseJsx(` import { GuidanceBlock, Button, Icon } from "@kaizen/components" } content={
Test
} actionsSlot={<>} /> `) expect(transformGuidanceBlock(inputAst)).toBe(printAst(outputAst)) }) it('removes secondaryDismiss prop', () => { const inputAst = parseJsx(` import { GuidanceBlock } from "@kaizen/components" } content={
Test
} secondaryDismiss={true} actions={{ primary: { label: 'Primary action', onClick: () => alert('click 1'), }, }} /> `) const outputAst = parseJsx(` import {GuidanceBlock, Button, Icon } from "@kaizen/components" } content={
Test
} actionsSlot={<>} /> `) expect(transformGuidanceBlock(inputAst)).toBe(printAst(outputAst)) }) }) }) })