UNPKG

gaf-mobile

Version:

GAF mobile Web site

125 lines (108 loc) 3.59 kB
'use strict'; describe('Controller: ResetPasswordCtrl', function() { // Vars var $scope, $rootScope, $location, $q, createCtrl; // Mocks var AuthMock, AnalyticsMock; // Load the controller's module beforeEach(module('gafMobileApp')); // Initialize the controller beforeEach(inject(function($controller, _$rootScope_, _$location_, _$q_) { $location = _$location_; $q = _$q_; $rootScope = _$rootScope_; $scope = $rootScope.$new(); // Controller constructer to enable to tweak the // mock ups before instantiate it createCtrl = function() { return $controller('ResetPasswordCtrl', { $scope: $scope, $location: $location, Auth: AuthMock, Analytics: AnalyticsMock }); }; })); // Create the mock ups beforeEach(function() { AuthMock = jasmine.createSpyObj('Auth', ['resetPassword', 'isAuthenticated']); AnalyticsMock = jasmine.createSpyObj('Analytics', ['trackAction', 'trackError']); $location.host = function () { return 'm.freelancer.com'; }; }); it('should not show submit success page by default', function() { AuthMock.isAuthenticated.and.returnValue(false); var ctrl = createCtrl(); expect(ctrl.state.success).toBe(false); }); it('should redirect user to home if already authenticated', function() { AuthMock.isAuthenticated.and.returnValue(true); createCtrl(); expect($location.path()).toBe('/'); }); it('should show submit success page when reset password is successful', function() { AuthMock.isAuthenticated.and.returnValue(false); AuthMock.resetPassword.and.returnValue($q.when(true)); var ctrl = createCtrl(); ctrl.resetPassword('email'); $scope.$digest(); expect(ctrl.state.success).toBe(true); }); it('should clear previous errors when form is submitted', function() { AuthMock.isAuthenticated.and.returnValue(false); AuthMock.resetPassword.and.returnValue($q.when(true)); var ctrl = createCtrl(); ctrl.resetPassword('email'); $scope.$digest(); expect(ctrl.error).toEqual({}); }); it('should show error when unknown email', function() { AuthMock.isAuthenticated.and.returnValue(false); AuthMock.resetPassword.and.returnValue($q.reject({ code: 'UNKNOWN_EMAIL' })); var ctrl = createCtrl(); ctrl.resetPassword('email'); $scope.$digest(); expect(ctrl.error.unknownEmail).toBe(true); }); it('should show error when invalid email', function() { AuthMock.isAuthenticated.and.returnValue(false); AuthMock.resetPassword.and.returnValue($q.reject({ code: 'INVALID_EMAIL' })); var ctrl = createCtrl(); ctrl.resetPassword('email'); $scope.$digest(); expect(ctrl.error.invalidEmail).toBe(true); }); it('should show error when access denied', function() { AuthMock.isAuthenticated.and.returnValue(false); AuthMock.resetPassword.and.returnValue($q.reject({ code: 'ACCESS_DENIED' })); var ctrl = createCtrl(); ctrl.resetPassword('email'); $scope.$digest(); expect(ctrl.error.accessDenied).toBe(true); }); it('should show error when unknown internal error', function() { AuthMock.isAuthenticated.and.returnValue(false); AuthMock.resetPassword.and.returnValue($q.reject({ code: 'INTERNAL_ERROR' })); var ctrl = createCtrl(); ctrl.resetPassword('email'); $scope.$digest(); expect(ctrl.error.internalError).toBe('INTERNAL_ERROR'); }); });