Skip to content

Commit

Permalink
[MWPW-159328] handle a case where there are not placeholders available (
Browse files Browse the repository at this point in the history
adobecom#2998)

* [MWPW-159328] handle a case where there are not palceholders availble

* fixed typos

---------

Co-authored-by: Denys Fedotov <dfedotov@Denyss-MacBook-Pro.local>
  • Loading branch information
2 people authored and mokimo committed Oct 4, 2024
1 parent ee9d4a1 commit 5b6ed27
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 13 additions & 1 deletion test/features/personalization/parseNestedPlaceholders.test.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 5b6ed27

Please sign in to comment.