Skip to content

Commit

Permalink
MWPW-146845 Setup Footer Promo for Localization (#2236)
Browse files Browse the repository at this point in the history
  • Loading branch information
meganthecoder authored May 1, 2024
1 parent 4460e96 commit 0df7b33
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
10 changes: 5 additions & 5 deletions libs/features/footer-promo.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}

Expand All @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions test/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ describe('Utils', () => {
const analytics = '<meta property="article:tag" content="Analytics">';
const commerce = '<meta property="article:tag" content="Commerce">';
const summit = '<meta property="article:tag" content="Summit">';
const promoConfig = { locale: { contentRoot: '/test/utils/mocks' } };
let oldHead;
let promoBody;
let taxonomyData;
Expand All @@ -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');
});
Expand Down

0 comments on commit 0df7b33

Please sign in to comment.