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-142878 add events for milo post lcp & milo delayed #2881

Open
wants to merge 10 commits into
base: stage
Choose a base branch
from
9 changes: 5 additions & 4 deletions libs/features/google-login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const GOOGLE_ID = '530526366930-l874a90ipfkn26naa71r010u8epp39jt.apps.googleuser
const PLACEHOLDER = 'feds-googleLogin';
const WRAPPER = 'feds-profile';

const onToken = async (getMetadata, data) => {
const onToken = async (getMetadata, data, getConfig) => {
let destination;
const config = getConfig();
try {
destination = new URL(getMetadata('google-login-redirect'))?.href;
destination = typeof config.googleLoginURLCallback === 'function' ? new URL(config.googleLoginURLCallback()) : new URL(getMetadata('google-login-redirect'))?.href;
vhargrave marked this conversation as resolved.
Show resolved Hide resolved
vhargrave marked this conversation as resolved.
Show resolved Hide resolved
} catch {
// Do nothing
}
Expand All @@ -30,7 +31,7 @@ const onToken = async (getMetadata, data) => {
});
};

export default async function initGoogleLogin(loadIms, getMetadata, loadScript) {
export default async function initGoogleLogin(loadIms, getMetadata, loadScript, getConfig) {
try {
await loadIms();
} catch {
Expand All @@ -45,7 +46,7 @@ export default async function initGoogleLogin(loadIms, getMetadata, loadScript)

window.google?.accounts?.id?.initialize({
client_id: GOOGLE_ID,
callback: (data) => onToken(getMetadata, data),
callback: (data) => onToken(getMetadata, data, getConfig),
prompt_parent_id: PLACEHOLDER,
cancel_on_tap_outside: false,
});
Expand Down
6 changes: 3 additions & 3 deletions libs/scripts/delayed.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ export const loadPrivacy = async (getConfig, loadScript) => {
});
};

export const loadGoogleLogin = async (getMetadata, loadIms, loadScript) => {
export const loadGoogleLogin = async (getMetadata, loadIms, loadScript, getConfig) => {
const googleLogin = getMetadata('google-login')?.toLowerCase();
if (window.adobeIMS?.isSignedInUser() || !['mobile', 'desktop', 'on'].includes(googleLogin)) return;
const desktopViewport = window.matchMedia('(min-width: 900px)').matches;
if (googleLogin === 'mobile' && desktopViewport) return;
if (googleLogin === 'desktop' && !desktopViewport) return;

const { default: initGoogleLogin } = await import('../features/google-login.js');
initGoogleLogin(loadIms, getMetadata, loadScript);
initGoogleLogin(loadIms, getMetadata, loadScript, getConfig);
};

/**
Expand All @@ -73,7 +73,7 @@ const loadDelayed = ([
setTimeout(() => {
loadPrivacy(getConfig, loadScript);
loadJarvisChat(getConfig, getMetadata, loadScript, loadStyle);
loadGoogleLogin(getMetadata, loadIms, loadScript);
loadGoogleLogin(getMetadata, loadIms, loadScript, getConfig);
if (getMetadata('interlinks') === 'on') {
const { locale } = getConfig();
const path = `${locale.contentRoot}/keywords.json`;
Expand Down
3 changes: 1 addition & 2 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,7 @@ export function scrollToHashedElement(hash) {
}

export async function loadDeferred(area, blocks, config) {
const event = new Event(MILO_EVENTS.DEFERRED);
area.dispatchEvent(event);
area.dispatchEvent(new Event(MILO_EVENTS.DEFERRED));
vhargrave marked this conversation as resolved.
Show resolved Hide resolved

if (area !== document) {
return;
Expand Down
15 changes: 8 additions & 7 deletions test/features/google-login/google-login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from '@esm-bundle/chai';
import { readFile, setViewport } from '@web/test-runner-commands';
import initGoogleLogin from '../../../libs/features/google-login.js';
import { viewports } from '../../blocks/global-navigation/test-utilities.js';
import { getConfig } from '../../../libs/utils/utils.js';

describe('Google Login', () => {
let initializeSpy;
Expand Down Expand Up @@ -37,12 +38,12 @@ describe('Google Login', () => {
});

it('should create a placeholder to inject DOM markup', async () => {
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub());
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub(), getConfig);
expect(document.getElementById('feds-googleLogin')).to.exist;
});

it('should initialize and render the login element', async () => {
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub());
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub(), getConfig);
expect(initializeSpy.called).to.be.true;
expect(promptSpy.called).to.be.true;
expect(initializeSpy.getCall(0).args[0].prompt_parent_id).to.equal('feds-googleLogin');
Expand All @@ -59,14 +60,14 @@ describe('Google Login', () => {
const socialHeadlessSignInStub = sinon.stub(window.adobeIMS, 'socialHeadlessSignIn')
.returns(new Promise((resolve, reject) => { reject(); }));
const signInWithSocialProviderSpy = sinon.spy(window.adobeIMS, 'signInWithSocialProvider');
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub());
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub(), getConfig);
let onToken = initializeSpy.getCall(0).args[0].callback;
await onToken(sinon.stub(), sinon.stub());
await onToken(sinon.stub(), sinon.stub(), getConfig);
expect(signInWithSocialProviderSpy.called).to.be.true;

// Existing account
socialHeadlessSignInStub.returns(new Promise((resolve) => { resolve(); }));
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub());
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub(), getConfig);
onToken = initializeSpy.getCall(0).args[0].callback;
window.DISABLE_PAGE_RELOAD = true;
signInWithSocialProviderSpy.resetHistory();
Expand All @@ -81,9 +82,9 @@ describe('Google Login', () => {
it('should not initialize if IMS is not ready or user is already logged-in', async () => {
window.adobeIMS = window.adobeIMS || { isSignedInUser: () => {} };
const loggedInStub = sinon.stub(window.adobeIMS, 'isSignedInUser').returns(() => true);
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub());
await initGoogleLogin(sinon.stub(), sinon.stub(), sinon.stub(), getConfig);
expect(document.getElementById('feds-googleLogin')).not.to.exist;
await initGoogleLogin(Promise.reject, sinon.stub(), sinon.stub());
await initGoogleLogin(Promise.reject, sinon.stub(), sinon.stub(), getConfig);
expect(document.getElementById('feds-googleLogin')).not.to.exist;
loggedInStub.restore();
});
Expand Down
16 changes: 9 additions & 7 deletions test/scripts/delayed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ describe('Delayed', () => {
it('should load interlinks logic', async () => {
const clock = sinon.useFakeTimers({ toFake: ['setTimeout'] });
document.querySelector('head')?.insertAdjacentHTML('beforeend', '<meta name="interlinks" content="on">');
loadDelayed([getConfig, getMetadata, loadScript, loadStyle, loadIms]).then((module) => {
expect(module).to.exist;
expect(typeof module === 'object').to.equal(true);
});
loadDelayed([getConfig, getMetadata, loadScript, loadStyle, loadIms])
.then((module) => {
expect(module).to.exist;
expect(typeof module === 'object').to.equal(true);
vhargrave marked this conversation as resolved.
Show resolved Hide resolved
});
await clock.runAllAsync();
clock.restore();
});

it('should skip load interlinks logic when metadata is off', async () => {
const clock = sinon.useFakeTimers({ toFake: ['setTimeout'] });
document.head.querySelector('meta[name="interlinks"]')?.remove();
loadDelayed([getConfig, getMetadata, loadScript, loadStyle, loadIms]).then((module) => {
expect(module == null).to.equal(true);
});
loadDelayed([getConfig, getMetadata, loadScript, loadStyle, loadIms])
.then((module) => {
expect(module == null).to.equal(true);
});
await clock.runAllAsync();
clock.restore();
});
Expand Down
Loading