Created
January 20, 2016 16:45
-
-
Save dam1/5858bdcabb89effca457 to your computer and use it in GitHub Desktop.
Example Login controller test with Angular, Ionic, $translate, Karma, Jasmine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('starter.controllers') | |
.controller('LoginCtrl', function ($scope, $state, DataCacheService, $rootScope, User, $ionicPopup, $translate, $ionicLoading, InitializeService) { | |
$rootScope.AAuser={password: '', email: ''}; | |
$scope.login=function () { | |
if ($rootScope.AAuser.password != null && $rootScope.AAuser.password.length >= 4) { | |
$ionicLoading.show(); | |
var loginData=$rootScope.AAuser; | |
User.login(loginData, function (data, err) { | |
$ionicLoading.hide(); | |
if (data != null && data.token != null && data.userId != null) { | |
DataCacheService.clearCache(); | |
InitializeService.initialize(); | |
} else { | |
$scope.showInvalidCredential(); | |
} | |
}); | |
} | |
else { | |
$scope.focusPassword(); | |
} | |
}; | |
$scope.focusPassword=function () { | |
setTimeout(function () { | |
cordova.plugins.Keyboard.show(); | |
document.getElementById("input-password").focus(); | |
}, 1); | |
}; | |
$scope.showInvalidCredential=function () { | |
$translate(['INVALID_CREDENTIALS', 'TRY_AGAIN']).then(function (translations) { | |
$ionicPopup.alert({ | |
title: translations.INVALID_CREDENTIALS, | |
template: translations.TRY_AGAIN | |
}); | |
}); | |
}; | |
$scope.resetPassword=function () { | |
$state.go('ForgotPassword'); | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('LoginCtrl', function () { | |
var controller, | |
deferredLogin, | |
UserServiceMock ={login:null}, | |
stateMock, | |
DataCacheService, | |
$translate, | |
InitializeService, | |
$ionicLoading, | |
ionicPopupMock, | |
scope={}, | |
rscope={}; | |
var UserServiceMockAll={ | |
rightCredentials: function (params, callback) { | |
callback({token: 'myToken', userId: '123456'}); | |
}, | |
wrongCredentials: function (para, callback) { | |
callback({status: 'err', message: 'wrong credentials'}); | |
} | |
}; | |
beforeEach(module('starter')); | |
beforeEach(module(function ($provide, $urlRouterProvider) { | |
$provide.value('$ionicTemplateCache', function () { | |
}); | |
$urlRouterProvider.deferIntercept(); | |
})); | |
describe('LoginCtrl', function () { | |
beforeEach(inject(function ($controller, $q) { | |
deferredLogin=$q.defer(); | |
DataCacheService={ | |
clearCache: jasmine.createSpy('clearCache') | |
.and.returnValue(deferredLogin.promise) | |
}; | |
$ionicLoading={ | |
show: jasmine.createSpy('show') | |
.and.returnValue(deferredLogin.promise), | |
hide: jasmine.createSpy('hide') | |
.and.returnValue(deferredLogin.promise) | |
}; | |
$translate=function (translation) { | |
return { | |
then: function (callback) { | |
var translated={}; | |
translation.map(function (transl) { | |
translated[transl]=transl; | |
}); | |
return callback(translated); | |
} | |
} | |
}; | |
jasmine.createSpy('$translate', $translate).and.callThrough(); | |
stateMock=jasmine.createSpyObj('$state spy', ['go']); | |
InitializeService=jasmine.createSpyObj('InitializeService spy', ['initialize']); | |
ionicPopupMock=jasmine.createSpyObj('$ionicPopup spy', ['alert']); | |
controller=$controller('LoginCtrl', { | |
'$scope': scope, | |
'$state': stateMock, | |
'DataCacheService': DataCacheService, | |
'$rootScope': rscope, | |
'User': UserServiceMock, | |
'$ionicPopup': ionicPopupMock, | |
'$translate': $translate, | |
'$ionicLoading': $ionicLoading, | |
'InitializeService': InitializeService | |
} | |
); | |
})); | |
it('should focus the next input if the password not entered ', function () { | |
rscope.AAuser.email='[email protected]'; | |
spyOn(scope, "focusPassword").and.callThrough(); | |
scope.login(); | |
expect(scope.focusPassword).toHaveBeenCalled(); | |
}); | |
it('should not login with wrong credentials ', function () { | |
UserServiceMock.login=UserServiceMockAll.wrongCredentials; | |
rscope.AAuser.email='[email protected]'; | |
rscope.AAuser.password='qwerty'; | |
spyOn(scope, "showInvalidCredential").and.callThrough(); | |
scope.login(); | |
expect(scope.showInvalidCredential).toHaveBeenCalled(); | |
expect($ionicLoading.show).toHaveBeenCalled(); | |
expect($ionicLoading.hide).toHaveBeenCalled(); | |
expect(ionicPopupMock.alert).toHaveBeenCalledWith({ | |
title: 'INVALID_CREDENTIALS', | |
template: 'TRY_AGAIN' | |
}); | |
}); | |
it('should login with right credentials', function () { | |
UserServiceMock.login=UserServiceMockAll.rightCredentials; | |
rscope.AAuser.email='[email protected]'; | |
rscope.AAuser.password='qwerty'; | |
scope.login(); | |
expect($ionicLoading.show).toHaveBeenCalled(); | |
expect($ionicLoading.hide).toHaveBeenCalled(); | |
}); | |
it('should go to resetPassword', function () { | |
rscope.AAuser.email='[email protected]'; | |
scope.resetPassword(); | |
expect(stateMock.go).toHaveBeenCalledWith('ForgotPassword'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment