diff --git a/src/helpers/cms-content-helper.js b/src/helpers/cms-content-helper.js index b036cc0f..7f5a7546 100644 --- a/src/helpers/cms-content-helper.js +++ b/src/helpers/cms-content-helper.js @@ -9,10 +9,12 @@ import { useMainStore } from '@/store'; * @returns {string} */ export function getStringWithCustomValues(str, customValueMap) { - let newString = str; - Object.keys(customValueMap).forEach((key) => { - newString = newString.replaceAll(`{custom:${key}}`, customValueMap[key]); - }); + let newString = str ?? ''; + if (customValueMap != null) { + Object.keys(customValueMap).forEach((key) => { + newString = newString.replaceAll(`{custom:${key}}`, customValueMap[key]); + }); + } return newString; } diff --git a/src/helpers/cms-content-helper.spec.js b/src/helpers/cms-content-helper.spec.js index 71d7f535..a69a10c5 100644 --- a/src/helpers/cms-content-helper.spec.js +++ b/src/helpers/cms-content-helper.spec.js @@ -2,14 +2,16 @@ import { getStringWithCustomValues } from '@/helpers/cms-content-helper.js'; describe('getStringWithCustomValues', () => { test.each([ - ['Hello, {custom:name}!', { name: 'John' }, 'Hello, John!']// , - // ['{custom:greeting}, {custom:name}!', { greeting: 'Hi', name: 'John' }, 'Hi, John!'], - // ['{custom:greeting}, {custom:name}!', { greeting: 'Hi' }, 'Hi, {custom:name}!'], - // ['Hello, {custom:name}!', {}, 'Hello, {custom:name}!'], - // ['Hello, world!', { name: 'John' }, 'Hello, world!'], - // ['', { name: 'John' }, ''], - // ['Hello, {custom:name}!', null, 'Hello, {custom:name}!'], - // [null, { name: 'John' }, null] + ['Hello, {custom:name}!', { name: 'John' }, 'Hello, John!'], + ['{custom:greeting}, {custom:name}!', { greeting: 'Hi', name: 'John' }, 'Hi, John!'], + ['{custom:greeting}, {custom:name}!', { greeting: 'Hi' }, 'Hi, {custom:name}!'], + ['Hello, {custom:name}!', {}, 'Hello, {custom:name}!'], + ['Hello, world!', { name: 'John' }, 'Hello, world!'], + ['', { name: 'John' }, ''], + ['Hello, {custom:name}!', null, 'Hello, {custom:name}!'], + ['Hello, {custom:name}!', undefined, 'Hello, {custom:name}!'], + [null, { name: 'John' }, ''], + [undefined, { name: 'John' }, ''] ])('getStringWithCustomValues(%s, %o) should return %s', (str, customValueMap, expected) => { expect(getStringWithCustomValues(str, customValueMap)).toBe(expected); });