Skip to content
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
27 changes: 0 additions & 27 deletions eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1560,33 +1560,6 @@
"count": 3
}
},
"packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 23
},
"id-length": {
"count": 7
}
},
"packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 31
},
"@typescript-eslint/no-misused-promises": {
"count": 1
},
"id-denylist": {
"count": 5
},
"id-length": {
"count": 8
}
},
"packages/notification-services-controller/src/NotificationServicesController/__fixtures__/mockServices.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 6
}
},
"packages/notification-services-controller/src/NotificationServicesController/constants/notification-schema.ts": {
"@typescript-eslint/naming-convention": {
"count": 18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ const featureAnnouncementsEnv = {
};

// Testing util to clean up verbose logs when testing errors
const mockErrorLog = () =>
const mockErrorLog = (): jest.SpyInstance =>
jest.spyOn(log, 'error').mockImplementation(jest.fn());
const mockWarnLog = () => jest.spyOn(log, 'warn').mockImplementation(jest.fn());
const mockWarnLog = (): jest.SpyInstance =>
jest.spyOn(log, 'warn').mockImplementation(jest.fn());

// Removing caches to avoid interference
const clearAPICache = () => {
const clearAPICache = (): void => {
notificationsConfigCache.clear();
};

Expand Down Expand Up @@ -94,11 +95,11 @@ describe('NotificationServicesController', () => {
});

describe('init', () => {
const arrangeMocks = () => {
const arrangeMocks = (): ReturnType<typeof mockNotificationMessenger> => {
const messengerMocks = mockNotificationMessenger();
jest
.spyOn(ControllerUtils, 'toChecksumHexAddress')
.mockImplementation((x) => x);
.mockImplementation((address) => address);

return messengerMocks;
};
Expand All @@ -107,7 +108,7 @@ describe('NotificationServicesController', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
messenger: any,
accounts: string[] = ['0x111', '0x222'],
) => {
): Promise<void> => {
messenger.publish(
'KeyringController:stateChange',
{
Expand All @@ -119,7 +120,11 @@ describe('NotificationServicesController', () => {

const arrangeActAssertKeyringTest = async (
controllerState?: Partial<NotificationServicesControllerState>,
) => {
): Promise<{
act: (addresses: string[], assertion: () => void) => Promise<void>;
mockEnable: jest.SpyInstance;
mockDisable: jest.SpyInstance;
}> => {
const mocks = arrangeMocks();
const { messenger, globalMessenger, mockKeyringControllerGetState } =
mocks;
Expand Down Expand Up @@ -155,7 +160,10 @@ describe('NotificationServicesController', () => {
.spyOn(controller, 'disableAccounts')
.mockResolvedValue();

const act = async (addresses: string[], assertion: () => void) => {
const act = async (
addresses: string[],
assertion: () => void,
): Promise<void> => {
mockKeyringControllerGetState.mockReturnValue({
isUnlocked: true,
keyrings: [
Expand Down Expand Up @@ -246,7 +254,9 @@ describe('NotificationServicesController', () => {

const arrangeActInitialisePushNotifications = (
modifications?: (mocks: ReturnType<typeof arrangeMocks>) => void,
) => {
): ReturnType<typeof arrangeMocks> & {
mockAPIGetNotificationConfig: nock.Scope;
} => {
// Arrange
const mockAPIGetNotificationConfig = mockGetOnChainNotificationsConfig();
const mocks = arrangeMocks();
Expand Down Expand Up @@ -378,7 +388,12 @@ describe('NotificationServicesController', () => {
});

describe('createOnChainTriggers', () => {
const arrangeMocks = (overrides?: { mockGetConfig: () => nock.Scope }) => {
const arrangeMocks = (overrides?: {
mockGetConfig: () => nock.Scope;
}): ReturnType<typeof mockNotificationMessenger> & {
mockGetConfig: nock.Scope;
mockUpdateNotifications: nock.Scope;
} => {
const messengerMocks = mockNotificationMessenger();
const mockGetConfig =
overrides?.mockGetConfig() ?? mockGetOnChainNotificationsConfig();
Expand Down Expand Up @@ -532,7 +547,9 @@ describe('NotificationServicesController', () => {
});

describe('disableAccounts', () => {
const arrangeMocks = () => {
const arrangeMocks = (): ReturnType<typeof mockNotificationMessenger> & {
mockUpdateNotifications: nock.Scope;
} => {
const messengerMocks = mockNotificationMessenger();
const mockUpdateNotifications = mockUpdateOnChainNotifications();
return { ...messengerMocks, mockUpdateNotifications };
Expand Down Expand Up @@ -572,7 +589,9 @@ describe('NotificationServicesController', () => {
});

describe('enableAccounts', () => {
const arrangeMocks = () => {
const arrangeMocks = (): ReturnType<typeof mockNotificationMessenger> & {
mockUpdateNotifications: nock.Scope;
} => {
const messengerMocks = mockNotificationMessenger();
const mockUpdateNotifications = mockUpdateOnChainNotifications();
return { ...messengerMocks, mockUpdateNotifications };
Expand Down Expand Up @@ -612,7 +631,17 @@ describe('NotificationServicesController', () => {
});

describe('fetchAndUpdateMetamaskNotifications', () => {
const arrangeMocks = () => {
const arrangeMocks = (): ReturnType<typeof mockNotificationMessenger> & {
mockNotificationConfigAPI: nock.Scope;
mockFeatureAnnouncementAPIResult: ReturnType<
typeof createMockFeatureAnnouncementAPIResult
>;
mockFeatureAnnouncementsAPI: nock.Scope;
mockOnChainNotificationsAPIResult: ReturnType<
typeof createMockNotificationEthSent
>[];
mockOnChainNotificationsAPI: nock.Scope;
} => {
const messengerMocks = mockNotificationMessenger();

const mockFeatureAnnouncementAPIResult =
Expand Down Expand Up @@ -646,7 +675,7 @@ describe('NotificationServicesController', () => {
const arrangeController = (
messenger: NotificationServicesControllerMessenger,
overrideState?: Partial<NotificationServicesControllerState>,
) => {
): NotificationServicesController => {
const controller = new NotificationServicesController({
messenger,
env: { featureAnnouncements: featureAnnouncementsEnv },
Expand Down Expand Up @@ -677,18 +706,25 @@ describe('NotificationServicesController', () => {

// Should have 1 feature announcement
expect(
result.filter((n) => n.type === TRIGGER_TYPES.FEATURES_ANNOUNCEMENT),
result.filter(
(notification) =>
notification.type === TRIGGER_TYPES.FEATURES_ANNOUNCEMENT,
),
).toHaveLength(1);

// Should have 1 Wallet Notification
expect(
result.filter((n) => n.type === TRIGGER_TYPES.ETH_SENT),
result.filter(
(notification) => notification.type === TRIGGER_TYPES.ETH_SENT,
),
).toHaveLength(1);

// Should have 1 Snap Notification
expect(result.filter((n) => n.type === TRIGGER_TYPES.SNAP)).toHaveLength(
1,
);
expect(
result.filter(
(notification) => notification.type === TRIGGER_TYPES.SNAP,
),
).toHaveLength(1);

// Total notification length = 3
expect(result).toHaveLength(3);
Expand All @@ -708,9 +744,11 @@ describe('NotificationServicesController', () => {
// Should only contain snap notification
// As this is not controlled by the global notification switch
expect(result).toHaveLength(1);
expect(result.filter((n) => n.type === TRIGGER_TYPES.SNAP)).toHaveLength(
1,
);
expect(
result.filter(
(notification) => notification.type === TRIGGER_TYPES.SNAP,
),
).toHaveLength(1);

// APIs should not have been called
expect(mocks.mockFeatureAnnouncementsAPI.isDone()).toBe(false);
Expand All @@ -727,7 +765,10 @@ describe('NotificationServicesController', () => {

// Should not have any feature announcements
expect(
result.filter((n) => n.type === TRIGGER_TYPES.FEATURES_ANNOUNCEMENT),
result.filter(
(notification) =>
notification.type === TRIGGER_TYPES.FEATURES_ANNOUNCEMENT,
),
).toHaveLength(0);

// Should not have called feature announcement API
Expand Down Expand Up @@ -895,7 +936,11 @@ describe('NotificationServicesController', () => {
});

describe('markMetamaskNotificationsAsRead', () => {
const arrangeMocks = (options?: { onChainMarkAsReadFails: boolean }) => {
const arrangeMocks = (options?: {
onChainMarkAsReadFails: boolean;
}): ReturnType<typeof mockNotificationMessenger> & {
mockMarkAsReadAPI: nock.Scope;
} => {
const messengerMocks = mockNotificationMessenger();

const mockMarkAsReadAPI = mockMarkNotificationsAsRead({
Expand Down Expand Up @@ -977,7 +1022,12 @@ describe('NotificationServicesController', () => {
});

describe('enableMetamaskNotifications', () => {
const arrangeMocks = (overrides?: { mockGetConfig: () => nock.Scope }) => {
const arrangeMocks = (overrides?: {
mockGetConfig: () => nock.Scope;
}): ReturnType<typeof mockNotificationMessenger> & {
mockGetConfig: nock.Scope;
mockUpdateNotifications: nock.Scope;
} => {
const messengerMocks = mockNotificationMessenger();
const mockGetConfig =
overrides?.mockGetConfig() ?? mockGetOnChainNotificationsConfig();
Expand Down Expand Up @@ -1143,7 +1193,9 @@ describe('NotificationServicesController', () => {
});

describe('enablePushNotifications', () => {
const arrangeMocks = () => {
const arrangeMocks = (): ReturnType<typeof mockNotificationMessenger> & {
mockGetConfig: nock.Scope;
} => {
const messengerMocks = mockNotificationMessenger();
const mockGetConfig = mockGetOnChainNotificationsConfig({
status: 200,
Expand Down Expand Up @@ -1214,7 +1266,9 @@ describe('NotificationServicesController', () => {
});

describe('sendPerpPlaceOrderNotification', () => {
const arrangeMocks = () => {
const arrangeMocks = (): ReturnType<typeof mockNotificationMessenger> & {
mockCreatePerpAPI: nock.Scope;
} => {
const messengerMocks = mockNotificationMessenger();
const mockCreatePerpAPI = mockCreatePerpNotification({
status: 200,
Expand Down Expand Up @@ -1396,8 +1450,10 @@ describe('NotificationServicesController', () => {
// Thus this `AnyFunc` can be used to help constrain the generic parameters correctly
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyFunc = (...args: any[]) => any;
const typedMockAction = <Action extends { handler: AnyFunc }>() =>
jest.fn<ReturnType<Action['handler']>, Parameters<Action['handler']>>();
const typedMockAction = <Action extends { handler: AnyFunc }>(): jest.Mock<
ReturnType<Action['handler']>,
Parameters<Action['handler']>
> => jest.fn<ReturnType<Action['handler']>, Parameters<Action['handler']>>();

const controllerName = 'NotificationServicesController';

Expand Down Expand Up @@ -1429,7 +1485,17 @@ function getRootMessenger(): RootMessenger {
*
* @returns mock notification messenger and other messenger mocks
*/
function mockNotificationMessenger() {
function mockNotificationMessenger(): {
globalMessenger: RootMessenger;
messenger: NotificationServicesControllerMessenger;
mockGetBearerToken: jest.Mock;
mockIsSignedIn: jest.Mock;
mockAuthPerformSignIn: jest.Mock;
mockDisablePushNotifications: jest.Mock;
mockEnablePushNotifications: jest.Mock;
mockSubscribeToPushNotifications: jest.Mock;
mockKeyringControllerGetState: jest.Mock;
} {
const globalMessenger = getRootMessenger();

const messenger = new Messenger<
Expand Down Expand Up @@ -1568,15 +1634,19 @@ function mockNotificationMessenger() {
*/
function arrangeFailureAuthAssertions(
mocks: ReturnType<typeof mockNotificationMessenger>,
) {
): {
notLoggedIn: () => jest.Mock;
noBearerToken: () => jest.Mock;
rejectedBearerToken: () => jest.Mock;
} {
const testScenarios = {
NotLoggedIn: () => mocks.mockIsSignedIn.mockReturnValue(false),
notLoggedIn: (): jest.Mock => mocks.mockIsSignedIn.mockReturnValue(false),

// unlikely, but in case it returns null
NoBearerToken: () =>
noBearerToken: (): jest.Mock =>
mocks.mockGetBearerToken.mockResolvedValueOnce(null as unknown as string),

RejectedBearerToken: () =>
rejectedBearerToken: (): jest.Mock =>
mocks.mockGetBearerToken.mockRejectedValueOnce(
new Error('MOCK - no bearer token'),
),
Expand Down
Loading
Loading