Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWPW-155723 - Adds group metadata to ensure dyanmic nav continuity #3124

Open
wants to merge 3 commits into
base: stage
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libs/features/dynamic-navigation/dynamic-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,28 @@
return foundValues.length ? foundValues : false;
}

function dynamicNavGroupMatches(groupMetaData) {
const storedGroup = window.sessionStorage.getItem('dynamicNavGroup');
if (groupMetaData && storedGroup) return storedGroup === groupMetaData;
return false;

Check warning on line 20 in libs/features/dynamic-navigation/dynamic-navigation.js

View check run for this annotation

Codecov / codecov/patch

libs/features/dynamic-navigation/dynamic-navigation.js#L20

Added line #L20 was not covered by tests
}

export default function dynamicNav(url, key) {
if (foundDisableValues()) return url;
const metadataContent = getMetadata('dynamic-nav');
const dynamicNavGroup = getMetadata('dynamic-nav-group');

if (metadataContent === 'entry') {
window.sessionStorage.setItem('gnavSource', url);
window.sessionStorage.setItem('dynamicNavKey', key);
if (dynamicNavGroup) window.sessionStorage.setItem('dynamicNavGroup', dynamicNavGroup);
return url;
}

if (metadataContent === 'on' && dynamicNavGroup) {
if (!dynamicNavGroupMatches(dynamicNavGroup)) return url;
}

if (metadataContent !== 'on' || key !== window.sessionStorage.getItem('dynamicNavKey')) return url;

return window.sessionStorage.getItem('gnavSource') || url;
Expand Down
8 changes: 7 additions & 1 deletion libs/features/dynamic-navigation/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ const createStatusWidget = (dynamicNavKey) => {
const currentSource = getCurrentSource(dynamicNavSetting, storedSource, authoredSource);
const dynamicNavDisableValues = getMetadata('dynamic-nav-disable');
const foundValues = foundDisableValues();
const status = getStatus(dynamicNavSetting, foundValues.length >= 1, storedSource);
const groupMetaSetting = getMetadata('dynamic-nav-group') || 'Group not set';
const groupsMatch = groupMetaSetting === window.sessionStorage.getItem('dynamicNavGroup');
const groupsMatchMessage = groupsMatch ? 'Yes' : 'No';
const isDisabled = foundValues.length >= 1 || (!groupsMatch && groupMetaSetting !== 'Group not set');
const status = getStatus(dynamicNavSetting, isDisabled, storedSource);
const statusWidget = createTag('div', { class: 'dynamic-nav-status' });

statusWidget.innerHTML = `
Expand All @@ -94,6 +98,8 @@ const createStatusWidget = (dynamicNavKey) => {
</div>
<p class="status">Status: <span>${status}</span></p>
<p class="setting">Setting: <span>${dynamicNavSetting}</span></p>
<p class="group">Group: <span>${groupMetaSetting}</span></p>
<p class="group-match">Group matches stored group: <span>${groupsMatchMessage}</span></p>
<p class="consumer-key">Consumer key: <span>${dynamicNavKey}</span></p>
<div class="nav-source-info">
<p>Authored and stored source match: <span>${authoredSource === currentSource}</span></p>
Expand Down
30 changes: 30 additions & 0 deletions test/features/dynamic-nav/dynamicNav.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ describe('Dynamic nav', () => {
expect(url).to.equal('gnav/aem-sites');
});

it('Returns the provided url when the group has not been set', async () => {
document.head.innerHTML = await readFile({ path: './mocks/on.html' });
const url = dynamicNav('gnav/aem-sites', 'bacom');
expect(url).to.equal('some-source-string');
});

it('Returns the provided url when the group does not match', async () => {
document.head.innerHTML = await readFile({ path: './mocks/on.html' });
const groupMeta = document.createElement('meta');
groupMeta.setAttribute('name', 'dynamic-nav-group');
groupMeta.setAttribute('content', 'test');
document.head.appendChild(groupMeta);

window.sessionStorage.setItem('dynamicNavGroup', 'no-test');
const url = dynamicNav('gnav/aem-sites', 'bacom');
expect(url).to.equal('gnav/aem-sites');
});

it('Returns the sessionStorage url when dynamic nav ignore items are present but do not match the metadata', async () => {
document.head.innerHTML = await readFile({ path: './mocks/on-ignore-does-not-match.html' });
const url = dynamicNav('gnav/aem-sites', 'bacom');
Expand All @@ -75,4 +93,16 @@ describe('Dynamic nav', () => {
const url = dynamicNav('gnav/aem-sites', 'bacom');
expect(url).to.equal('some-source-string');
});

it('Returns the sessionStorage url when the groups match', async () => {
document.head.innerHTML = await readFile({ path: './mocks/on.html' });
const groupMeta = document.createElement('meta');
groupMeta.setAttribute('name', 'dynamic-nav-group');
groupMeta.setAttribute('content', 'test');
document.head.appendChild(groupMeta);

window.sessionStorage.setItem('dynamicNavGroup', 'test');
const url = dynamicNav('gnav/aem-sites', 'bacom');
expect(url).to.equal('some-source-string');
});
});
70 changes: 69 additions & 1 deletion test/features/dynamic-nav/status.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('Dynamic Nav Status', () => {
beforeEach(async () => {
const conf = { dynamicNavKey: 'bacom' };
document.body.innerHTML = await readFile({ path: './mocks/status.html' });
document.head.innerHTML = '<meta name="dynamic-nav" content=""><meta name="gnav-source" content="">';
document.head.innerHTML = '<meta name="dynamic-nav" content=""><meta name="gnav-source" content=""><meta name="dynamic-nav-group" content="test">';
window.sessionStorage.setItem('dynamicNavGroup', 'test');
setConfig(conf);
});

Expand Down Expand Up @@ -159,6 +160,73 @@ describe('Dynamic Nav Status', () => {
expect(info.authoredSource).to.equal('/test');
expect(info.storedSource).to.equal('/test');
});

it('displays the correct information for a group match', () => {
document.querySelector('meta[name="dynamic-nav"]').setAttribute('content', 'on');
document.querySelector('meta[name="gnav-source"]').setAttribute('content', 'https://main--milo--adobecom.hlx/test');

dynamicNav();
status();

const statusWidget = document.querySelector('.dynamic-nav-status');
const group = statusWidget.querySelector('.group span');
const groupMatch = statusWidget.querySelector('.group-match span');

expect(group.innerText).to.equal('test');
expect(groupMatch.innerText).to.equal('Yes');
expect(statusWidget.classList.contains(ENABLED)).to.be.true;
});

it('displays the correct information for a group mismatch', () => {
document.querySelector('meta[name="dynamic-nav"]').setAttribute('content', 'on');
document.querySelector('meta[name="gnav-source"]').setAttribute('content', 'https://main--milo--adobecom.hlx/test');

window.sessionStorage.setItem('dynamicNavGroup', 'no-test');

dynamicNav();
status();

const statusWidget = document.querySelector('.dynamic-nav-status');
const group = statusWidget.querySelector('.group span');
const groupMatch = statusWidget.querySelector('.group-match span');

expect(group.innerText).to.equal('test');
expect(groupMatch.innerText).to.equal('No');
expect(statusWidget.classList.contains(INACTIVE)).to.be.true;
});

it('displays the correct information for no group being set', () => {
document.querySelector('meta[name="dynamic-nav"]').setAttribute('content', 'on');
document.querySelector('meta[name="gnav-source"]').setAttribute('content', 'https://main--milo--adobecom.hlx/test');

document.querySelector('meta[name="dynamic-nav-group"]').remove();
window.sessionStorage.setItem('dynamicNavGroup', 'no-test');

dynamicNav();
status();

const statusWidget = document.querySelector('.dynamic-nav-status');
const group = statusWidget.querySelector('.group span');
const groupMatch = statusWidget.querySelector('.group-match span');

expect(group.innerText).to.equal('Group not set');
expect(groupMatch.innerText).to.equal('No');
});

it('remains active when there is no group match but the nav is active', () => {
document.querySelector('meta[name="dynamic-nav"]').setAttribute('content', 'on');
document.querySelector('meta[name="gnav-source"]').setAttribute('content', 'https://main--milo--adobecom.hlx/test');

document.querySelector('meta[name="dynamic-nav-group"]').remove();
window.sessionStorage.setItem('dynamicNavGroup', 'no-test');
window.sessionStorage.setItem('gnavSource', GNAV_SOURCE);

dynamicNav();
status();

const statusWidget = document.querySelector('.dynamic-nav-status');
expect(statusWidget.classList.contains(ACTIVE)).to.be.true;
});
});

describe('disabled values', () => {
Expand Down
Loading