UNPKG

gaf-mobile

Version:

GAF mobile Web site

160 lines (130 loc) 4.32 kB
'use strict'; describe('Controller: PhoneVerificationCtrl', function () { var $scope; var $rootScope; var $location; var $q; var UsersMock; var loggedInUserMock; var AnalyticsMock; var createCtrl; var userData = { location: { country: { name: 'Australia' } }, status: { phone_verified: false } }; beforeEach(module('gafMobileApp')); beforeEach(inject(function($controller, _$rootScope_, _$location_, _$q_, _COUNTRY_PHONE_CODES_) { $controller = $controller; $rootScope = _$rootScope_; $scope = $rootScope.$new(); $location = _$location_; $q = _$q_; createCtrl = function() { return $controller('PhoneVerificationCtrl', { $scope: $scope, $rootScope: $rootScope, $location: $location, Users: UsersMock, Analytics: AnalyticsMock, loggedInUser: loggedInUserMock, COUNTRY_PHONE_CODES: _COUNTRY_PHONE_CODES_ }); }; })); beforeEach(function() { UsersMock = jasmine.createSpyObj('Users', ['sendPhoneVerification', 'submitPhoneVerification']); loggedInUserMock = jasmine.createSpyObj('loggedInUser', ['get']); AnalyticsMock = jasmine.createSpyObj('Analytics', ['trackAction', 'trackError']); loggedInUserMock.get.and.returnValue(userData); }); describe('initialisation', function() { it('should get the logged in user', function() { var ctrl = createCtrl(); expect(loggedInUserMock.get).toHaveBeenCalled(); expect(ctrl.user.location.country.name).toBe('Australia'); expect(ctrl.user.status.phone_verified).toBeFalsy(); expect(ctrl.viewState).toBe('notVerified'); }); }); describe('sending the verification code', function() { var ctrl; beforeEach(function() { ctrl = createCtrl(); }); it('should make an api request to send the code', function() { UsersMock.sendPhoneVerification.and.returnValue($q.when({})); ctrl.sendVerificationCode(); $scope.$digest(); expect(UsersMock.sendPhoneVerification).toHaveBeenCalled(); }); describe('success', function() { it('should set the referenceId after sending the code', function() { UsersMock.sendPhoneVerification.and.returnValue($q.when({ reference_id: 'ABCDEFGH12345678', })); ctrl.sendVerificationCode(); $scope.$digest(); expect(ctrl.viewState).toBe('codeSent'); expect(ctrl.referenceId).toBe('ABCDEFGH12345678'); }); }); describe('failure', function() { it('should store the error message', function() { UsersMock.sendPhoneVerification.and.returnValue($q.reject({ data: { message: 'send error message' } })); ctrl.sendVerificationCode(); $scope.$digest(); expect(ctrl.sendError.error).toBeTruthy(); expect(ctrl.sendError.message).toBe('send error message'); }); }); }); describe('submitting the verification code', function() { var ctrl; beforeEach(function() { ctrl = createCtrl(); ctrl.codeSent = true; ctrl.referenceId = 'ABCDEFGH12345678'; }); it('should make an api request to submit the code', function() { UsersMock.submitPhoneVerification.and.returnValue($q.when({})); ctrl.submitVerificationCode(); expect(UsersMock.submitPhoneVerification).toHaveBeenCalled(); }); describe('success', function() { it('should set verified', function() { UsersMock.submitPhoneVerification.and.returnValue($q.when({ id_verification_required: true, })); ctrl.submitVerificationCode(); $scope.$digest(); expect(ctrl.viewState).toBe('verified'); }); }); describe('failure', function() { it('should store the error message', function() { UsersMock.submitPhoneVerification.and.returnValue($q.reject({ data: { message: 'submit error message' } })); ctrl.submitVerificationCode(); $scope.$digest(); expect(ctrl.submitError.error).toBeTruthy(); expect(ctrl.submitError.message).toBe('submit error message'); }); }); }); });