Skip to content

Commit

Permalink
add assertion to deleteNotificationById and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Oct 18, 2024
1 parent 2e8c150 commit 3cf79b3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import * as OnChainNotifications from './services/onchain-notifications';
import type { INotification } from './types';
import type { UserStorage } from './types/user-storage/user-storage';
import * as Utils from './utils/utils';
import { processFeatureAnnouncement } from './processors';

Check failure on line 46 in packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

`./processors` import should occur before import of `./processors/process-notifications`

// Mock type used for testing purposes
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -606,6 +607,52 @@ describe('metamask-notifications - deleteNotificationById', () => {

expect(controller.state.metamaskNotificationsList).toHaveLength(0);
});

it('will throw if a notification is not found', async () => {
const { messenger } = mockNotificationMessenger();
const processedSnapNotification = processSnapNotification(
createMockSnapNotification(),
);
const controller = new NotificationServicesController({
messenger,
env: { featureAnnouncements: featureAnnouncementsEnv },
state: { metamaskNotificationsList: [processedSnapNotification] },
});

await expect(controller.deleteNotificationById('foo')).rejects.toThrow(
'The notification to be deleted does not exist.',
);

expect(controller.state.metamaskNotificationsList).toHaveLength(1);
});

it('will throw if the notification to be deleted is not locally persisted', async () => {
const { messenger } = mockNotificationMessenger();
const processedSnapNotification = processSnapNotification(
createMockSnapNotification(),
);
const processedFeatureAnnouncement = processFeatureAnnouncement(
createMockFeatureAnnouncementRaw(),
);
const controller = new NotificationServicesController({
messenger,
env: { featureAnnouncements: featureAnnouncementsEnv },
state: {
metamaskNotificationsList: [
processedFeatureAnnouncement,
processedSnapNotification,
],
},
});

await expect(
controller.deleteNotificationById(processedFeatureAnnouncement.id),
).rejects.toThrow(
'The notification type of "features_announcement" is not locally persisted, only the following types can use this function: snap.',
);

expect(controller.state.metamaskNotificationsList).toHaveLength(2);
});
});

describe('metamask-notifications - markMetamaskNotificationsAsRead()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ export const defaultState: NotificationServicesControllerState = {
isCheckingAccountsPresence: false,
};

const locallyPersistedNotificationTypes = new Set<string>([TRIGGER_TYPES.SNAP]);

export type NotificationServicesControllerGetStateAction =
ControllerGetStateAction<
typeof controllerName,
Expand Down Expand Up @@ -1194,6 +1196,16 @@ export default class NotificationServicesController extends BaseController<
'The notification to be deleted does not exist.',
);

assert(
locallyPersistedNotificationTypes.has(fetchedNotification.type),
`The notification type of "${
// notifications are guaranteed to have type properties which equate to strings
fetchedNotification.type as string
}" is not locally persisted, only the following types can use this function: ${[
...locallyPersistedNotificationTypes,
].join(', ')}.`,
);

const newList = this.state.metamaskNotificationsList.filter(
(notification) => notification.id !== id,
);
Expand Down

0 comments on commit 3cf79b3

Please sign in to comment.