I have the following function:
(function () {
'use strict';
angular.module("app")
.service('EventHandler', EventHandler);
EventHandler.$inject = ['$rootScope', '$location', '$animate', '$state', '$stateParams', '$sanitize', '$q', '$uibModal', 'toastr',
'storage', 'StorageService', 'LogService', 'LanguageResource', 'LoginService', 'StcpUtilsService', 'DAOService',
'CSRF', 'Authorization', 'ShortcutService', 'data_login', 'PermissionsService', 'BusinessLogicService', 'DebugMode'
];
function EventHandler($rootScope, $location, $animate, $state, $stateParams, $sanitize, $q, $uibModal, toastr, storage, StorageService,
LogService, LanguageResource, LoginService, StcpUtilsService, DAOService, CSRF,
Authorization, ShortcutService, data_login, PermissionsService, BusinessLogicService, Debug) {
var animate;
/* = parameters.$animate || $animate; */
var rootScope;
/* = parameters.$rootScope || $rootScope; */
var scope;
/* = parameters.$scope; */
var _toastr;
/* = parameters.toastr || toastr; */
var _storage;
/* = parameters.StorageService || StorageService; */
var location;
/* = parameters.$location || $location; */
var log;
/* = parameters.LogService ? parameters.LogService.log : LogService.log || LogService.log; */
var language;
/* = parameters.LanguageResource || LanguageResource; */
var shortcut;
var t;
var dao;
var DebugMode;
return {
initialize: initialize_component,
onCheckSession: onCheckSessionHandle,
onLogin: onLoginHandle,
onLogout: onLogoutHandle,
onStateChangeError: onStateChangeErrorHandle,
onHideMenuCheck: onHideMenuCheckHandle,
onSetInstance: onSetInstanceHandle,
onRemoveInstance: onRemoveInstanceHandle,
onGetInstanceName: onGetInstanceNameHandle,
onGetInstanceId: onGetInstanceIdHandle,
onGetOperatorLogged: onGetOperatorLoggedHandle,
onOperatorIsLogged: onOperatorIsLoggedHandle,
onShakeMenu: onShakeMenuHandle,
onCheckInstance: onCheckInstanceHandle,
onSave: onSaveHandle,
onDelete: onDeleteHandle,
onInterceptError: onInterceptErrorHandle,
onChangeOperatorPassword: onChangePasswordHandle,
onGetTransfer: onGetTransferHandle,
onOpenChangeModuleDialog: onOpenChangeModuleDialogHandle,
onChangeSubModule: onChangeSubModuleHandle,
onDisplaySelectionInstance: onDisplaySelectionInstanceHandle,
onGetInstances: onGetInstancesHandle,
onCheckModule: onCheckModuleHandle,
// onDataToPersist: onDataToPersistHandle
};
// function onDataToPersistHandle() { rootScope.$on('dataToPersist', onDataToPersist); }
function onRemoveInstanceHandle() {
rootScope.$on('removeInstance', onRemoveInstance);
}
function onCheckSessionHandle() {
rootScope.$on('checkSession', onCheckSession);
}
function onRemoveInstance(ev, args) {
onCheckSession(ev, args);
if (ev.defaultPrevented === false) {
_storage[storage].del("instance_id");
_storage[storage].del("instance_name");
_storage.local.del("instance_name");
_storage.local.del("instanceSelected");
_storage.session.del("instanceSelected");
rootScope.instanceSelected = undefined;
delete rootScope.displaySelectionInstance;
delete rootScope.instance_id;
delete rootScope.instance_name;
}
}
function onCheckSession(ev, args) {
var serv;
var opLogged;
var head_req;
var perm;
var userLog;
var token;
var current_module;
try {
serv = StorageService[storage];
} catch (e) {
console.error(e.message);
}
try {
opLogged = serv.get('operator_logged') || null;
} catch (e) {
opLogged = null;
}
try {
head_req = serv.get('header_request') || null;
} catch (e) {
head_req = null;
}
try {
perm = serv.get('permissions') || null;
} catch (e) {
perm = null;
}
try {
userLog = serv.get('userLogged') || null;
} catch (e) {
userLog = null;
}
try {
token = serv.get('token_info') || null;
} catch (e) {
token = null;
}
try {
current_module = serv.get('current_module');
} catch (e) {
current_module = null;
}
if (token === null || opLogged === null || head_req === null || perm === null || userLog === null) {
if (LoginService.doLogout()) {
// Previne que o listener passado por parâmetro seja executado.
ev.preventDefault();
// Previne que os demais listeners assinados ao evento passado sejam executados.
ev.stopPropagation();
// Retira do escopo raiz dados do operador logado.
$rootScope.operator_logged = undefined;
// Inibe a exibição do Menu principal.
$rootScope.hideMenu = true;
// Se não estiver no estado de login.
Eif (!$state.is('login') && !$state.is('master.modules')) {
// Exibe mensagem de sessão expirada.
_toastr.warning(t('Sessão expirada, inicie uma nova sessão!'), t("Sessão expirada!"));
$state.go("login", {location: "replace"});
} else if (!$state.is('login')) {
// Redireciona para o estado de login.
$state.go("login", {location: "replace"});
}
}
}
}
//...
And I'm applying the following test in order to validate ev.defaultPrevented = false so I can get inside if
it('testing if the onRemoveInstance function', function(){
var args;
var ev = {
defaultPrevented: false
};
function onRemoveInstance(ev) {
if (ev.defaultPrevented === false) {
srv.onRemoveInstance();
rootScope.$emit('removeInstance');
}
}
onRemoveInstance(ev);
Array.prototype.removeByAttr = srv.onCheckSession(ev, args);
});
But it is not entering into if. I would like to know what I am doing wrong and what is the correct way for the test to work out.