Skip to content

Commit

Permalink
MWPW-155720: Dynamic nav ignore values (#2739)
Browse files Browse the repository at this point in the history
* Working, needs refactoring and tests

* Tests and mocks added

* Removing sinon and stub

* Clarifying the test statement

* Changes to some instead of reduce, tests not working

* Refactored logic, tests working, additional test

* Some test cleanup

* Clarifying function name

---------

Co-authored-by: milo-pr-merge[bot] <169241390+milo-pr-merge[bot]@users.noreply.github.com>
  • Loading branch information
JasonHowellSlavin and milo-pr-merge[bot] authored Sep 4, 2024
1 parent 8956fbe commit f7dc1e2
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
13 changes: 13 additions & 0 deletions libs/features/dynamic-navigation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { getMetadata } from '../utils/utils.js';

function isDynamicNavDisabled() {
const dynamicNavDisableValues = getMetadata('dynamic-nav-disable');
if (!dynamicNavDisableValues) return false;

const metadataPairsMap = dynamicNavDisableValues.split(',').map((pair) => pair.split(';'));
return metadataPairsMap.some(([metadataKey, metadataContent]) => {
const metaTagContent = getMetadata(metadataKey.toLowerCase());
return (metaTagContent
&& metaTagContent.toLowerCase() === metadataContent.toLowerCase());
});
}

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

if (metadataContent === 'entry') {
Expand Down
32 changes: 31 additions & 1 deletion test/features/dynamic-nav/dynamicNav.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,40 @@ describe('Dynamic nav', () => {
expect(url).to.equal('some-source-string');
});

it('Returns the pprovided url if it does not find an item in sessionStorage and dynamic nav is on', async () => {
it('Returns the provided url if it does not find an item in sessionStorage and dynamic nav is on', async () => {
document.head.innerHTML = await readFile({ path: './mocks/on.html' });
window.sessionStorage.removeItem('gnavSource');
const url = dynamicNav('gnav/aem-sites', 'bacom');
expect(url).to.equal('gnav/aem-sites');
});

it('Returns the provided url if it finds a metadata that matches items in the ignore list', async () => {
document.head.innerHTML = await readFile({ path: './mocks/on-ignore-match.html' });
const url = dynamicNav('gnav/aem-sites', 'bacom');
expect(url).to.equal('gnav/aem-sites');
});

it('Returns the provided url when ignore items match some metadata but not all', async () => {
document.head.innerHTML = await readFile({ path: './mocks/on-ignore-some-matches.html' });
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');
expect(url).to.equal('some-source-string');
});

it('Returns the sessionStorage url when dynamic nav ignore metadata is not found', async () => {
document.head.innerHTML = await readFile({ path: './mocks/on-ignore-misspelled.html' });
const url = dynamicNav('gnav/aem-sites', 'bacom');
expect(url).to.equal('some-source-string');
});

it('Returns the sessionStorage url when dynamic nav ignore metadata content is empty', async () => {
document.head.innerHTML = await readFile({ path: './mocks/on-ignore-no-content.html' });
const url = dynamicNav('gnav/aem-sites', 'bacom');
expect(url).to.equal('some-source-string');
});
});
3 changes: 3 additions & 0 deletions test/features/dynamic-nav/mocks/on-ignore-does-not-match.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<meta name="dynamic-nav" content="on">
<meta name="primaryproductname" content="Commerce Cloud">
<meta name="dynamic-nav-disable" content="primaryProductName;DX Solutions, caas:content-type;summary">
3 changes: 3 additions & 0 deletions test/features/dynamic-nav/mocks/on-ignore-match.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<meta name="dynamic-nav" content="on">
<meta name="primaryproductname" content="Dx Solutions">
<meta name="dynamic-nav-disable" content="primaryProductName;DX Solutions, caas:content-type;summary">
3 changes: 3 additions & 0 deletions test/features/dynamic-nav/mocks/on-ignore-misspelled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<meta name="dynamic-nav" content="on">
<meta name="primaryproductname" content="Dx Solutions">
<meta name="dynamic-nav-dise" content="primaryProductName;DX Solutions, caas:content-type;summary">
3 changes: 3 additions & 0 deletions test/features/dynamic-nav/mocks/on-ignore-no-content.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<meta name="dynamic-nav" content="on">
<meta name="primaryproductname" content="Dx Solutions">
<meta name="dynamic-nav-disable" content="">
5 changes: 5 additions & 0 deletions test/features/dynamic-nav/mocks/on-ignore-some-matches.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<meta name="dynamic-nav" content="on">
<meta name="primaryproductname" content="Dx Solutions">
<meta name="dynamic-nav-disable" content="primaryProductName;DX Solutions, caas:content-type;summary">
<meta name="caas:content" content="presentation">
<meta name="test" content="stub">

0 comments on commit f7dc1e2

Please sign in to comment.