diff --git a/libs/features/footer-promo.js b/libs/features/footer-promo.js index 9854983afc..072f302b15 100644 --- a/libs/features/footer-promo.js +++ b/libs/features/footer-promo.js @@ -1,11 +1,11 @@ import { createTag, getConfig } from '../utils/utils.js'; import { decorateSectionAnalytics } from '../martech/attributes.js'; -async function getPromoFromTaxonomy(contentRoot) { +async function getPromoFromTaxonomy(contentRoot, doc) { const NAME_KEY = 'Name'; const FOOTER_PROMO_LINK_KEY = 'Footer Promo Link'; const taxonomyUrl = `${contentRoot}/taxonomy.json`; - const tags = [...document.head.querySelectorAll('meta[property="article:tag"]')].map((el) => el.content); + const tags = [...doc.head.querySelectorAll('meta[property="article:tag"]')].map((el) => el.content); if (!tags.length) return undefined; @@ -25,13 +25,13 @@ async function getPromoFromTaxonomy(contentRoot) { return undefined; } -export default async function initFooterPromo(footerPromoTag, footerPromoType) { +export default async function initFooterPromo(footerPromoTag, footerPromoType, doc = document) { const config = getConfig(); const { locale: { contentRoot } } = config; let href = footerPromoTag && `${contentRoot}/fragments/footer-promos/${footerPromoTag}`; if (footerPromoType === 'taxonomy') { - const promo = await getPromoFromTaxonomy(contentRoot); + const promo = await getPromoFromTaxonomy(contentRoot, doc); if (promo) href = promo; } @@ -41,7 +41,7 @@ export default async function initFooterPromo(footerPromoTag, footerPromoType) { const a = createTag('a', { href }, href); const div = createTag('div', null, a); const section = createTag('div', null, div); - document.querySelector('main > div:last-of-type').insertAdjacentElement('afterend', section); + doc.querySelector('main > div:last-of-type').insertAdjacentElement('afterend', section); await loadFragment(a); section.classList.add('section'); const sections = document.querySelectorAll('main > div'); diff --git a/libs/utils/utils.js b/libs/utils/utils.js index 5c802a6740..61f99ffcac 100644 --- a/libs/utils/utils.js +++ b/libs/utils/utils.js @@ -776,13 +776,13 @@ function decorateSections(el, isDoc) { return [...el.querySelectorAll(selector)].map(decorateSection); } -export async function decorateFooterPromo() { - const footerPromoTag = getMetadata('footer-promo-tag'); - const footerPromoType = getMetadata('footer-promo-type'); +export async function decorateFooterPromo(doc = document) { + const footerPromoTag = getMetadata('footer-promo-tag', doc); + const footerPromoType = getMetadata('footer-promo-type', doc); if (!footerPromoTag && footerPromoType !== 'taxonomy') return; const { default: initFooterPromo } = await import('../features/footer-promo.js'); - await initFooterPromo(footerPromoTag, footerPromoType); + await initFooterPromo(footerPromoTag, footerPromoType, doc); } let imsLoaded; diff --git a/test/utils/utils.test.js b/test/utils/utils.test.js index ee333fbb72..3c00ddb619 100644 --- a/test/utils/utils.test.js +++ b/test/utils/utils.test.js @@ -527,7 +527,6 @@ describe('Utils', () => { const analytics = ''; const commerce = ''; const summit = ''; - const promoConfig = { locale: { contentRoot: '/test/utils/mocks' } }; let oldHead; let promoBody; let taxonomyData; @@ -553,21 +552,21 @@ describe('Utils', () => { it('loads from metadata', async () => { document.head.innerHTML = favicon + ccxVideo; - await utils.decorateFooterPromo(promoConfig); + await utils.decorateFooterPromo(); const a = document.querySelector('main > div:last-of-type a'); expect(a.href).includes('/fragments/footer-promos/ccx-video-links'); }); it('loads from taxonomy in order on sheet', async () => { document.head.innerHTML = ccxVideo + typeTaxonomy + analytics + commerce + summit; - await utils.decorateFooterPromo(promoConfig); + await utils.decorateFooterPromo(); const a = document.querySelector('main > div:last-of-type a'); expect(a.href).includes('/fragments/footer-promos/commerce'); }); it('loads backup from tag when taxonomy has no promo', async () => { document.head.innerHTML = ccxVideo + typeTaxonomy + summit; - await utils.decorateFooterPromo(promoConfig); + await utils.decorateFooterPromo(); const a = document.querySelector('main > div:last-of-type a'); expect(a.href).includes('/fragments/footer-promos/ccx-video-links'); });