Skip to content

Commit

Permalink
Fix for errors in dynamically loaded scripts in test cases (adobecom#…
Browse files Browse the repository at this point in the history
…2619)

* Fix for dynamic script loading in test cases

* Lint fix
  • Loading branch information
bandana147 authored Jul 24, 2024
1 parent c5fcc8e commit 0569bfd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
8 changes: 4 additions & 4 deletions libs/navigation/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const envMap = {
qa: 'https://feds--milo--adobecom.hlx.page',
};

export default async function loadBlock(configs = {}) {
const { footer, locale, env = 'prod' } = configs;
export default async function loadBlock(configs, customLib) {
const { footer, locale, env = 'prod' } = configs || {};
const branch = new URLSearchParams(window.location.search).get('navbranch');
const miloLibs = branch ? `https://${branch}--milo--adobecom.hlx.page` : envMap[env];
const miloLibs = branch ? `https://${branch}--milo--adobecom.hlx.page` : customLib || envMap[env];

// Relative path can't be used, as the script will run on consumer's app
const { default: bootstrapBlock } = await import(`${miloLibs}/libs/navigation/bootstrapper.js`);
Expand All @@ -27,7 +27,7 @@ export default async function loadBlock(configs = {}) {
locales: configs.locales || locales,
};
if (footer) {
const { footer: { authoringPath, privacyId, privacyLoadDelay = 3000 } } = configs;
const { authoringPath, privacyId, privacyLoadDelay = 3000 } = footer;
blockConfig.delay = privacyLoadDelay;
bootstrapBlock({ ...clientConfig, contentRoot: authoringPath, privacyId }, blockConfig.footer);
}
Expand Down
38 changes: 34 additions & 4 deletions test/navigation/bootstrapper.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,57 @@
import { readFile } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import { stub, useFakeTimers, restore } from 'sinon';
import loadBlock from '../../libs/navigation/bootstrapper.js';
import fetchedFooter from '../blocks/global-footer/mocks/fetched-footer.js';
import placeholders from '../blocks/global-navigation/mocks/placeholders.js';

document.body.innerHTML = await readFile({ path: './mocks/body.html' });

const blockConfig = {
name: 'global-footer',
targetEl: 'footer',
appendType: 'appendChild',
appendType: 'append',
footer: { authoringPath: '/federal/home', privacyLoadDelay: 0 },
};

const miloConfigs = {
origin: 'https://feds--milo--adobecom.hlx.page',
miloLibs: 'https://feds--milo--adobecom.hlx.page/libs',
miloLibs: 'http://localhost:2000/libs',
pathname: '/',
};

const mockRes = ({ payload, status = 200, ok = true } = {}) => new Promise((resolve) => {
resolve({
status,
ok,
json: () => payload,
text: () => payload,
});
});

describe('Bootstrapper', async () => {
beforeEach(async () => {
stub(window, 'fetch').callsFake(async (url) => {
if (url.includes('/footer')) {
return mockRes({
payload: fetchedFooter(
{ regionPickerHash: '/fragments/regions#langnav' },
),
});
}
if (url.includes('/placeholders')) return mockRes({ payload: placeholders });
if (url.includes('/footer.plain.html')) return mockRes({ payload: await readFile({ path: '../blocks/region-nav/mocks/regions.html' }) });
return null;
});
});

afterEach(() => {
restore();
});

it('Renders the footer block', async () => {
await loadBlock(miloConfigs, blockConfig);
const clock = sinon.useFakeTimers({
const clock = useFakeTimers({
toFake: ['setTimeout'],
shouldAdvanceTime: true,
});
Expand Down
4 changes: 2 additions & 2 deletions test/navigation/navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import loadBlock from '../../libs/navigation/navigation.js';

document.body.innerHTML = await readFile({ path: './mocks/body.html' });

describe('Bootstrapper', async () => {
describe('Navigation component', async () => {
it('Renders the footer block', async () => {
await loadBlock({ footer: { authoringPath: '/federal/home' }, env: 'qa' });
await loadBlock({ footer: { authoringPath: '/federal/home' }, env: 'qa' }, 'http://localhost:2000');
const el = document.getElementsByTagName('footer');
expect(el).to.exist;
});
Expand Down

0 comments on commit 0569bfd

Please sign in to comment.