@@ -61,12 +61,13 @@ const featureAnnouncementsEnv = {
6161} ;
6262
6363// Testing util to clean up verbose logs when testing errors
64- const mockErrorLog = ( ) =>
64+ const mockErrorLog = ( ) : jest . SpyInstance =>
6565 jest . spyOn ( log , 'error' ) . mockImplementation ( jest . fn ( ) ) ;
66- const mockWarnLog = ( ) => jest . spyOn ( log , 'warn' ) . mockImplementation ( jest . fn ( ) ) ;
66+ const mockWarnLog = ( ) : jest . SpyInstance =>
67+ jest . spyOn ( log , 'warn' ) . mockImplementation ( jest . fn ( ) ) ;
6768
6869// Removing caches to avoid interference
69- const clearAPICache = ( ) => {
70+ const clearAPICache = ( ) : void => {
7071 notificationsConfigCache . clear ( ) ;
7172} ;
7273
@@ -94,11 +95,11 @@ describe('NotificationServicesController', () => {
9495 } ) ;
9596
9697 describe ( 'init' , ( ) => {
97- const arrangeMocks = ( ) => {
98+ const arrangeMocks = ( ) : ReturnType < typeof mockNotificationMessenger > => {
9899 const messengerMocks = mockNotificationMessenger ( ) ;
99100 jest
100101 . spyOn ( ControllerUtils , 'toChecksumHexAddress' )
101- . mockImplementation ( ( x ) => x ) ;
102+ . mockImplementation ( ( address ) => address ) ;
102103
103104 return messengerMocks ;
104105 } ;
@@ -107,7 +108,7 @@ describe('NotificationServicesController', () => {
107108 // eslint-disable-next-line @typescript-eslint/no-explicit-any
108109 messenger : any ,
109110 accounts : string [ ] = [ '0x111' , '0x222' ] ,
110- ) => {
111+ ) : Promise < void > => {
111112 messenger . publish (
112113 'KeyringController:stateChange' ,
113114 {
@@ -119,7 +120,11 @@ describe('NotificationServicesController', () => {
119120
120121 const arrangeActAssertKeyringTest = async (
121122 controllerState ?: Partial < NotificationServicesControllerState > ,
122- ) => {
123+ ) : Promise < {
124+ act : ( addresses : string [ ] , assertion : ( ) => void ) => Promise < void > ;
125+ mockEnable : jest . SpyInstance ;
126+ mockDisable : jest . SpyInstance ;
127+ } > => {
123128 const mocks = arrangeMocks ( ) ;
124129 const { messenger, globalMessenger, mockKeyringControllerGetState } =
125130 mocks ;
@@ -155,7 +160,10 @@ describe('NotificationServicesController', () => {
155160 . spyOn ( controller , 'disableAccounts' )
156161 . mockResolvedValue ( ) ;
157162
158- const act = async ( addresses : string [ ] , assertion : ( ) => void ) => {
163+ const act = async (
164+ addresses : string [ ] ,
165+ assertion : ( ) => void ,
166+ ) : Promise < void > => {
159167 mockKeyringControllerGetState . mockReturnValue ( {
160168 isUnlocked : true ,
161169 keyrings : [
@@ -246,7 +254,9 @@ describe('NotificationServicesController', () => {
246254
247255 const arrangeActInitialisePushNotifications = (
248256 modifications ?: ( mocks : ReturnType < typeof arrangeMocks > ) => void ,
249- ) => {
257+ ) : ReturnType < typeof arrangeMocks > & {
258+ mockAPIGetNotificationConfig : nock . Scope ;
259+ } => {
250260 // Arrange
251261 const mockAPIGetNotificationConfig = mockGetOnChainNotificationsConfig ( ) ;
252262 const mocks = arrangeMocks ( ) ;
@@ -378,7 +388,12 @@ describe('NotificationServicesController', () => {
378388 } ) ;
379389
380390 describe ( 'createOnChainTriggers' , ( ) => {
381- const arrangeMocks = ( overrides ?: { mockGetConfig : ( ) => nock . Scope } ) => {
391+ const arrangeMocks = ( overrides ?: {
392+ mockGetConfig : ( ) => nock . Scope ;
393+ } ) : ReturnType < typeof mockNotificationMessenger > & {
394+ mockGetConfig : nock . Scope ;
395+ mockUpdateNotifications : nock . Scope ;
396+ } => {
382397 const messengerMocks = mockNotificationMessenger ( ) ;
383398 const mockGetConfig =
384399 overrides ?. mockGetConfig ( ) ?? mockGetOnChainNotificationsConfig ( ) ;
@@ -532,7 +547,9 @@ describe('NotificationServicesController', () => {
532547 } ) ;
533548
534549 describe ( 'disableAccounts' , ( ) => {
535- const arrangeMocks = ( ) => {
550+ const arrangeMocks = ( ) : ReturnType < typeof mockNotificationMessenger > & {
551+ mockUpdateNotifications : nock . Scope ;
552+ } => {
536553 const messengerMocks = mockNotificationMessenger ( ) ;
537554 const mockUpdateNotifications = mockUpdateOnChainNotifications ( ) ;
538555 return { ...messengerMocks , mockUpdateNotifications } ;
@@ -572,7 +589,9 @@ describe('NotificationServicesController', () => {
572589 } ) ;
573590
574591 describe ( 'enableAccounts' , ( ) => {
575- const arrangeMocks = ( ) => {
592+ const arrangeMocks = ( ) : ReturnType < typeof mockNotificationMessenger > & {
593+ mockUpdateNotifications : nock . Scope ;
594+ } => {
576595 const messengerMocks = mockNotificationMessenger ( ) ;
577596 const mockUpdateNotifications = mockUpdateOnChainNotifications ( ) ;
578597 return { ...messengerMocks , mockUpdateNotifications } ;
@@ -612,7 +631,17 @@ describe('NotificationServicesController', () => {
612631 } ) ;
613632
614633 describe ( 'fetchAndUpdateMetamaskNotifications' , ( ) => {
615- const arrangeMocks = ( ) => {
634+ const arrangeMocks = ( ) : ReturnType < typeof mockNotificationMessenger > & {
635+ mockNotificationConfigAPI : nock . Scope ;
636+ mockFeatureAnnouncementAPIResult : ReturnType <
637+ typeof createMockFeatureAnnouncementAPIResult
638+ > ;
639+ mockFeatureAnnouncementsAPI : nock . Scope ;
640+ mockOnChainNotificationsAPIResult : ReturnType <
641+ typeof createMockNotificationEthSent
642+ > [ ] ;
643+ mockOnChainNotificationsAPI : nock . Scope ;
644+ } => {
616645 const messengerMocks = mockNotificationMessenger ( ) ;
617646
618647 const mockFeatureAnnouncementAPIResult =
@@ -646,7 +675,7 @@ describe('NotificationServicesController', () => {
646675 const arrangeController = (
647676 messenger : NotificationServicesControllerMessenger ,
648677 overrideState ?: Partial < NotificationServicesControllerState > ,
649- ) => {
678+ ) : NotificationServicesController => {
650679 const controller = new NotificationServicesController ( {
651680 messenger,
652681 env : { featureAnnouncements : featureAnnouncementsEnv } ,
@@ -677,18 +706,25 @@ describe('NotificationServicesController', () => {
677706
678707 // Should have 1 feature announcement
679708 expect (
680- result . filter ( ( n ) => n . type === TRIGGER_TYPES . FEATURES_ANNOUNCEMENT ) ,
709+ result . filter (
710+ ( notification ) =>
711+ notification . type === TRIGGER_TYPES . FEATURES_ANNOUNCEMENT ,
712+ ) ,
681713 ) . toHaveLength ( 1 ) ;
682714
683715 // Should have 1 Wallet Notification
684716 expect (
685- result . filter ( ( n ) => n . type === TRIGGER_TYPES . ETH_SENT ) ,
717+ result . filter (
718+ ( notification ) => notification . type === TRIGGER_TYPES . ETH_SENT ,
719+ ) ,
686720 ) . toHaveLength ( 1 ) ;
687721
688722 // Should have 1 Snap Notification
689- expect ( result . filter ( ( n ) => n . type === TRIGGER_TYPES . SNAP ) ) . toHaveLength (
690- 1 ,
691- ) ;
723+ expect (
724+ result . filter (
725+ ( notification ) => notification . type === TRIGGER_TYPES . SNAP ,
726+ ) ,
727+ ) . toHaveLength ( 1 ) ;
692728
693729 // Total notification length = 3
694730 expect ( result ) . toHaveLength ( 3 ) ;
@@ -708,9 +744,11 @@ describe('NotificationServicesController', () => {
708744 // Should only contain snap notification
709745 // As this is not controlled by the global notification switch
710746 expect ( result ) . toHaveLength ( 1 ) ;
711- expect ( result . filter ( ( n ) => n . type === TRIGGER_TYPES . SNAP ) ) . toHaveLength (
712- 1 ,
713- ) ;
747+ expect (
748+ result . filter (
749+ ( notification ) => notification . type === TRIGGER_TYPES . SNAP ,
750+ ) ,
751+ ) . toHaveLength ( 1 ) ;
714752
715753 // APIs should not have been called
716754 expect ( mocks . mockFeatureAnnouncementsAPI . isDone ( ) ) . toBe ( false ) ;
@@ -727,7 +765,10 @@ describe('NotificationServicesController', () => {
727765
728766 // Should not have any feature announcements
729767 expect (
730- result . filter ( ( n ) => n . type === TRIGGER_TYPES . FEATURES_ANNOUNCEMENT ) ,
768+ result . filter (
769+ ( notification ) =>
770+ notification . type === TRIGGER_TYPES . FEATURES_ANNOUNCEMENT ,
771+ ) ,
731772 ) . toHaveLength ( 0 ) ;
732773
733774 // Should not have called feature announcement API
@@ -895,7 +936,11 @@ describe('NotificationServicesController', () => {
895936 } ) ;
896937
897938 describe ( 'markMetamaskNotificationsAsRead' , ( ) => {
898- const arrangeMocks = ( options ?: { onChainMarkAsReadFails : boolean } ) => {
939+ const arrangeMocks = ( options ?: {
940+ onChainMarkAsReadFails : boolean ;
941+ } ) : ReturnType < typeof mockNotificationMessenger > & {
942+ mockMarkAsReadAPI : nock . Scope ;
943+ } => {
899944 const messengerMocks = mockNotificationMessenger ( ) ;
900945
901946 const mockMarkAsReadAPI = mockMarkNotificationsAsRead ( {
@@ -977,7 +1022,12 @@ describe('NotificationServicesController', () => {
9771022 } ) ;
9781023
9791024 describe ( 'enableMetamaskNotifications' , ( ) => {
980- const arrangeMocks = ( overrides ?: { mockGetConfig : ( ) => nock . Scope } ) => {
1025+ const arrangeMocks = ( overrides ?: {
1026+ mockGetConfig : ( ) => nock . Scope ;
1027+ } ) : ReturnType < typeof mockNotificationMessenger > & {
1028+ mockGetConfig : nock . Scope ;
1029+ mockUpdateNotifications : nock . Scope ;
1030+ } => {
9811031 const messengerMocks = mockNotificationMessenger ( ) ;
9821032 const mockGetConfig =
9831033 overrides ?. mockGetConfig ( ) ?? mockGetOnChainNotificationsConfig ( ) ;
@@ -1143,7 +1193,9 @@ describe('NotificationServicesController', () => {
11431193 } ) ;
11441194
11451195 describe ( 'enablePushNotifications' , ( ) => {
1146- const arrangeMocks = ( ) => {
1196+ const arrangeMocks = ( ) : ReturnType < typeof mockNotificationMessenger > & {
1197+ mockGetConfig : nock . Scope ;
1198+ } => {
11471199 const messengerMocks = mockNotificationMessenger ( ) ;
11481200 const mockGetConfig = mockGetOnChainNotificationsConfig ( {
11491201 status : 200 ,
@@ -1214,7 +1266,9 @@ describe('NotificationServicesController', () => {
12141266 } ) ;
12151267
12161268 describe ( 'sendPerpPlaceOrderNotification' , ( ) => {
1217- const arrangeMocks = ( ) => {
1269+ const arrangeMocks = ( ) : ReturnType < typeof mockNotificationMessenger > & {
1270+ mockCreatePerpAPI : nock . Scope ;
1271+ } => {
12181272 const messengerMocks = mockNotificationMessenger ( ) ;
12191273 const mockCreatePerpAPI = mockCreatePerpNotification ( {
12201274 status : 200 ,
@@ -1396,8 +1450,10 @@ describe('NotificationServicesController', () => {
13961450// Thus this `AnyFunc` can be used to help constrain the generic parameters correctly
13971451// eslint-disable-next-line @typescript-eslint/no-explicit-any
13981452type AnyFunc = ( ...args : any [ ] ) => any ;
1399- const typedMockAction = < Action extends { handler : AnyFunc } > ( ) =>
1400- jest . fn < ReturnType < Action [ 'handler' ] > , Parameters < Action [ 'handler' ] > > ( ) ;
1453+ const typedMockAction = < Action extends { handler : AnyFunc } > ( ) : jest . Mock <
1454+ ReturnType < Action [ 'handler' ] > ,
1455+ Parameters < Action [ 'handler' ] >
1456+ > => jest . fn < ReturnType < Action [ 'handler' ] > , Parameters < Action [ 'handler' ] > > ( ) ;
14011457
14021458const controllerName = 'NotificationServicesController' ;
14031459
@@ -1429,7 +1485,17 @@ function getRootMessenger(): RootMessenger {
14291485 *
14301486 * @returns mock notification messenger and other messenger mocks
14311487 */
1432- function mockNotificationMessenger ( ) {
1488+ function mockNotificationMessenger ( ) : {
1489+ globalMessenger : RootMessenger ;
1490+ messenger : NotificationServicesControllerMessenger ;
1491+ mockGetBearerToken : jest . Mock ;
1492+ mockIsSignedIn : jest . Mock ;
1493+ mockAuthPerformSignIn : jest . Mock ;
1494+ mockDisablePushNotifications : jest . Mock ;
1495+ mockEnablePushNotifications : jest . Mock ;
1496+ mockSubscribeToPushNotifications : jest . Mock ;
1497+ mockKeyringControllerGetState : jest . Mock ;
1498+ } {
14331499 const globalMessenger = getRootMessenger ( ) ;
14341500
14351501 const messenger = new Messenger <
@@ -1568,15 +1634,19 @@ function mockNotificationMessenger() {
15681634 */
15691635function arrangeFailureAuthAssertions (
15701636 mocks : ReturnType < typeof mockNotificationMessenger > ,
1571- ) {
1637+ ) : {
1638+ notLoggedIn : ( ) => jest . Mock ;
1639+ noBearerToken : ( ) => jest . Mock ;
1640+ rejectedBearerToken : ( ) => jest . Mock ;
1641+ } {
15721642 const testScenarios = {
1573- NotLoggedIn : ( ) => mocks . mockIsSignedIn . mockReturnValue ( false ) ,
1643+ notLoggedIn : ( ) : jest . Mock => mocks . mockIsSignedIn . mockReturnValue ( false ) ,
15741644
15751645 // unlikely, but in case it returns null
1576- NoBearerToken : ( ) =>
1646+ noBearerToken : ( ) : jest . Mock =>
15771647 mocks . mockGetBearerToken . mockResolvedValueOnce ( null as unknown as string ) ,
15781648
1579- RejectedBearerToken : ( ) =>
1649+ rejectedBearerToken : ( ) : jest . Mock =>
15801650 mocks . mockGetBearerToken . mockRejectedValueOnce (
15811651 new Error ( 'MOCK - no bearer token' ) ,
15821652 ) ,
0 commit comments