Skip to content

Commit

Permalink
Test update for bootstrapper and navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
bandana147 committed Sep 19, 2024
1 parent 15b77cf commit 039fa9b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
10 changes: 1 addition & 9 deletions test/navigation/bootstrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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';
import { setConfig } from '../../libs/utils/utils.js';
import { mockRes } from '../blocks/global-navigation/test-utilities.js';

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

Expand All @@ -24,15 +25,6 @@ const blockConfig = {

const miloLibs = 'http://localhost:2000/libs';

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) => {
Expand Down
53 changes: 50 additions & 3 deletions test/navigation/navigation.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,75 @@
import { readFile } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import { stub, restore } from 'sinon';
import loadBlock from '../../libs/navigation/navigation.js';
import { setConfig } from '../../libs/utils/utils.js';
import { mockRes } from '../blocks/global-navigation/test-utilities.js';

document.body.innerHTML = await readFile({ path: './mocks/body.html' });
const miloLibs = 'http://localhost:2000/libs';

describe('Navigation component', async () => {
beforeEach(async () => {
stub(window, 'fetch').callsFake(async (url) => {
if (url.includes('/footer.plain.html')) return mockRes({ payload: await readFile({ path: '../blocks/region-nav/mocks/regions.html' }) });
if (url.includes('/federal/dev/gnav.plain.html')) return mockRes({ payload: await readFile({ path: './mocks/gnav.html' }) });

return null;
});
setConfig({ miloLibs, contentRoot: '/federal/dev' });
});

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

it('Renders the footer block', async () => {
await loadBlock({ authoringPath: '/federal/dev', footer: { privacyId: '12343' }, env: 'qa' }, 'http://localhost:2000');
const onReady = stub();
await loadBlock({ authoringPath: '/federal/dev', footer: { privacyId: '12343' }, env: 'qa', onReady }, 'http://localhost:2000');
const el = document.getElementsByTagName('footer');
expect(el).to.exist;
expect(onReady.called).to.be.true;
});

it('Renders the footer block should not load when config is not passed', async () => {
try {
await loadBlock({ authoringPath: '/federal/dev-new', env: 'qa', footer: { privacyId: '12343' } }, 'http://localhost:2000');
const el = document.getElementsByTagName('footer');
expect(el).to.not.exist;
} catch (e) {
// handle error
}
});

it('Renders the header block', async () => {
await loadBlock({ authoringPath: '/federal/dev', header: { imsClientId: 'fedsmilo' }, env: 'qa' }, 'http://localhost:2000');
await loadBlock({ authoringPath: '/federal/dev', header: { imsClientId: 'fedsmilo' }, env: 'prod' }, 'http://localhost:2000');
const el = document.getElementsByTagName('header');
expect(el).to.exist;
});

it('Does not render either header or footer if not found in configs', async () => {
const onError = stub();
document.body.innerHTML = await readFile({ path: './mocks/body.html' });
await loadBlock({ authoringPath: '/federal/dev', env: 'qa', onError }, 'http://localhost:2000');
const header = document.getElementsByTagName('header');
const footer = document.getElementsByTagName('footer');
expect(header).to.be.empty;
expect(footer).to.be.empty;
expect(onError.called).to.be.true;
});

it('Does not render either header or footer if configs is not passed', async () => {
document.body.innerHTML = await readFile({ path: './mocks/body.html' });
await loadBlock({ authoringPath: '/federal/dev', env: 'qa' }, 'http://localhost:2000');
await loadBlock();
const header = document.getElementsByTagName('header');
const footer = document.getElementsByTagName('footer');
expect(header).to.be.empty;
expect(footer).to.be.empty;
});

it('Renders the footer block with authoringpath passed in footer', async () => {
await loadBlock({ footer: { privacyId: '12343', authoringPath: '/federal/dev' }, env: 'qa' }, 'http://localhost:2000');
const el = document.getElementsByTagName('footer');
expect(el).to.exist;
});
});

0 comments on commit 039fa9b

Please sign in to comment.