MM-24852 ensure only one listener for in-app notification is registered (#4267)
This commit is contained in:
parent
86737a70c6
commit
597e03a6bc
2 changed files with 38 additions and 3 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue