diff --git a/test/utils/utils.test.js b/test/utils/utils.test.js index 1c528f35f2..60fbac18d1 100644 --- a/test/utils/utils.test.js +++ b/test/utils/utils.test.js @@ -32,6 +32,18 @@ const stageDomainsMap = { }, '.business-graybox.adobe.com': { 'business.adobe.com': 'origin' }, }; +const stageDomainsMapWRegex = { + hostname: 'stage--milo--owner.hlx.page', + map: { + '^https://.*--milo--owner.hlx.page': { + '^https://www.adobe.com/acrobat': 'https://main--dc--adobecom.hlx.page', + '^https://business.adobe.com/blog': 'https://main--bacom-blog--adobecom.hlx.page', + '^https://business.adobe.com': 'https://business.stage.adobe.com', + '^https://www.adobe.com': 'origin', + }, + }, + +}; const prodDomains = ['www.adobe.com', 'business.adobe.com', 'blog.adobe.com', 'helpx.adobe.com', 'news.adobe.com']; const externalDomains = ['external1.com', 'external2.com']; const ogFetch = window.fetch; @@ -521,7 +533,7 @@ describe('Utils', () => { }); describe('stageDomainsMap', () => { - it('should convert links when stageDomainsMap provided', async () => { + it('should convert links when stageDomainsMap provided without regex', async () => { const stageConfig = { ...config, env: { name: 'stage' }, @@ -548,6 +560,34 @@ describe('Utils', () => { }); }); + it('should convert links when stageDomainsMap provided with regex', async () => { + const { hostname, map } = stageDomainsMapWRegex; + const stageConfigWRegex = { + ...config, + env: { name: 'stage' }, + stageDomainsMap: map, + }; + + Object.entries(map).forEach(([, domainsMap]) => { + const anchors = Object.keys(domainsMap).map((d) => utils.createTag('a', { href: d.replace('^', '') })); + const externalAnchors = externalDomains.map((url) => utils.createTag('a', { href: url })); + + utils.convertStageLinks({ + anchors: [...anchors, ...externalAnchors], + config: stageConfigWRegex, + hostname, + href: `https://${hostname}`, + }); + + anchors.forEach((a, index) => { + const expectedDomain = Object.values(domainsMap)[index]; + expect(a.href).to.contain(expectedDomain === 'origin' ? hostname : expectedDomain); + }); + + externalAnchors.forEach((a) => expect(a.href).to.equal(a.href)); + }); + }); + it('should not convert links when no stageDomainsMap provided', async () => { const stageConfig = { ...config,