From 597e03a6bc2984ff048bfafc368771f22cf01bde Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Tue, 5 May 2020 18:56:58 -0400 Subject: [PATCH] MM-24852 ensure only one listener for in-app notification is registered (#4267) --- app/init/global_event_handler.js | 8 ++++++- app/init/global_event_handler.test.js | 33 +++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/init/global_event_handler.js b/app/init/global_event_handler.js index 2ca97e36b..f02ec4109 100644 --- a/app/init/global_event_handler.js +++ b/app/init/global_event_handler.js @@ -45,6 +45,8 @@ const PROMPT_IN_APP_PIN_CODE_AFTER = 5 * 1000; class GlobalEventHandler { constructor() { + this.pushNotificationListener = false; + EventEmitter.on(NavigationTypes.NAVIGATION_RESET, this.onLogout); EventEmitter.on(NavigationTypes.RESTART_APP, this.onRestartApp); EventEmitter.on(General.SERVER_VERSION_CHANGED, this.onServerVersionChanged); @@ -326,10 +328,14 @@ class GlobalEventHandler { }; turnOnInAppNotificationHandling = () => { - EventEmitter.on(ViewTypes.NOTIFICATION_IN_APP, this.handleInAppNotification); + if (!this.pushNotificationListener) { + this.pushNotificationListener = true; + EventEmitter.on(ViewTypes.NOTIFICATION_IN_APP, this.handleInAppNotification); + } } turnOffInAppNotificationHandling = () => { + this.pushNotificationListener = false; EventEmitter.off(ViewTypes.NOTIFICATION_IN_APP, this.handleInAppNotification); } diff --git a/app/init/global_event_handler.test.js b/app/init/global_event_handler.test.js index 49047b957..898661778 100644 --- a/app/init/global_event_handler.test.js +++ b/app/init/global_event_handler.test.js @@ -9,6 +9,7 @@ import semver from 'semver/preload'; import PushNotification from 'app/push_notifications'; import mattermostBucket from 'app/mattermost_bucket'; import * as I18n from '@i18n'; +import EventEmitter from '@mm-redux/utils/event_emitter'; import Store from '@store/store'; import intitialState from '@store/initial_state'; @@ -22,7 +23,7 @@ jest.mock('app/init/credentials', () => ({ removeAppCredentials: jest.fn(), })); -jest.mock('app/utils/error_handling', () => ({ +jest.mock('@utils/error_handling', () => ({ default: { initializeErrorHandling: jest.fn(), }, @@ -45,7 +46,7 @@ jest.mock('@mm-redux/actions/general', () => ({ setServerVersion: jest.fn().mockReturnValue('setServerVersion'), })); -jest.mock('app/actions/views/root', () => ({ +jest.mock('@actions/views/root', () => ({ startDataCleanup: jest.fn(), loadConfigAndLicense: jest.fn().mockReturnValue('loadConfigAndLicense'), })); @@ -123,6 +124,34 @@ describe('GlobalEventHandler', () => { expect(setUserTimezone).toHaveBeenCalledTimes(1); }); + it('should register NOTIFICATION_IN_APP once', () => { + const on = jest.spyOn(EventEmitter, 'on'); + + // Reset the listener + GlobalEventHandler.pushNotificationListener = false; + + GlobalEventHandler.turnOnInAppNotificationHandling(); + + // call it a second time + GlobalEventHandler.turnOnInAppNotificationHandling(); + + expect(on).toBeCalledTimes(1); + expect(on).toBeCalledWith('NOTIFICATION_IN_APP', GlobalEventHandler.handleInAppNotification); + }); + + it('should register NOTIFICATION_IN_APP once after unregister', () => { + const on = jest.spyOn(EventEmitter, 'on'); + GlobalEventHandler.turnOnInAppNotificationHandling(); + GlobalEventHandler.turnOffInAppNotificationHandling(); + + // call it a second time + GlobalEventHandler.turnOnInAppNotificationHandling(); + GlobalEventHandler.turnOnInAppNotificationHandling(); + + expect(on).toBeCalledTimes(1); + expect(on).toBeCalledWith('NOTIFICATION_IN_APP', GlobalEventHandler.handleInAppNotification); + }); + describe('onServerVersionChanged', () => { beforeEach(() => { jest.clearAllMocks();