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-158537 : Upgrade Universal Nav to version 1.4 #3367

Open
wants to merge 7 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
30 changes: 21 additions & 9 deletions libs/blocks/global-navigation/global-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,27 @@ export const CONFIG = {
name: 'profile',
attributes: {
isSignUpRequired: false,
messageEventListener: (event) => {
const { name, payload, executeDefaultAction } = event.detail;
if (name === 'System' && payload.subType === 'AppInitiated') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can fail fast here

if (name !== 'System') return;
switch (payload.subType) {
    case 'AppInitiated':
        // code
    case 'SignOut':
        // code
    default:
        // code
}

window.adobeProfile?.getUserProfile()
.then((data) => { setUserProfile(data); })
.catch(() => { setUserProfile({}); });
}
if (name === 'System' && payload.subType === 'SignOut') {
executeDefaultAction();
}
if (name === 'System' && payload.subType === 'ProfileSwitch') {
executeDefaultAction().then((profile) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is executeDefaultAction always a Promise?

if (profile) window.location.reload();
});
}
},
componentLoaderConfig: {
config: {
enableLocalSection: true,
enableProfileSwitcher: true,
miniAppContext: {
onMessage: (name, payload) => {
if (name === 'System' && payload.subType === 'AppInitiated') {
window.adobeProfile?.getUserProfile()
.then((data) => { setUserProfile(data); })
.catch(() => { setUserProfile({}); });
}
},
logger: {
trace: () => {},
debug: () => {},
Expand Down Expand Up @@ -118,6 +128,7 @@ export const CONFIG = {
callbacks: getConfig().jarvis?.callbacks,
},
},
cart: { name: 'cart' },
},
},
};
Expand Down Expand Up @@ -540,7 +551,7 @@ class Gnav {
return 'linux';
};

const unavVersion = new URLSearchParams(window.location.search).get('unavVersion') || '1.3';
const unavVersion = new URLSearchParams(window.location.search).get('unavVersion') || '1.4';
await Promise.all([
loadScript(`https://${environment}.adobeccstatic.com/unav/${unavVersion}/UniversalNav.js`),
loadStyles(`https://${environment}.adobeccstatic.com/unav/${unavVersion}/UniversalNav.css`, true),
Expand Down Expand Up @@ -640,14 +651,15 @@ class Gnav {
},
children: getChildren(),
isSectionDividerRequired: getConfig()?.unav?.showSectionDivider,
showTrayExperience: (!isDesktop.matches),
});

// Exposing UNAV config for consumers
CONFIG.universalNav.universalNavConfig = getConfiguration();
await window.UniversalNav(CONFIG.universalNav.universalNavConfig);
this.decorateAppPrompt({ getAnchorState: () => window.UniversalNav.getComponent?.('app-switcher') });
isDesktop.addEventListener('change', () => {
window.UniversalNav.reload(CONFIG.universalNav.universalNavConfig);
window.UniversalNav.reload(getConfiguration());
});
};

Expand Down
2 changes: 1 addition & 1 deletion libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ export async function loadIms() {
return;
}
const [unavMeta, ahomeMeta] = [getMetadata('universal-nav')?.trim(), getMetadata('adobe-home-redirect')];
const defaultScope = `AdobeID,openid,gnav${unavMeta && unavMeta !== 'off' ? ',pps.read,firefly_api,additional_info.roles,read_organizations' : ''}`;
const defaultScope = `AdobeID,openid,gnav${unavMeta && unavMeta !== 'off' ? ',pps.read,firefly_api,additional_info.roles,read_organizations,account_cluster.read' : ''}`;
const timeout = setTimeout(() => reject(new Error('IMS timeout')), imsTimeout || 5000);
window.adobeid = {
client_id: imsClientId,
Expand Down
30 changes: 30 additions & 0 deletions test/blocks/global-navigation/global-navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ describe('global navigation', () => {
});
window.UniversalNav = sinon.spy(() => Promise.resolve());
window.UniversalNav.reload = sinon.spy(() => Promise.resolve());
window.adobeProfile = { getUserProfile: sinon.spy(() => Promise.resolve({})) };
// eslint-disable-next-line no-underscore-dangle
window._satellite = { track: sinon.spy() };
window.alloy = () => new Promise((resolve) => {
Expand Down Expand Up @@ -430,6 +431,27 @@ describe('global navigation', () => {
expect(window.UniversalNav.reload.getCall(0)).to.exist;
});

it('should handle message events correctly', async () => {
// eslint-disable-next-line max-len
const mockEvent = (name, payload) => ({ detail: { name, payload, executeDefaultAction: sinon.spy(() => Promise.resolve(null)) } });
await createFullGlobalNavigation({ unavContent: 'on' });
const messageEventListener = window.UniversalNav.getCall(0).args[0].children
.map((c) => c.attributes.messageEventListener)
.find((listener) => listener);

const appInitiatedEvent = mockEvent('System', { subType: 'AppInitiated' });
messageEventListener(appInitiatedEvent);
expect(window.adobeProfile.getUserProfile.called).to.be.true;

const signOutEvent = mockEvent('System', { subType: 'SignOut' });
messageEventListener(signOutEvent);
expect(signOutEvent.detail.executeDefaultAction.called).to.be.true;

const profileSwitch = mockEvent('System', { subType: 'ProfileSwitch' });
messageEventListener(profileSwitch);
expect(profileSwitch.detail.executeDefaultAction.called).to.be.true;
});

it('should send the correct analytics events', async () => {
await createFullGlobalNavigation({ unavContent: 'on' });
const analyticsFn = window.UniversalNav.getCall(0)
Expand Down Expand Up @@ -484,6 +506,14 @@ describe('global navigation', () => {
expect(getUniversalNavLocale({ prefix: data.prefix })).to.equal(data.expectedLocale);
}
});

it('should pass enableProfileSwitcher to the profile component configuration', async () => {
await createFullGlobalNavigation({ unavContent: 'on' });
const profileConfig = window.UniversalNav.getCall(0).args[0].children
.find((c) => c.name === 'profile').attributes.componentLoaderConfig.config;

expect(profileConfig.enableProfileSwitcher).to.be.true;
});
});

describe('small desktop', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/blocks/global-navigation/test-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const analyticsTestData = {
'unc|click|markUnread': 'Mark Notification as unread',
};

export const unavVersion = '1.3';
export const unavVersion = '1.4';

export const unavLocalesTestData = Object.entries(LANGMAP).reduce((acc, curr) => {
const result = [];
Expand Down
Loading