Skip to content

Commit 9f24308

Browse files
feat: add tests and utils
1 parent 670fcdc commit 9f24308

File tree

10 files changed

+77
-59
lines changed

10 files changed

+77
-59
lines changed

packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ describe('metamask-notifications - constructor()', () => {
206206
return mocks;
207207
};
208208

209-
it('initializes push notifications', async () => {
209+
it('initialises push notifications', async () => {
210210
const { mockEnablePushNotifications } =
211211
arrangeActInitialisePushNotifications();
212212

packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,6 @@ export type NotificationServicesControllerDisableNotificationServices = {
198198
handler: NotificationServicesController['disableNotificationServices'];
199199
};
200200

201-
export type NotificationServicesControllerSelectIsNotificationServicesEnabled =
202-
{
203-
type: `${typeof controllerName}:selectIsNotificationServicesEnabled`;
204-
handler: NotificationServicesController['selectIsNotificationServicesEnabled'];
205-
};
206-
207201
export type NotificationServicesControllerGetNotificationsByType = {
208202
type: `${typeof controllerName}:getNotificationsByType`;
209203
handler: NotificationServicesController['getNotificationsByType'];
@@ -219,7 +213,6 @@ export type Actions =
219213
| NotificationServicesControllerGetStateAction
220214
| NotificationServicesControllerUpdateMetamaskNotificationsList
221215
| NotificationServicesControllerDisableNotificationServices
222-
| NotificationServicesControllerSelectIsNotificationServicesEnabled
223216
| NotificationServicesControllerGetNotificationsByType
224217
| NotificationServicesControllerDeleteNotificationsById;
225218

@@ -582,11 +575,6 @@ export default class NotificationServicesController extends BaseController<
582575
this.disableNotificationServices.bind(this),
583576
);
584577

585-
this.messagingSystem.registerActionHandler(
586-
`${controllerName}:selectIsNotificationServicesEnabled`,
587-
this.selectIsNotificationServicesEnabled.bind(this),
588-
);
589-
590578
this.messagingSystem.registerActionHandler(
591579
`${controllerName}:getNotificationsByType`,
592580
this.getNotificationsByType.bind(this),
@@ -664,18 +652,6 @@ export default class NotificationServicesController extends BaseController<
664652
}
665653
}
666654

667-
/**
668-
* Retrieves the current enabled state of MetaMask notifications.
669-
*
670-
* This method directly returns the boolean value of `isMetamaskNotificationsEnabled`
671-
* from the controller's state, indicating whether MetaMask notifications are currently enabled.
672-
*
673-
* @returns The enabled state of MetaMask notifications.
674-
*/
675-
public selectIsNotificationServicesEnabled(): boolean {
676-
return this.state.isNotificationServicesEnabled;
677-
}
678-
679655
/**
680656
* Sets the state of notification creation process.
681657
*

packages/notification-services-controller/src/NotificationServicesController/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ export * as Constants from './constants';
1212
export * from './constants';
1313
export * as Mocks from './__fixtures__';
1414
export * as UI from './ui';
15-
export * from '../shared/to-raw-notification';
15+
export * from '../shared';

packages/notification-services-controller/src/NotificationServicesController/processors/process-notifications.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { processNotification } from './process-notifications';
1+
import {
2+
processNotification,
3+
safeProcessNotification,
4+
} from './process-notifications';
25
import { createMockFeatureAnnouncementRaw } from '../__fixtures__/mock-feature-announcements';
36
import { createMockNotificationEthSent } from '../__fixtures__/mock-raw-notifications';
47
import { createMockSnapNotification } from '../__fixtures__/mock-snap-notification';
@@ -34,3 +37,20 @@ describe('process-notifications - processNotification()', () => {
3437
);
3538
});
3639
});
40+
41+
describe('process-notifications - safeProcessNotification()', () => {
42+
// More thorough tests are found in the specific process
43+
it('maps On Chain Notification to shared Notification Type', () => {
44+
const result = safeProcessNotification(createMockNotificationEthSent());
45+
expect(result).toBeDefined();
46+
});
47+
48+
it('returns undefined for a notification unable to process', () => {
49+
const rawNotification = createMockNotificationEthSent();
50+
51+
// Testing Mock with invalid notification type
52+
rawNotification.type = 'FAKE_NOTIFICATION_TYPE' as TRIGGER_TYPES.ETH_SENT;
53+
const result = safeProcessNotification(rawNotification);
54+
expect(result).toBeUndefined();
55+
});
56+
});

packages/notification-services-controller/src/NotificationServicesController/utils/utils.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ describe('metamask-notifications/utils - initializeUserStorage()', () => {
4141
);
4242
assertEmptyStorage(userStorageTest2);
4343
});
44+
45+
it('cleans User Storage if there are erroneous accounts', () => {
46+
const mockAddress = ADDRESS_1;
47+
const badAddress = '0xtb1qkw6c6f9lql679spp8qjfg3u6qrcdp5a6wqe35y';
48+
const userStorage = Utils.initializeUserStorage(
49+
[{ address: mockAddress }, { address: badAddress }],
50+
true,
51+
);
52+
53+
expect(userStorage[mockAddress.toLowerCase()]).toBeDefined();
54+
expect(userStorage[badAddress.toLowerCase()]).toBeUndefined(); // Removed bad address
55+
});
4456
});
4557

4658
describe('metamask-notifications/utils - traverseUserStorageTriggers()', () => {

packages/notification-services-controller/src/NotificationServicesPushController/constants.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/notification-services-controller/src/NotificationServicesPushController/utils/get-notification-message.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -238,27 +238,6 @@ function getChainSymbol(chainId: number) {
238238
return Constants.CHAIN_SYMBOLS[chainId] ?? null;
239239
}
240240

241-
/**
242-
* Checks if the given value is an OnChainRawNotification object.
243-
*
244-
* @param n - The value to check.
245-
* @returns True if the value is an OnChainRawNotification object, false otherwise.
246-
*/
247-
export function isOnChainNotification(
248-
n: unknown,
249-
): n is Types.OnChainRawNotification {
250-
const assumed = n as Types.OnChainRawNotification;
251-
252-
// We don't have a validation/parsing library to check all possible types of an on chain notification
253-
// It is safe enough just to check "some" fields, and catch any errors down the line if the shape is bad.
254-
const isValidEnoughToBeOnChainNotification = [
255-
assumed?.id,
256-
assumed?.data,
257-
assumed?.trigger_id,
258-
].every((field) => field !== undefined);
259-
return isValidEnoughToBeOnChainNotification;
260-
}
261-
262241
/**
263242
* Creates a push notification message based on the given on-chain raw notification.
264243
*
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './is-onchain-notification';
2+
export * from './to-raw-notification';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { isOnChainRawNotification } from '.';
2+
import {
3+
createMockFeatureAnnouncementRaw,
4+
createMockNotificationEthSent,
5+
} from '../NotificationServicesController/__fixtures__';
6+
7+
describe('is-onchain-notification - isOnChainRawNotification()', () => {
8+
it('returns true if OnChainRawNotification', () => {
9+
const notification = createMockNotificationEthSent();
10+
const result = isOnChainRawNotification(notification);
11+
expect(result).toBe(true);
12+
});
13+
it('returns false if not OnChainRawNotification', () => {
14+
const notification = createMockFeatureAnnouncementRaw();
15+
const result = isOnChainRawNotification(notification);
16+
expect(result).toBe(false);
17+
});
18+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { OnChainRawNotification } from '../NotificationServicesController';
2+
3+
/**
4+
* Checks if the given value is an OnChainRawNotification object.
5+
*
6+
* @param n - The value to check.
7+
* @returns True if the value is an OnChainRawNotification object, false otherwise.
8+
*/
9+
export function isOnChainRawNotification(
10+
n: unknown,
11+
): n is OnChainRawNotification {
12+
const assumed = n as OnChainRawNotification;
13+
14+
// We don't have a validation/parsing library to check all possible types of an on chain notification
15+
// It is safe enough just to check "some" fields, and catch any errors down the line if the shape is bad.
16+
const isValidEnoughToBeOnChainNotification = [
17+
assumed?.id,
18+
assumed?.data,
19+
assumed?.trigger_id,
20+
].every((field) => field !== undefined);
21+
return isValidEnoughToBeOnChainNotification;
22+
}

0 commit comments

Comments
 (0)