diff --git a/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts b/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts index f951213f8f4..e3650e4f99a 100644 --- a/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts +++ b/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.test.ts @@ -69,26 +69,6 @@ const mockErrorLog = () => const mockWarnLog = () => jest.spyOn(log, 'warn').mockImplementation(jest.fn()); describe('metamask-notifications - constructor()', () => { - const arrangeMocks = () => { - const messengerMocks = mockNotificationMessenger(); - jest - .spyOn(ControllerUtils, 'toChecksumHexAddress') - .mockImplementation((x) => x); - - return messengerMocks; - }; - - const actPublishKeyringStateChange = async ( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - messenger: any, - ) => { - messenger.publish( - 'KeyringController:stateChange', - {} as KeyringControllerState, - [], - ); - }; - it('initializes state & override state', () => { const controller1 = new NotificationServicesController({ messenger: mockNotificationMessenger().messenger, @@ -108,6 +88,28 @@ describe('metamask-notifications - constructor()', () => { expect(controller2.state.isFeatureAnnouncementsEnabled).toBe(true); expect(controller2.state.isNotificationServicesEnabled).toBe(true); }); +}); + +describe('metamask-notifications - init()', () => { + const arrangeMocks = () => { + const messengerMocks = mockNotificationMessenger(); + jest + .spyOn(ControllerUtils, 'toChecksumHexAddress') + .mockImplementation((x) => x); + + return messengerMocks; + }; + + const actPublishKeyringStateChange = async ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + messenger: any, + ) => { + messenger.publish( + 'KeyringController:stateChange', + {} as KeyringControllerState, + [], + ); + }; it('keyring Change Event but feature not enabled will not add or remove triggers', async () => { const { messenger, globalMessenger, mockWithKeyring } = arrangeMocks(); @@ -118,6 +120,7 @@ describe('metamask-notifications - constructor()', () => { messenger, env: { featureAnnouncements: featureAnnouncementsEnv }, }); + controller.init(); const mockUpdate = jest .spyOn(controller, 'updateOnChainTriggersByAccount') @@ -146,6 +149,7 @@ describe('metamask-notifications - constructor()', () => { subscriptionAccountsSeen: [ADDRESS_1], }, }); + controller.init(); const mockUpdate = jest .spyOn(controller, 'updateOnChainTriggersByAccount') @@ -199,12 +203,14 @@ describe('metamask-notifications - constructor()', () => { modifications?.(mocks); // Act - new NotificationServicesController({ + const controller = new NotificationServicesController({ messenger: mocks.messenger, env: { featureAnnouncements: featureAnnouncementsEnv }, state: { isNotificationServicesEnabled: true }, }); + controller.init(); + return mocks; }; @@ -287,7 +293,7 @@ describe('metamask-notifications - constructor()', () => { modifications?.(mocks); // Act - new NotificationServicesController({ + const controller = new NotificationServicesController({ messenger: mocks.messenger, env: { featureAnnouncements: featureAnnouncementsEnv, @@ -296,6 +302,8 @@ describe('metamask-notifications - constructor()', () => { state: { isNotificationServicesEnabled: true }, }); + controller.init(); + return mocks; }; diff --git a/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts b/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts index 324078c8272..a3b12725856 100644 --- a/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts +++ b/packages/notification-services-controller/src/NotificationServicesController/NotificationServicesController.ts @@ -545,7 +545,9 @@ export default class NotificationServicesController extends BaseController< this.#featureAnnouncementEnv = env.featureAnnouncements; this.#registerMessageHandlers(); this.#clearLoadingStates(); + } + init() { this.#keyringController.setupLockedStateSubscriptions(async () => { await this.#accounts.initialize(); await this.#pushNotifications.initializePushNotifications();