Skip to content

Commit

Permalink
Merge pull request #151 from adobecom/MWPW-163532-env-specific-links
Browse files Browse the repository at this point in the history
MWPW-163532 - environment-specific partner finder gnav links
  • Loading branch information
richardhand-fortedigital authored Dec 9, 2024
2 parents eb1db2a + 09c7bef commit 86373a3
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 22 deletions.
3 changes: 3 additions & 0 deletions edsdme/blocks/partners-navigation/partners-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
darkIcons,
} from './utilities/utilities.js';

import { applyGnavLinkRewriting } from '../../scripts/links.js';

// MWPW-157751
import { getLibs } from '../../scripts/utils.js'; // MWPW-157751

Expand Down Expand Up @@ -1038,6 +1040,7 @@ export default async function init(block) {
if (!content) return null;
block.classList.add('global-navigation'); // MWPW-157751
content = applyGnavPersonalization(content); // MWPW-157751
content = applyGnavLinkRewriting(content);
const gnav = new Gnav({
content,
block,
Expand Down
52 changes: 52 additions & 0 deletions edsdme/scripts/links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getConfig } from '../blocks/utils/utils.js';

/**
* Domain mappings where the key is the production domain,
* and the value is the corresponding stage domain.
*/
const domainMappings = {
'adobe.force.com': 'adobe--sfstage.sandbox.my.site.com',
'io-partners-dx.adobe.com': 'io-partners-dx.stage.adobe.com',
// add mappings here as necessary
};

/**
* Rewrite a link href domain based on production to stage domain mappings.
* @param {string} href - The link href to rewrite.
* @returns {string} - The rewritten link href, or the original if the environment is prod,
* there was a problem processing, or there is no domain mapping defined for it.
*/
export function rewriteLinkHref(href) {
const { env } = getConfig();

if (env.name === 'prod') return href;

let url;

try {
url = new URL(href);
} catch {
return href;
}

if (domainMappings[url.hostname]) {
url.hostname = domainMappings[url.hostname];
return url.toString();
}

return href;
}

/**
* Applies link rewriting to the global navigation,
* based on production to stage domain mappings.
* @param {HTMLElement} gnav - The global navigation.
*/
export function applyGnavLinkRewriting(gnav) {
const links = gnav.querySelectorAll('a[href]');
links.forEach((link) => {
link.href = rewriteLinkHref(link.href);
});

return gnav;
}
22 changes: 0 additions & 22 deletions edsdme/scripts/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ const PERSONALIZATION_CONDITIONS = {
'partner-level': (level) => PARTNER_LEVEL === level,
};

const SALES_FORCE_DOMAINS = {
stage: 'adobe--sfstage.sandbox.my.site.com',
prod: 'adobe.force.com',
};

function personalizePlaceholders(placeholders, context = document) {
Object.entries(placeholders).forEach(([key, value]) => {
const placeholderValue = COOKIE_OBJECT[key];
Expand Down Expand Up @@ -231,26 +226,9 @@ function personalizeProfile(gnav) {
processRenew(profile);
}

function rewriteSalesForceLinks(gnav) {
const { env } = getConfig();

if (env.name !== 'prod') {
const links = gnav.querySelectorAll('a[href]');
links.forEach((link) => {
const href = link.getAttribute('href');

if (href.includes(SALES_FORCE_DOMAINS.prod)) {
const newHref = href.replace(SALES_FORCE_DOMAINS.prod, SALES_FORCE_DOMAINS.stage);
link.setAttribute('href', newHref);
}
});
}
}

export function applyGnavPersonalization(gnav) {
if (!isMember()) return gnav;
const importedGnav = document.importNode(gnav, true);
rewriteSalesForceLinks(importedGnav);
personalizeMainNav(importedGnav);
personalizeProfile(importedGnav);
return importedGnav;
Expand Down
81 changes: 81 additions & 0 deletions test/scripts/links.jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* @jest-environment jsdom
*/

import { applyGnavLinkRewriting, rewriteLinkHref } from '../../edsdme/scripts/links.js';
import { getConfig } from '../../edsdme/blocks/utils/utils.js';

jest.mock('../../edsdme/blocks/utils/utils.js', () => ({ getConfig: jest.fn() }));

describe('Test links.js', () => {
beforeEach(() => {
getConfig.mockReturnValue({ env: { name: 'stage' } });
});

describe('rewriteLinkHref', () => {
test('should return link href unchanged in production environment', () => {
getConfig.mockReturnValue({ env: { name: 'prod' } });

const href = 'https://adobe.force.com/path';
const result = rewriteLinkHref(href);

expect(result).toBe(href);
});

test('should rewrite sales for link hrefs', () => {
const href = 'https://adobe.force.com/path';
const result = rewriteLinkHref(href);

expect(result).toBe('https://adobe--sfstage.sandbox.my.site.com/path');
});

test('should rewrite runtime link hrefs', () => {
const href = 'https://io-partners-dx.adobe.com/path';
const result = rewriteLinkHref(href);

expect(result).toBe('https://io-partners-dx.stage.adobe.com/path');
});

test('should return unchanged link hrefs if invalid', () => {
const href = 'invalid-url';
const result = rewriteLinkHref(href);

expect(result).toBe(href);
});

test('should return unchanged link hrefs if domain is not mapped', () => {
const href = 'https://unmapped-domain.com/path';
const result = rewriteLinkHref(href);

expect(result).toBe(href);
});
});

describe('applyGnavLinkRewriting', () => {
const gnav = document.createElement('div');
const gnavHTML = `
<a href="https://adobe.force.com/path"></a>
<a href="https://io-partners-dx.adobe.com/path"></a>
<a href="https://unmapped-domain.com/path"></a>
`;
gnav.innerHTML = gnavHTML;

test('should not rewrite links in the production environment', () => {
getConfig.mockReturnValue({ env: { name: 'prod' } });

const result = applyGnavLinkRewriting(gnav);

expect(result.innerHTML).toBe(gnavHTML);
});

test('should rewrite links in the stage environment', () => {
const result = applyGnavLinkRewriting(gnav);

expect(result.innerHTML).toBe(`
<a href="https://adobe--sfstage.sandbox.my.site.com/path"></a>
<a href="https://io-partners-dx.stage.adobe.com/path"></a>
<a href="https://unmapped-domain.com/path"></a>
`);
});
});
});

0 comments on commit 86373a3

Please sign in to comment.