How to solve a problem in Angularjs

Asked

Viewed 80 times

1

I have the following function onLogin and within it I have a Promise being called on Loginservice how do I pass a unit test in this trial?

 function onLogin(e, args) {
        DebugMode && console.info('[INFO]', 'Login requested on ' + new Date());

        var cred;
        var data;

        try {
            cred = args.credentials;
            data = data_login;
        } catch (ex) {
            log('warn', 'Cannot get credentials to access.');
            _toastr.warning(ex.message, ' ', 'Contate o suporte e informe a mensagem exibida.');
            $state.go('login');
        }

        // Sanitize login credentials.
        cred.username = $sanitize(cred.username);
        cred.password = $sanitize(cred.password);

        data.username = cred.username;
        data.password = cred.password;

        LoginService.doLogin(cred/*data*/).then(
            /* Success */
            function (result) {
                scope.errorLogin = false;
                rootScope.hideMenu = false;
                rootScope.loading = false;
                rootScope.$emit('operatorIsLogged', null);
                rootScope.$broadcast('operatorIsLogged', null);
                rootScope.$broadcast('getOperatorLogged', null);
                rootScope.module_permissions = PermissionsService.check_modules_permissions();
                _toastr.success(t("Login efetuado com sucesso!"), 'Login');
                dao.getList("instance", null).then(function (res) {
                    rootScope.instances = res.data.data;
                    _storage.session.set('instances', rootScope.instances);
                }, function (err) {
                    _toastr.error("Não foi possível recuperar a lista de Instâncias, saindo do sistema.", "Solicitando o Logout!");
                    rootScope.instances = null;
                    rootScope.$emit('doLogout');
                });
                $state.go('master.modules');//$state.go('stcpcfg');
                /* $rootScope.current_module = JSON.parse(StorageService.session.get("current_module")); */
            }, /* Error */
            function (reason) {
                scope.errorLogin = true;
                rootScope.hideMenu = true;
                rootScope.loading = false;
            }
        );
    }

I was doing it this way but it’s not right because it’s not covering the function:

describe('Testing EventHandler', function () {
beforeEach(module('app'));

var srv, rootScope, injector, httpBackend, animate, scope, location, loginService, _toastr, _storage, dao, log, language, permissionsService;

beforeEach(inject(function (EventHandler,_$injector_, $rootScope, $httpBackend, $animate, $location, LoginService, toastr, storage, StorageService, LogService, DAOService, LanguageResource, $q, PermissionsService) {
  injector = _$injector_;
  srv = EventHandler;
  rootScope = $rootScope;          
  animate = $animate;
  location = $location;
  loginService = LoginService;
  _toastr = toastr;
  _storage = StorageService;
  log = LogService;
  language = LanguageResource;
  dao = DAOService;
  permissionsService = PermissionsService;  

  srv.initialize({
      $rootScope: rootScope,
      $animate: animate,
      $scope: $rootScope.$new(),
      $location: location,
      _toastr: toastr,
      _storage: StorageService,
      log: LogService,
      language: LanguageResource,
      dao: DAOService,
  })  

var defDoLoginSuccess = $q.defer();
var responseDoLoginSuccess = {
   scope: { errorLogin: false }
};

defDoLoginSuccess.resolve(responseDoLoginSuccess);
spyOn(loginService, 'doLogin').and.returnValue(defDoLoginSuccess.promise); 
}));    

it('LoginService.doLogin', function(){
    srv.onLogin(scope: { errorLogin: false });
    expect(loginService).toBeDefined();

});
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.