Skip to content
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
63 changes: 63 additions & 0 deletions bundle/playwright/tests/mobile-sticky-slot.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect, test } from '@playwright/test';
import { testAtBreakpoints } from '../lib/breakpoints';
import { loadPage } from '../lib/load-page';
import { cmpAcceptAll } from '../lib/cmp';
import { articles } from '../fixtures/pages';
import { GuPage } from '../fixtures/pages/Page';

const { path } = articles[0] as unknown as GuPage;
testAtBreakpoints(['mobile']).forEach(({ width, height }) => {
test(`mobile sticky ad waits for StickyBottomBanner to be dismissed before loading`, async ({
page,
}) => {
await page.setViewportSize({ width, height });

await loadPage({ page, path, region: 'US' });
await cmpAcceptAll(page);
await loadPage({
page,
path,
region: 'US',
queryParams: { 'force-banner': '', adtest: 'mobileStickyTest' },
});

// Banner should be visible, ad slot should not exist
await expect(
page.locator('[name="StickyBottomBanner"] > *'),
).toBeVisible();
await expect(page.locator('#dfp-ad--mobile-sticky')).not.toBeAttached();

// Dismiss banner
await page.getByRole('button', { name: 'Collapse banner' }).click();
await page.getByText('Maybe later').click();
Copy link
Contributor

Choose a reason for hiding this comment

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

This text could change so is a bit fragile, but we don't have anything better here due to the way it appears in the DOM! If we find this test fails due to text changes, we should try to establish a more concrete identifier for this button (and the previous "Collapse banner" one)


// Banner hidden, ad slot should now appear
await expect(
page.locator('[name="StickyBottomBanner"]'),
).not.toBeVisible();
await expect(page.locator('#dfp-ad--mobile-sticky')).toBeAttached();
});
});

testAtBreakpoints(['mobile']).forEach(({ width, height }) => {
test(`mobile sticky responds to banner:none event at mobile breakpoint`, async ({
page,
}) => {
await page.setViewportSize({ width, height });
await loadPage({
page,
path,
region: 'US',
queryParams: {
adtest: 'mobileStickyTest',
},
});
await cmpAcceptAll(page);

await page.evaluate(() => {
document.dispatchEvent(new Event('banner:none'));
});

await expect(page.locator('#dfp-ad--mobile-sticky')).toBeAttached();
});
});
48 changes: 29 additions & 19 deletions bundle/src/insert/mobile-sticky.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { log } from '@guardian/libs';
import { createAdSlot } from '../lib/create-ad-slot';
import fastdom from '../lib/fastdom-promise';
import { shouldIncludeMobileSticky } from '../lib/header-bidding/utils';
Expand Down Expand Up @@ -31,27 +32,36 @@ const createAdWrapper = () => {
* Initialise mobile sticky ad slot
* @returns Promise
*/

const renderMobileStickySlot = async () => {
log('commercial', '🪵 Rendering MobileSticky');
const mobileStickyWrapper = createAdWrapper();
await fastdom.mutate(() => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- Is body really always defined?
if (document.body && mobileStickyWrapper) {
document.body.appendChild(mobileStickyWrapper);
}
});
if (mobileStickyWrapper) {
const mobileStickyAdSlot =
mobileStickyWrapper.querySelector<HTMLElement>(
'#dfp-ad--mobile-sticky',
);
if (mobileStickyAdSlot) {
void fillDynamicAdSlot(mobileStickyAdSlot, true);
}
}
};

export const init = (): Promise<void> => {
const handleBannerEvent = () => {
log('commercial', '🪵 Handle Banner Event');
void renderMobileStickySlot();
};

if (shouldIncludeMobileSticky()) {
const mobileStickyWrapper = createAdWrapper();
return fastdom
.mutate(() => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- Is body really always defined?
if (document.body && mobileStickyWrapper) {
document.body.appendChild(mobileStickyWrapper);
}
})
.then(() => {
if (mobileStickyWrapper) {
const mobileStickyAdSlot =
mobileStickyWrapper.querySelector<HTMLElement>(
'#dfp-ad--mobile-sticky',
);
if (mobileStickyAdSlot) {
void fillDynamicAdSlot(mobileStickyAdSlot, true);
}
}
});
document.addEventListener('banner:close', handleBannerEvent);
document.addEventListener('banner:none', handleBannerEvent);
}

return Promise.resolve();
Expand Down