From 1575a7f2d4a687324070429e485905b67fb7c479 Mon Sep 17 00:00:00 2001 From: vgoodric Date: Mon, 11 Mar 2024 22:57:28 -0600 Subject: [PATCH] unit test repair --- .../personalization/personalization.js | 35 ++++++++++--------- test/features/personalization/actions.test.js | 4 ++- .../personalization/deprecatedActions.test.js | 3 +- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/libs/features/personalization/personalization.js b/libs/features/personalization/personalization.js index e22e336df6..f5a4665d5d 100644 --- a/libs/features/personalization/personalization.js +++ b/libs/features/personalization/personalization.js @@ -634,6 +634,24 @@ const createDefaultExperiment = (manifest) => ({ selectedVariant: { commands: [], fragments: [] }, }); +export function handleFragmentCommand(command, a) { + const config = getConfig(); + const { action, fragment, manifestPath } = command; + if (action === 'replace') { + a.href = fragment; + if (config.mep.preview) a.dataset.manifestId = manifestPath; + return fragment; + } + if (action === 'remove') { + if (config.mep.preview) { + a.parentElement.dataset.removedManifestId = manifestPath; + } else { + a.parentElement.remove(); + } + } + return false; +} + export async function applyPers(manifests) { const config = getConfig(); @@ -678,20 +696,5 @@ export async function applyPers(manifests) { }); if (!config?.mep) config.mep = {}; config.mep.martech = `|${pznVariants.join('--')}|${pznManifests.join('--')}`; - config.mep.handleFragmentCommand = (command, a) => { - const { action, fragment, manifestPath } = command; - if (action === 'replace') { - a.href = fragment; - if (config.mep.preview) a.dataset.manifestId = manifestPath; - return fragment; - } - if (action === 'remove') { - if (config.mep.preview) { - a.parentElement.dataset.removedManifestId = manifestPath; - } else { - a.parentElement.remove(); - } - } - return false; - }; + config.mep.handleFragmentCommand = handleFragmentCommand; } diff --git a/test/features/personalization/actions.test.js b/test/features/personalization/actions.test.js index f958cf265d..d7e50e7603 100644 --- a/test/features/personalization/actions.test.js +++ b/test/features/personalization/actions.test.js @@ -3,7 +3,7 @@ import { readFile } from '@web/test-runner-commands'; import { stub } from 'sinon'; import { getConfig, loadBlock } from '../../../libs/utils/utils.js'; import initFragments from '../../../libs/blocks/fragment/fragment.js'; -import { applyPers } from '../../../libs/features/personalization/personalization.js'; +import { applyPers, handleFragmentCommand } from '../../../libs/features/personalization/personalization.js'; document.head.innerHTML = await readFile({ path: './mocks/metadata.html' }); document.body.innerHTML = await readFile({ path: './mocks/personalization.html' }); @@ -11,6 +11,7 @@ document.body.innerHTML = await readFile({ path: './mocks/personalization.html' // Add custom keys so tests doesn't rely on real data const config = getConfig(); config.env = { name: 'prod' }; +config.mep = { handleFragmentCommand }; const getFetchPromise = (data, type = 'json') => new Promise((resolve) => { resolve({ @@ -174,6 +175,7 @@ describe('remove action', () => { config.mep = { override: '', preview: true, + handleFragmentCommand, }; expect(document.querySelector('.z-pattern')).to.not.be.null; diff --git a/test/features/personalization/deprecatedActions.test.js b/test/features/personalization/deprecatedActions.test.js index ed4fc3aaf1..9f0ed6dd21 100644 --- a/test/features/personalization/deprecatedActions.test.js +++ b/test/features/personalization/deprecatedActions.test.js @@ -3,7 +3,7 @@ import { readFile } from '@web/test-runner-commands'; import { stub } from 'sinon'; import { getConfig } from '../../../libs/utils/utils.js'; import initFragments from '../../../libs/blocks/fragment/fragment.js'; -import { applyPers } from '../../../libs/features/personalization/personalization.js'; +import { applyPers, handleFragmentCommand } from '../../../libs/features/personalization/personalization.js'; document.head.innerHTML = await readFile({ path: './mocks/metadata.html' }); document.body.innerHTML = await readFile({ path: './mocks/personalization.html' }); @@ -25,6 +25,7 @@ describe('Functional Test', () => { // Add custom keys so tests doesn't rely on real data const config = getConfig(); config.env = { name: 'prod' }; + config.mep = { handleFragmentCommand }; }); it('replaceContent should replace an element with a fragment', async () => {