Skip to content

Commit e85e68a

Browse files
Automigration: Implement transformations for backgrounds parameters and enhance viewport handling in story files
1 parent e9c3bf8 commit e85e68a

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

code/lib/cli-storybook/src/automigrate/fixes/addon-globals-api.test.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ describe('addon-globals-api', () => {
635635
export const MobileOnly = {
636636
globals: {
637637
viewport: {
638-
value: "mobile",
638+
value: 'mobile',
639639
isRotated: false
640640
}
641641
}
@@ -665,7 +665,7 @@ describe('addon-globals-api', () => {
665665
export const DarkMobile = {
666666
globals: {
667667
viewport: {
668-
value: "mobile",
668+
value: 'mobile',
669669
isRotated: false
670670
},
671671
backgrounds: {
@@ -804,7 +804,7 @@ describe('addon-globals-api', () => {
804804
},
805805
globals: {
806806
viewport: {
807-
value: "tablet",
807+
value: 'tablet',
808808
isRotated: false
809809
},
810810
backgrounds: {
@@ -845,7 +845,7 @@ describe('addon-globals-api', () => {
845845
},
846846
globals: {
847847
viewport: {
848-
value: "iphonex",
848+
value: 'iphonex',
849849
isRotated: true
850850
}
851851
}
@@ -887,5 +887,48 @@ describe('addon-globals-api', () => {
887887
expect(transformFn).toBeDefined();
888888
expect(transformFn!('story.js', storyContent)).toBe(expectedContent);
889889
});
890+
891+
it('should transform backgrounds values to options and migrate default in story files', async () => {
892+
const { transformFn } = await runMigrationAndGetTransformFn(defaultPreview);
893+
const storyContent = dedent`
894+
import Button from './Button';
895+
export default { component: Button };
896+
export const Mobile = {
897+
parameters: {
898+
backgrounds: {
899+
default: 'Light',
900+
values: [
901+
{ name: 'Gray', value: '#CCC' },
902+
],
903+
},
904+
},
905+
};
906+
`;
907+
const expectedContent = dedent`
908+
import Button from './Button';
909+
export default {
910+
component: Button
911+
};
912+
export const Mobile = {
913+
parameters: {
914+
backgrounds: {
915+
options: {
916+
gray: {
917+
name: 'Gray',
918+
value: '#CCC'
919+
}
920+
}
921+
}
922+
},
923+
globals: {
924+
backgrounds: {
925+
value: "light"
926+
}
927+
}
928+
};
929+
`;
930+
expect(transformFn).toBeDefined();
931+
expect(transformFn!('story.js', storyContent)).toBe(expectedContent);
932+
});
890933
});
891934
});

code/lib/cli-storybook/src/automigrate/fixes/addon-globals-api.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,18 @@ function transformStoryFile(
373373
if (backgroundsParams) {
374374
const defaultBackground = getObjectProperty(backgroundsParams, 'default');
375375
const disableBackground = getObjectProperty(backgroundsParams, 'disable');
376+
const valuesBackground = getObjectProperty(backgroundsParams, 'values');
377+
378+
// Handle values -> options transformation
379+
if (valuesBackground && t.isArrayExpression(valuesBackground)) {
380+
// Transform values array to options object
381+
const optionsObject = transformValuesToOptions(valuesBackground);
382+
383+
// Remove the old values property
384+
removeProperty(backgroundsParams, 'values');
385+
addProperty(backgroundsParams, 'options', optionsObject);
386+
storyHasChanges = true;
387+
}
376388

377389
if (defaultBackground && t.isStringLiteral(defaultBackground)) {
378390
// Create globals.backgrounds

0 commit comments

Comments
 (0)