Skip to content

Commit

Permalink
MWPW-142290: Update get/setLibs for stage (#172)
Browse files Browse the repository at this point in the history
* Updating getLibs to Milo College format, adding testing for libs

* More tests, moving around vars

* Remove commented out code

* CR fixes
  • Loading branch information
JasonHowellSlavin authored Feb 15, 2024
1 parent 1feafe4 commit 45c418d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
24 changes: 9 additions & 15 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,15 @@
export const [setLibs, getLibs] = (() => {
let libs;
return [
(prodLibs) => {
const { hostname } = window.location;
const stageEnvs = ['localhost', 'hlx.page', 'hlx.live', 'business.stage.adobe.com'];
if (!stageEnvs.some((env) => hostname.includes(env))) {
libs = prodLibs;
} else {
const branch = new URLSearchParams(window.location.search).get('milolibs') || 'main';
if (branch === 'local') {
libs = 'http://localhost:6456/libs';
} else if (branch.indexOf('--') > -1) {
libs = `https://${branch}.hlx.live/libs`;
} else {
libs = `https://${branch}--milo--adobecom.hlx.live/libs`;
}
}
(prodLibs, location) => {
libs = (() => {
const { hostname, search } = location || window.location;
const branch = new URLSearchParams(search).get('milolibs') || 'main';
if (branch === 'main' && hostname === 'business.stage.adobe.com') return 'https://www.stage.adobe.com/libs';
if (!(hostname.includes('.hlx.') || hostname.includes('local'))) return prodLibs;
if (branch === 'local') return 'http://localhost:6456/libs';
return branch.includes('--') ? `https://${branch}.hlx.live/libs` : `https://${branch}--milo--adobecom.hlx.live/libs`;
})();
return libs;
}, () => libs,
];
Expand Down
63 changes: 63 additions & 0 deletions test/scripts/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect } from '@esm-bundle/chai';
import { setLibs } from '../../scripts/utils.js';

describe('Libs', () => {
it('Sets default Libs', () => {
const libs = setLibs('/libs');
expect(libs).to.equal('https://main--milo--adobecom.hlx.live/libs');
});

it('Does not support milolibs query param on prod', () => {
const location = {
hostname: 'business.adobe.com',
search: '?milolibs=foo',
};
const libs = setLibs('/libs', location);
expect(libs).to.equal('/libs');
});

it('Supports milolibs query param', () => {
const location = {
hostname: 'localhost',
search: '?milolibs=foo',
};
const libs = setLibs('/libs', location);
expect(libs).to.equal('https://foo--milo--adobecom.hlx.live/libs');
});

it('Supports milo stage libs with stage as host', () => {
const location = {
hostname: 'business.stage.adobe.com',
search: '',
};
const libs = setLibs('/libs', location);
expect(libs).to.equal('https://www.stage.adobe.com/libs');
});

it('Does not support milo stage libs on non prod stage hosts', () => {
const location = {
hostname: 'stage--bacom--adobecom.hlx.live',
search: '',
};
const libs = setLibs('/libs', location);
expect(libs).to.equal('https://main--milo--adobecom.hlx.live/libs');
});

it('Supports local milolibs query param', () => {
const location = {
hostname: 'localhost',
search: '?milolibs=local',
};
const libs = setLibs('/libs', location);
expect(libs).to.equal('http://localhost:6456/libs');
});

it('Supports forked milolibs query param', () => {
const location = {
hostname: 'localhost',
search: '?milolibs=awesome--milo--forkedowner',
};
const libs = setLibs('/libs', location);
expect(libs).to.equal('https://awesome--milo--forkedowner.hlx.live/libs');
});
});

0 comments on commit 45c418d

Please sign in to comment.