Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MWPW-159328] handle a case where there are not placeholders available #2998

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading