diff --git a/packages/sitecore-jss/src/personalize/layout-personalizer.test.ts b/packages/sitecore-jss/src/personalize/layout-personalizer.test.ts index 397017d565..f6b0e7b070 100644 --- a/packages/sitecore-jss/src/personalize/layout-personalizer.test.ts +++ b/packages/sitecore-jss/src/personalize/layout-personalizer.test.ts @@ -360,6 +360,16 @@ describe('layout-personalizer', () => { }); }); + it('should return HIDDEN_RENDERING variant in metadata mode with uid preserved', () => { + const variant = 'mountain_bike_audience'; + const personalizedComponentResult = personalizeComponent( + (variantIsHidden as unknown) as ComponentRenderingWithExperiences, + [variant], + true + ); + expect(personalizedComponentResult?.uid).to.equal(variantIsHidden.uid); + }); + it('should return HIDDEN_RENDERING variant in metadata edit mode when non-default variant is hidden', () => { const variant = 'mountain_bike_audience'; const personalizedComponentResult = personalizeComponent( diff --git a/packages/sitecore-jss/src/personalize/layout-personalizer.ts b/packages/sitecore-jss/src/personalize/layout-personalizer.ts index 9f1d7138bb..926f61820f 100644 --- a/packages/sitecore-jss/src/personalize/layout-personalizer.ts +++ b/packages/sitecore-jss/src/personalize/layout-personalizer.ts @@ -7,10 +7,13 @@ import { EditMode, } from './../layout/models'; -const hiddenRenderingVariant = { +const transformToHiddenRenderingVariant = ( + component: ComponentRendering | HtmlElementRendering +) => ({ + ...component, componentName: HIDDEN_RENDERING_NAME, experiences: {}, -}; +}); export type ComponentRenderingWithExperiences = ComponentRendering & { experiences: { [name: string]: ComponentRenderingWithExperiences }; @@ -106,14 +109,14 @@ export function personalizeComponent( if (!variant && !component.componentName) { // DEFAULT IS HIDDEN if (metadataEditing) { - component = hiddenRenderingVariant; + component = transformToHiddenRenderingVariant(component); } else { return null; } } else if (variant && variant.componentName === null && variant.dataSource === null) { // VARIANT IS HIDDEN if (metadataEditing) { - component = hiddenRenderingVariant; + component = transformToHiddenRenderingVariant(component); } else { return null; }