Skip to content

Commit

Permalink
[MWPW-161563] CDT Overlap in Global and APAC Promotion file (#3168)
Browse files Browse the repository at this point in the history
* Use schedule as source for CDT

* seperate start and end

* Added unit tests
  • Loading branch information
rahulgupta999 authored Nov 12, 2024
1 parent 82bf330 commit bf847a0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
37 changes: 28 additions & 9 deletions libs/features/cdt/cdt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getMetadata, getConfig, createTag } from '../../utils/utils.js';
import { getMetadata, getConfig, createTag, getMepEnablement } from '../../utils/utils.js';
import getPromoManifests from '../personalization/promo-utils.js';
import { replaceKey } from '../placeholders.js';

function loadCountdownTimer(
Expand Down Expand Up @@ -89,15 +90,23 @@ function isMobile() {
return window.matchMedia('(max-width: 767px)').matches;
}

export default async function initCDT(el, classList) {
const placeholders = ['cdt-ends-in', 'cdt-days', 'cdt-hours', 'cdt-mins'];
const [cdtLabel, cdtDays, cdtHours, cdtMins] = await Promise.all(
placeholders.map((placeholder) => replaceKey(placeholder, getConfig())),
);
function getCDTTimeRange() {
const promoEnabled = getMepEnablement('manifestnames', 'promo');
const PAGE_URL = new URL(window.location.href);
const persManifests = getPromoManifests(promoEnabled, PAGE_URL.searchParams);
let cdtMetadata = null;
persManifests?.forEach((manifest) => {
if (manifest.disabled) return;
if (!manifest.event.cdtStart || !manifest.event.cdtEnd) return;

cdtMetadata = `${manifest.event.cdtStart},${manifest.event.cdtEnd}`;
});

const cdtMetadata = getMetadata('countdown-timer');
if (cdtMetadata === null) {
throw new Error('Metadata for countdown-timer is not available');
if (!cdtMetadata) {
cdtMetadata = getMetadata('countdown-timer');
if (cdtMetadata === null) {
throw new Error('Metadata for countdown-timer is not available');
}
}

const cdtRange = cdtMetadata.split(',');
Expand All @@ -114,6 +123,16 @@ export default async function initCDT(el, classList) {
throw new Error('Invalid format for countdown timer range');
}

return timeRangesEpoch;
}

export default async function initCDT(el, classList) {
const placeholders = ['cdt-ends-in', 'cdt-days', 'cdt-hours', 'cdt-mins'];
const [cdtLabel, cdtDays, cdtHours, cdtMins] = await Promise.all(
placeholders.map((placeholder) => replaceKey(placeholder, getConfig())),
);

const timeRangesEpoch = getCDTTimeRange();
const cdtDiv = createTag('div', { class: 'countdown-timer' }, null, { parent: el });
cdtDiv.classList.add(isMobile() ? 'vertical' : 'horizontal');
if (classList.contains('dark')) cdtDiv.classList.add('dark');
Expand Down
4 changes: 3 additions & 1 deletion libs/features/personalization/promo-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ const getRegionalPromoManifests = (manifestNames, region, searchParams) => {
}
return schedule.split(',')
.map((manifest) => {
const [name, start, end, manifestPath, locales] = manifest.trim().split('|').map((s) => s.trim());
const [name, start, end, manifestPath, locales, cdtStart, cdtEnd] = manifest.trim().split('|').map((s) => s.trim());
if (attachedManifests.includes(name) && isManifestWithinLocale(locales)) {
const event = {
name,
start: GMTStringToLocalDate(start),
end: GMTStringToLocalDate(end),
cdtStart,
cdtEnd,
};
const disabled = isDisabled(event, searchParams);
return { manifestPath, disabled, event };
Expand Down
12 changes: 12 additions & 0 deletions test/features/cdt/cdt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ describe('CDT test start is equal to end date', () => {
expect(container.querySelectorAll('.timer-label')).to.have.lengthOf(0);
});
});

describe('CDT test start is equal to end date', () => {
before(() => {
document.head.innerHTML = '<meta name="manifestnames" content="black-friday-offer,twp-cct"><meta name="schedule" content=", black-friday-offer | 2024-03-22T05:00:00 | 2038-03-31T05:00:00 | https://main--milo--adobecom.hlx.page/drafts/nala/features/promotions/manifests/promo-with-fragments-insert.json | | 2024-08-26T12:00:00 | 2026-08-30T00:00:00, mwpw-159157-delayed-modal-space-between | 2024-09-26T00:00:00 | 2026-11-30T00:00:00 | https://main--milo--adobecom.hlx.live/fragments/mirafedas/promos/2024/global/delayed-modal-promo/delayed-modal-promo.json | | |">';
});
it('check for countdown-timer meta data by MEP', async () => {
const container = document.getElementById('cdt-container');
await loadCDT(container, container.classList);
expect(container.querySelectorAll('.timer-label')).to.have.lengthOf(1);
container.innerHTML = '';
});
});
2 changes: 1 addition & 1 deletion test/features/personalization/mocks/head-schedule.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<meta name="manifestnames" content="pre-black-friday-global,black-friday-global,cyber-monday,bf-us">
<meta name="emea_manifestnames" content="bf-de">
<meta name="americas_manifestnames" content="bf-us">
<meta name="schedule" content="pre-black-friday-global | 2000-11-01T00:00:00 | 2300-12-15T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/pre-black-friday/manifest-global.json, black-friday-global | 2000-12-15T00:00:00 | 2000-12-31T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/black-friday/manifest-global.json">
<meta name="schedule" content="pre-black-friday-global | 2000-11-01T00:00:00 | 2300-12-15T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/pre-black-friday/manifest-global.json, black-friday-global | 2000-12-15T00:00:00 | 2000-12-31T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/black-friday/manifest-global.json | | 2024-08-26T12:00:00 | 2026-08-30T00:00:00">
<meta name="emea_schedule" content="bf-de | 2000-11-01T00:00:00 | 2300-12-15T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/bf-de.json | de;ch, black-friday-de | 2000-12-15T00:00:00 | 2300-12-31T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/black-friday/manifest-de.json">
<meta name="americas_schedule" content="bf-us | 2000-11-01T00:00:00 | 2300-12-15T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/bf-us.json | ca;us, bf-ca | 2000-11-01T00:00:00 | 2300-12-15T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/bf-ca.json | ca,black-friday-us | 2000-12-15T00:00:00 | 2300-12-31T00:00:00 | https://main--milo--adobecom.hlx.page/promos/2023/black-friday/black-friday/manifest-us.json">
6 changes: 6 additions & 0 deletions test/features/personalization/promo-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ describe('getPromoManifests', () => {
name: 'bf-us',
start: new Date('2000-11-01T00:00:00.000Z'),
end: new Date('2300-12-15T00:00:00.000Z'),
cdtEnd: undefined,
cdtStart: undefined,
},
},
{
Expand All @@ -85,6 +87,8 @@ describe('getPromoManifests', () => {
name: 'pre-black-friday-global',
start: new Date('2000-11-01T00:00:00.000Z'),
end: new Date('2300-12-15T00:00:00.000Z'),
cdtEnd: undefined,
cdtStart: undefined,
},
},
{
Expand All @@ -94,6 +98,8 @@ describe('getPromoManifests', () => {
name: 'black-friday-global',
start: new Date('2000-12-15T00:00:00.000Z'),
end: new Date('2000-12-31T00:00:00.000Z'),
cdtEnd: '2026-08-30T00:00:00',
cdtStart: '2024-08-26T12:00:00',
},
},
];
Expand Down

0 comments on commit bf847a0

Please sign in to comment.