Skip to content

Commit 574a883

Browse files
committed
add test for triggered re-fetch
1 parent 7f9d95a commit 574a883

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

special-pages/pages/new-tab/app/activity/ActivityProvider.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,27 @@ export function SignalStateProvider({ children }) {
241241
activity.value = normalizeItems(activity.value, evt.data);
242242
});
243243
});
244-
// console.log('<--', document.visibilityState);
245-
document.addEventListener('visibilitychange', () => {
244+
const handler = () => {
246245
if (document.visibilityState === 'visible') {
246+
console.log('will fetch');
247247
service
248248
.triggerDataFetch()
249249
// eslint-disable-next-line promise/prefer-await-to-then
250250
.catch((e) => console.error('trigger fetch errored', e));
251251
}
252-
});
252+
};
253+
254+
(() => {
255+
// export the event in tests
256+
if (window.__playwright_01) {
257+
/** @type {any} */ (window).__trigger_document_visibilty__ = handler;
258+
}
259+
})();
260+
261+
document.addEventListener('visibilitychange', handler);
253262
return () => {
254-
console.log('did cleanup');
255263
unsub();
264+
document.removeEventListener('visibilitychange', handler);
256265
};
257266
});
258267

special-pages/pages/new-tab/app/activity/integration-tests/activity.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,26 @@ test.describe('activity widget', () => {
9292
await ap.didRender();
9393
await ap.listsAtMost3TrackerCompanies();
9494
});
95+
test('after rendering and navigating to a new tab, data is re-requested on return', async ({ page }, workerInfo) => {
96+
const ntp = NewtabPage.create(page, workerInfo);
97+
const ap = new ActivityPage(page, ntp);
98+
await ntp.reducedMotion();
99+
await ntp.openPage({ additional: { feed: 'activity' } });
100+
await ap.didRender();
101+
102+
// Open a new tab and navigate it to about:blank
103+
await ntp.mocks.waitForCallCount({ method: 'activity_getData', count: 1 });
104+
const newTab = await page.context().newPage();
105+
await newTab.goto('about:blank');
106+
107+
// Bring the first tab back into focus
108+
await page.bringToFront();
109+
110+
await page.evaluate(() => {
111+
// @ts-expect-error - testing only property
112+
const fn = window.__trigger_document_visibilty__;
113+
fn?.();
114+
});
115+
await ntp.mocks.waitForCallCount({ method: 'activity_getData', count: 2 });
116+
});
95117
});

0 commit comments

Comments
 (0)