Advertisement
Guest User

Untitled

a guest
Dec 26th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. angular.module('login.controller', [])
  2.  
  3. .controller('LoginCtrl', function($scope, $state, $translate, $ionicPopup, UserService, Auth) {
  4.  
  5. // With the new view caching in Ionic, Controllers are only called
  6. // when they are recreated or on app start, instead of every page change.
  7. // To listen for when this page is active (for example, to refresh data),
  8. // listen for the $ionicView.enter event:
  9. //$scope.$on('$ionicView.enter', function(e) {
  10. //});
  11.  
  12. // Form data for the login modal
  13. $scope.loginData = {};
  14.  
  15. // Perform the login action when the user submits the login form
  16. $scope.doLogin = function() {
  17.  
  18. var onSuccess = function(response) {
  19. if (response.data.user !== null) {
  20. Auth.setUser(response.data.user);
  21. Auth.setToken(response.data.user.token);
  22.  
  23. $state.go('app.main');
  24. } else if (response.data.result == 101) {
  25. $ionicPopup.alert({
  26. title: $translate.instant('login_error'),
  27. template: $translate.instant('login_not_verified')
  28. });
  29. }else {
  30. $ionicPopup.alert({
  31. title: $translate.instant('login_error'),
  32. template: $translate.instant('login_bad_password_text')
  33. });
  34. }
  35.  
  36. };
  37.  
  38. var onError = function() {
  39. $ionicPopup.alert({
  40. title: $translate.instant('login_error'),
  41. template: $translate.instant('login_error_text')
  42. });
  43. };
  44. console.log('Doing login', $scope.loginData);
  45.  
  46. UserService.login($scope.loginData.username, $scope.loginData.password).then(onSuccess, onError);
  47.  
  48. };
  49.  
  50. $scope.doRegister = function() {
  51. $state.go("register");
  52. };
  53. });
  54.  
  55. describe('LoginCtrl', function() {
  56.  
  57. var controller,
  58. deferredLogin,
  59. userServiceMock,
  60. stateMock,
  61. scopeMock,
  62. ionicPopupMock;
  63.  
  64. //Load the App module
  65. beforeEach(module('starter'));
  66.  
  67. // Instantiate the Controller and Mocks
  68.  
  69. beforeEach(inject(function($controller, $q, $rootScope, $translate, Auth) {
  70. deferredLogin = $q.defer();
  71.  
  72.  
  73.  
  74. scopeMock = $rootScope.$new();
  75.  
  76. // mock userServiceMock
  77. userServiceMock = {
  78. login: jasmine.createSpy('login spy')
  79. .and.returnValue(deferredLogin.promise)
  80. };
  81.  
  82. //mock $state
  83. stateMock = jasmine.createSpyObj('$state spy', ['go']);
  84.  
  85. //mock $ionicPopup
  86. ionicPopupMock = jasmine.createSpyObj('$ionicPopup spy', ['alert']);
  87.  
  88. //Instantiate LoginCtrl
  89. controller = $controller('LoginCtrl', {
  90. '$scope' : scopeMock,
  91. '$state' : stateMock,
  92. '$translate' : $translate,
  93. '$ionicPopup' : ionicPopupMock,
  94. 'UserService' : userServiceMock,
  95. 'Auth' : Auth
  96. });
  97. }));
  98.  
  99. describe('#doLogin', function() {
  100. // Call doLogin on the Controllers
  101. beforeEach(inject(function(_$rootScope_) {
  102. $rootScope = _$rootScope_;
  103. controller.$scope.loginData.username = 'test';
  104. controller.$scope.loginData.password = 'password';
  105. controller.$scope.doLogin();
  106.  
  107. }));
  108.  
  109. it('should call login on userService', function() {
  110. expect(userServiceMock.login).toHaveBeenCalledWith('test','password');
  111. });
  112.  
  113. describe('when the login is executed,', function() {
  114. it('if successful, should change state to app.main', function() {
  115.  
  116. //TODO: Mock the login response from userService
  117.  
  118. expect(stateMock.go).toHaveBeenCalledWith('app.main');
  119. });
  120.  
  121. it('if unsuccessful, should show popup', function() {
  122. //TODO: Mock the login response from userService
  123. expect(ionicPopup.alert).toHaveBeenCalled();
  124. });
  125. });
  126. });
  127. });
  128.  
  129. // Karma configuration
  130. // Generated on Mon Dec 26 2016 10:38:06 GMT+0100 (CET)
  131.  
  132. module.exports = function(config) {
  133. config.set({
  134.  
  135. // base path that will be used to resolve all patterns (eg. files, exclude)
  136. basePath: '',
  137.  
  138.  
  139. // frameworks to use
  140. // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
  141. frameworks: ['jasmine'],
  142.  
  143.  
  144. // list of files / patterns to load in the browser
  145. files: [
  146. '../www/lib/ionic/js/ionic.bundle.js',
  147. '../www/js/**/*.js',
  148. '../www/lib/angular-mocks/angular-mocks.js',
  149. 'unit-tests/**/*.js'
  150. ],
  151.  
  152.  
  153. // list of files to exclude
  154. exclude: [
  155. ],
  156.  
  157.  
  158. // preprocess matching files before serving them to the browser
  159. // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
  160. preprocessors: {
  161. },
  162.  
  163.  
  164. // test results reporter to use
  165. // possible values: 'dots', 'progress'
  166. // available reporters: https://npmjs.org/browse/keyword/karma-reporter
  167. reporters: ['progress'],
  168.  
  169.  
  170. // web server port
  171. port: 9876,
  172.  
  173.  
  174. // enable / disable colors in the output (reporters and logs)
  175. colors: true,
  176.  
  177.  
  178. // level of logging
  179. // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
  180. logLevel: config.LOG_INFO,
  181.  
  182.  
  183. // enable / disable watching file and executing tests whenever any file changes
  184. autoWatch: true,
  185.  
  186.  
  187. // start these browsers
  188. // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
  189. browsers: ['PhantomJS'],
  190.  
  191.  
  192. // Continuous Integration mode
  193. // if true, Karma captures browsers, runs the tests and exits
  194. singleRun: false,
  195.  
  196. // Concurrency level
  197. // how many browser should be started simultaneous
  198. concurrency: Infinity
  199. });
  200. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement