Skip to content

Commit

Permalink
Mobile gnav tests (#3433)
Browse files Browse the repository at this point in the history
* Fixed unit tests for gnav promo

* Prevent other tests from breaking

* Added a test case for safari

* Added an escape test for localnav
  • Loading branch information
sharmrj authored Jan 8, 2025
1 parent 490a349 commit 3b4ee98
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
12 changes: 12 additions & 0 deletions test/blocks/global-navigation/global-navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,5 +750,17 @@ describe('global navigation', () => {
headline.click();
expect(headline.getAttribute('aria-expanded')).to.equal('true');
});

it('adds the correct class when on Safari', async () => {
Object.defineProperty(navigator, 'userAgent', { get: () => 'Safari' });
await createFullGlobalNavigation({ globalNavigation: gnavWithlocalNav, viewport: 'mobile' });
const localNavTitle = document.querySelector(selectors.localNavTitle);
localNavTitle.click();
const localNav = document.querySelector(selectors.localNav);
const curtain = localNav.querySelector('.feds-localnav-curtain');
expect(document.body.classList.contains('disable-ios-scroll')).to.equal(true);
curtain.click();
expect(document.body.classList.contains('disable-ios-scroll')).to.equal(false);
});
});
});
2 changes: 1 addition & 1 deletion test/blocks/global-navigation/gnav-promo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Promo', () => {
it('is available if set up correctly', async () => {
const promoMeta = toFragment`<meta name="gnav-promo-source" content="http://localhost:2000/fragments/correct-promo-fragment">`;
document.head.append(promoMeta);
const nav = await createFullGlobalNavigation({ hasPromo: true });
const nav = await createFullGlobalNavigation({ hasPromo: true, imsInitialized: true });
expect(nav.block.classList.contains('has-promo')).to.be.true;
const asideElem = nav.block.querySelector('.aside.promobar');
expect(asideElem).to.exist;
Expand Down
11 changes: 11 additions & 0 deletions test/blocks/global-navigation/keyboard/keyboard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,17 @@ describe('keyboard navigation', () => {
const localnavTitle = document.querySelector('.feds-localnav-title');
expect(document.activeElement).to.equal(localnavTitle);
});
it('closes when escape is pressed', async () => {
await sendKeys({ press: 'Tab' });
await sendKeys({ press: 'Tab' });
await sendKeys({ press: 'Tab' });
await sendKeys({ press: 'Tab' });
await sendKeys({ press: 'Tab' });
await sendKeys({ press: 'Tab' });
await sendKeys({ press: 'Space' });
await sendKeys({ press: 'Escape' });
expect(document.querySelector('feds-localnav--active')).to.not.exist;
});
});
});
});
9 changes: 5 additions & 4 deletions test/blocks/global-navigation/test-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,9 @@ export const createFullGlobalNavigation = async ({
hasPromo,
hasBreadcrumbs = true,
unavContent = null,
imsInitialized = false,
} = {}) => {
const clock = sinon.useFakeTimers({
// Intercept setTimeout and call the function immediately
toFake: ['setTimeout'],
});
const clock = sinon.useFakeTimers({ shouldAdvanceTime: true });
setConfig({ ...config, ...customConfig });
await setViewport(viewports[viewport]);
window.lana = { log: stub() };
Expand All @@ -198,6 +196,7 @@ export const createFullGlobalNavigation = async ({
return null;
});
window.adobeIMS = {
initialized: imsInitialized,
isSignedInUser: stub().returns(signedIn),
getAccessToken: stub().returns('mock-access-token'),
getProfile: stub().returns(
Expand Down Expand Up @@ -228,9 +227,11 @@ export const createFullGlobalNavigation = async ({
const instancePromise = initGnav(document.body.querySelector('header'));

await clock.runToLastAsync();
clock.tick(1000);
const instance = await instancePromise;
const imsPromise = instance.imsReady();
await clock.runToLastAsync();
clock.tick(1000);
// We restore the clock here, because waitForElement uses setTimeout
clock.restore();

Expand Down

0 comments on commit 3b4ee98

Please sign in to comment.