Skip to content

Commit

Permalink
[MWPW-159328] handle a case where there are not palceholders availble
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Fedotov authored and Denys Fedotov committed Oct 2, 2024
1 parent 6cdc02a commit 6862509
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 ther are no placeholders availble', () => {
const str = 'For just {{promo-price}}, get 20+...';
const newStr = replacePlaceholders(str, null);
expect(newStr).to.equal(str);
});
});

0 comments on commit 6862509

Please sign in to comment.