MM-24852 ensure only one listener for in-app notification is registered (#4269)

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
This commit is contained in:
Mattermost Build 2020-05-06 01:15:52 +02:00 committed by GitHub
parent edfa0257c3
commit 6db738fecb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View file

@ -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);
}

View file

@ -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();