generated from adobecom/milo-college
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from adobecom/MWPW-163532-env-specific-links
MWPW-163532 - environment-specific partner finder gnav links
- Loading branch information
Showing
4 changed files
with
136 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
`); | ||
}); | ||
}); | ||
}); |