diff --git a/libs/features/personalization/personalization.js b/libs/features/personalization/personalization.js index dfc1ac354d..a3ec491dd8 100644 --- a/libs/features/personalization/personalization.js +++ b/libs/features/personalization/personalization.js @@ -154,7 +154,7 @@ export function replacePlaceholders(value, placeholders) { if (!matches) return val; matches.forEach((match) => { const key = match.replace(/{{|}}/g, '').trim(); - if (placeholders[key]) val = val.replace(match, placeholders[key]); + if (placeholders?.[key]) val = val.replace(match, placeholders[key]); }); return val; } diff --git a/test/features/personalization/parseNestedPlaceholders.test.js b/test/features/personalization/parseNestedPlaceholders.test.js index 43a5d05071..24b391c3e8 100644 --- a/test/features/personalization/parseNestedPlaceholders.test.js +++ b/test/features/personalization/parseNestedPlaceholders.test.js @@ -1,5 +1,5 @@ import { expect } from '@esm-bundle/chai'; -import { parseNestedPlaceholders, createContent } from '../../../libs/features/personalization/personalization.js'; +import { parseNestedPlaceholders, createContent, replacePlaceholders } from '../../../libs/features/personalization/personalization.js'; import { getConfig } from '../../../libs/utils/utils.js'; const config = getConfig(); @@ -29,3 +29,15 @@ describe('test createContent', () => { expect(newContent.innerHTML).to.equal('50'); }); }); +describe('replacePlaceholders()', () => { + it('should replace placeholders', () => { + const str = 'Buy now and save {{promo-discount}}% off {{promo-product-name}}.'; + const newStr = replacePlaceholders(str, config.placeholders); + expect(newStr).to.equal('Buy now and save 50% off CC All Apps.'); + }); + it('should not break when there are no placeholders available', () => { + const str = 'For just {{promo-price}}, get 20+...'; + const newStr = replacePlaceholders(str, null); + expect(newStr).to.equal(str); + }); +});