Skip to content

ntp: fixed the empty state of activity widget when trackers are present #1677

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ export const activityExamples = {
</Activity>
),
},
'activity.noActivity.someTrackers': {
factory: () => (
<Activity expansion={'collapsed'} itemCount={0} trackerCount={56} toggle={noop('toggle')} batched={false}>
<Mock size={0}>
<ActivityBody canBurn={false} visibility={'visible'} />
</Mock>
</Activity>
),
},
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,13 @@ export class ActivityPage {
- paragraph: Past 7 days
`);
}

async hasTrackingInfoWithoutButtons() {
const { page } = this;
await expect(page.getByTestId('ActivityHeading')).toMatchAriaSnapshot(`
- img "Privacy Shield"
- heading "56 tracking attempts blocked" [level=2]
- paragraph: Past 7 days
`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,35 @@ test.describe('activity widget', () => {
});

await widget.hasRows(5);
await batching.removesItem(0);
await batching.itemRemovedViaPatch(0);
await widget.hasRows(4);
});
test('patching removes last item and maintains tracker count', async ({ page }, workerInfo) => {
test.info().annotations.push({
type: 'link',
description: 'https://app.asana.com/1/137249556945/project/1207414201589134/task/1210188026604205?focus=true',
});
test.info().annotations.push({
type: 'info',
description: `
This test simulates removing the last item in the list. When that occurs, the tracker count
should remain if it was greater than zero. The bug here was that we'd reset back to 'no tracking activity'.
`,
});
const ntp = NewtabPage.create(page, workerInfo);
await ntp.reducedMotion();

const widget = new ActivityPage(page, ntp);
const batching = new BatchingPage(page, ntp, widget);

await ntp.openPage({
additional: { feed: 'activity', 'activity.api': 'batched', platform: 'windows', activity: 'singleWithTrackers' },
});

await widget.hasRows(1);
await batching.removesItem({ index: 0, nextTrackerCount: 56 }); // from activity/mocks/activity.mocks.js
await widget.hasTrackingInfoWithoutButtons();
});
test('items are fetched to replace patched removals', async ({ page }, workerInfo) => {
const ntp = NewtabPage.create(page, workerInfo);
await ntp.reducedMotion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,28 @@ export class BatchingPage {
*
* @param {number} index - The index of the item to remove from the sample data array.
*/
async removesItem(index) {
async itemRemovedViaPatch(index) {
const data = generateSampleData(this.ap.entries);
data.splice(index, 1);
const update = toPatch(data);
await this.ntp.mocks.simulateSubscriptionMessage(sub('activity_onDataPatch'), update);
}

/**
* Simulates removing all items
* @param {object} params
* @param {number} params.index - The index of the item to remove from the sample data array.
* @param {number} params.nextTrackerCount
*/
async removesItem({ index, nextTrackerCount }) {
const { page } = this;
await page.locator('button[data-action="remove"]').nth(index).click();

const update = toPatch([]);
update.totalTrackersBlocked = nextTrackerCount;
await this.ntp.mocks.simulateSubscriptionMessage(sub('activity_onDataPatch'), update);
}

async fillsHoleWhenItemRemoved() {
if (this.ap.entries !== 6) throw new Error('this scenario expects 6 initial items');

Expand All @@ -131,7 +146,7 @@ export class BatchingPage {
});

// now remove the first item via subscription
await this.removesItem(0);
await this.itemRemovedViaPatch(0);

// now there must have been 2 calls
const [, second] = await this.ntp.mocks.waitForCallCount({ method: 'activity_getDataForUrls', count: 2 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ export function activityMockTransport() {
/** @type {import('../../../types/new-tab.ts').NewTabMessages['notifications']} */
const msg = /** @type {any} */ (_msg);
switch (msg.method) {
case 'activity_removeItem': {
// grab the tracker count of the current dataset before we alter it
const oldCount = dataset.activity.reduce((acc, item) => acc + item.trackingStatus.totalCount, 0);

// now filter the items
dataset.activity = dataset.activity.filter((x) => x.url !== msg.params.url);

// create the patch dataset, and use the original tracker count
const patchParams = toPatch(dataset.activity);
patchParams.totalTrackersBlocked = oldCount;

// simulate the native side pushing the fresh data back into the page.
setTimeout(() => {
const cb = subs.get('activity_onDataPatch');
cb(patchParams);
}, 0);
break;
}
default: {
console.warn('unhandled notification', msg);
}
Expand All @@ -53,6 +71,9 @@ export function activityMockTransport() {
if (sub === 'activity_onDataUpdate') {
subs.set('activity_onDataUpdate', cb);
}
if (sub === 'activity_onDataPatch') {
subs.set('activity_onDataPatch', cb);
}
if (sub === 'activity_onDataUpdate' && url.searchParams.has('flood')) {
let count = 0;
const int = setInterval(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ export const activityMocks = {
},
],
},
singleWithTrackers: {
activity: [
{
favicon: { src: 'selco-icon.png' },
url: 'https://example.com',
title: 'example.com',
etldPlusOne: 'example.com',
favorite: false,
trackersFound: true,
trackingStatus: {
trackerCompanies: [{ displayName: 'Google' }, { displayName: 'Facebook' }, { displayName: 'Amazon' }],
totalCount: 56,
},
history: [],
},
],
},
few: {
activity: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function ActivityHeading({ expansion, canExpand, itemCount, trackerCount,
const [formatter] = useState(() => new Intl.NumberFormat());
const adBlocking = useAdBlocking();

const none = itemCount === 0;
const someItems = itemCount > 0;
const none = itemCount === 0 && trackerCount === 0;
const some = itemCount > 0 || trackerCount > 0;
const trackerCountFormatted = formatter.format(trackerCount);

let allTimeString;
Expand All @@ -46,7 +46,7 @@ export function ActivityHeading({ expansion, canExpand, itemCount, trackerCount,
<img src={adBlocking ? './icons/shield-green.svg' : './icons/shield.svg'} alt="Privacy Shield" />
</span>
{none && <h2 className={styles.title}>{t('activity_noRecent_title')}</h2>}
{someItems && (
{some && (
<h2 className={styles.title}>
<Trans str={allTimeString} values={{ count: trackerCountFormatted }} />
</h2>
Expand All @@ -64,12 +64,12 @@ export function ActivityHeading({ expansion, canExpand, itemCount, trackerCount,
/>
</span>
)}
{itemCount === 0 && (
{none && (
<p className={cn(styles.subtitle, { [styles.indented]: !adBlocking })}>
{adBlocking ? t('activity_noRecentAdsAndTrackers_subtitle') : t('activity_noRecent_subtitle')}
</p>
)}
{itemCount > 0 && (
{some && (
<p className={cn(styles.subtitle, styles.indented, { [styles.uppercase]: !adBlocking })}>
{t('stats_feedCountBlockedPeriod')}
</p>
Expand Down
Loading