/*** Created by npatil2 .*/ describe('service: userModel', function () { var $httpBackend, $rootScope, scope; beforeEach(module('myitsmApp')); beforeEach(inject(function ($injector, authService, localStorageService, userService, $q, $log, $rootScope, events, searchService) { scope = $rootScope.$new(); $httpBackend = $injector.get('$httpBackend'); var getLocale = function () { return readJSON('scripts/app/i18n/resources-locale_en.json'); }; $httpBackend.whenGET(/^scripts\/app\/i18n\/resources-locale_en.*$/).respond(getLocale()); $httpBackend.whenGET('/smartit/rest/serverstates').respond(200); $httpBackend.whenGET('/smartit/rest/sessionstatus?getLicenseKey=true').respond(200); $httpBackend.whenGET('views/dashboard/index.html').respond(200); $rootScope = $injector.get('$rootScope'); this.localStorageService = localStorageService; this.localStorageService.set('user.userId', '!test#test'); this.userModel = $injector.get('userModel'); this.authService = authService; this.userService = userService; this.$q = $q; this.$log = $log; this.events = events; this.searchService = searchService; })); beforeEach(inject(function ($q) { var deferred = $q.defer(), data = { id: null, name: 'test' }, companies = [{ id: 'AGGAA5V0HGVEGAOXNUF8OWRK5T4PP1', name: '#Calsoft' }, { id: 'AG00123F73CF5EDFYTSQRbxaAAFsUA', name: 'Calbro Services' } ], userProps = ['test', 'test1', 'test2', 'test3', 'test4'], id = 123; spyOn(this.userService, 'getFullUserData').and.callFake(function () { deferred.resolve(data); return deferred.promise; }); spyOn(this.searchService, 'getCompaniesByText').and.callFake(function () { deferred.resolve(companies); return deferred.promise; }); spyOn(this.userService, 'getUserPreferences').and.callFake(function () { deferred.resolve(userProps); return deferred.promise; }); spyOn(this.userService, 'getBCMIntegrationConfig').and.callFake(function () { deferred.resolve(data); return deferred.promise; }); spyOn(this.userService, 'doBCMLogin').and.callFake(function () { deferred.resolve(data); return deferred.promise; }); spyOn(this.userService, 'updateUserPreferences').and.callFake(function () { deferred.resolve(userProps); return deferred.promise; }); spyOn(this.userService, 'removeUserPreferences').and.callFake(function () { deferred.resolve(id); return deferred.promise; }); })); it('should return user data for current session', function () { this.userModel.getUserDataForCurrentSession(); scope.$apply(); expect(this.userModel.userFullData.name).toEqual('test'); }); it('should return Companies ', function () { this.myResult = this.userModel.getOperatingCompanies(); scope.$apply(); expect(this.myResult.$$state.value[0].name).toEqual('#Calsoft'); expect(this.myResult.$$state.value[0].id).toEqual('AGGAA5V0HGVEGAOXNUF8OWRK5T4PP1'); this.userModel.operatingCompanies = true; this.myResult = this.userModel.getOperatingCompanies(); expect(this.myResult.$$state.value.companies).toBe(true); }); it('should return User Preferences', function () { this.preferenceGroup = 0; this.userModel.userConfig = ['test', 'test2', 'test3']; this.myResult = this.userModel.getUserPreferences(this.preferenceGroup); expect(this.myResult.$$state.value).toEqual('test'); this.preferenceGroup = 'test'; this.userModel.userConfig = []; this.myResult = this.userModel.getUserPreferences(this.preferenceGroup); scope.$apply(); expect(this.myResult.$$state.value).toBeDefined(); }); it('should return BCM Integration Config details', function () { this.myResult = this.userModel.getBCMIntegrationConfig(); scope.$apply(); expect(this.myResult.$$state.value.configLoaded).toBeDefined(); this.userModel.bcmConfig = { configLoaded: true }; this.myResult = this.userModel.getBCMIntegrationConfig(); expect(this.myResult.$$state.value.configLoaded).toBeDefined(); }); it('should be Login', function () { this.username = 'test'; this.password = 'test password'; this.myResult = this.userModel.doBCMLogin(this.username, this.password); scope.$apply(); expect(this.myResult.$$state.value.configLoaded).toBe(true); }); it('should update the User Preferences', function () { this.preferenceGroup = 'test'; this.details = { id: 1234, value: 'test value', systemgenerated: 'test systemgenerated' }; this.args = { updateColumns: true }; this.myResult = this.userModel.updateUserPreferences(this.preferenceGroup, this.details, this.args); scope.$apply(); expect(this.myResult.$$state.value[0]).toEqual('test'); }); it('should encode userid', function () { this.userModel.getFullCurrentUserData(); scope.$apply(); expect(this.userModel.userId).toBe(encodeURIComponent(this.localStorageService.get('user.userId'))); }); });