Skip to content

Commit

Permalink
MWPW-152455 added handling of "no placeholders" case for parsePlaceho…
Browse files Browse the repository at this point in the history
…lders() (#3007)

* added no placeholders case for parsePlaceholders()

* added fix to a case where no placeholder was found

* added test fix

---------

Co-authored-by: Denys Fedotov <dfedotov@Denyss-MacBook-Pro.local>
  • Loading branch information
denlight and Denys Fedotov authored Oct 22, 2024
1 parent ba4bd23 commit 2ba175c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
14 changes: 8 additions & 6 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ export async function createMartechMetadata(placeholders, config, column) {
}

/* c8 ignore start */
function parsePlaceholders(placeholders, config, selectedVariantName = '') {
export function parsePlaceholders(placeholders, config, selectedVariantName = '') {
if (!placeholders?.length || selectedVariantName === 'default') return config;
const valueNames = [
selectedVariantName.toLowerCase(),
Expand All @@ -655,17 +655,19 @@ function parsePlaceholders(placeholders, config, selectedVariantName = '') {
'value',
'other',
];
const [val] = Object.entries(placeholders[0])
.find(([key]) => valueNames.includes(key.toLowerCase()));
if (val) {
const keys = placeholders?.length ? Object.entries(placeholders[0]) : [];
const keyVal = keys.find(([key]) => valueNames.includes(key.toLowerCase()));
const key = keyVal?.[0];

if (key) {
const results = placeholders.reduce((res, item) => {
res[item.key] = item[val];
res[item.key] = item[key];
return res;
}, {});
config.placeholders = { ...(config.placeholders || {}), ...results };
}

createMartechMetadata(placeholders, config, val);
createMartechMetadata(placeholders, config, key);

return config;
}
Expand Down
24 changes: 23 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, replacePlaceholders } from '../../../libs/features/personalization/personalization.js';
import { parseNestedPlaceholders, createContent, replacePlaceholders, parsePlaceholders } from '../../../libs/features/personalization/personalization.js';
import { getConfig } from '../../../libs/utils/utils.js';

const config = getConfig();
Expand All @@ -10,6 +10,7 @@ config.placeholders = {
'promo-description': 'For just {{promo-price}}, get 20+...',
'promo-price': 'US$49.99',
};
config.locale = { region: 'US', ietf: 'en-US' };
describe('test different values for parseNestedPlaceholders', () => {
it('should update placeholders', () => {
parseNestedPlaceholders(config);
Expand Down Expand Up @@ -53,3 +54,24 @@ describe('replacePlaceholders()', () => {
expect(newStr).to.equal(str);
});
});

describe('parsePlaceholders()', () => {
it('should parse placeholders', () => {
const response = parsePlaceholders([
{
'en-US': 'bar',
key: 'foo',
},
], config);
expect(response.placeholders.foo).to.equal('bar');
});
it('should not break when there are no placeholders available', () => {
const response = parsePlaceholders([
{
fr: 'bar',
key: 'foo',
},
], config);
expect(response.placeholders).to.exist;
});
});

0 comments on commit 2ba175c

Please sign in to comment.