Skip to content

Commit

Permalink
unit test repair
Browse files Browse the repository at this point in the history
  • Loading branch information
vgoodric committed Mar 12, 2024
1 parent 2664595 commit 1575a7f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
35 changes: 19 additions & 16 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}
4 changes: 3 additions & 1 deletion test/features/personalization/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ 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' });

// 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({
Expand Down Expand Up @@ -174,6 +175,7 @@ describe('remove action', () => {
config.mep = {
override: '',
preview: true,
handleFragmentCommand,
};

expect(document.querySelector('.z-pattern')).to.not.be.null;
Expand Down
3 changes: 2 additions & 1 deletion test/features/personalization/deprecatedActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand All @@ -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 () => {
Expand Down

0 comments on commit 1575a7f

Please sign in to comment.