I am trying to test the some of the validation for a form I have in a registration view. I have the view file created which contains a form. Now I need to know how I can access this form from inside a unit test so that I can verify the $setValidity is working as expected.
Here's the code I am trying to test:
angular
.module('enigma.registration', ['ngMessages'])
.controller('Registration', Registration);
Registration.$inject = ['$state', 'authFactory'];
function Registration($state, authFactory) {
var reg = this;
reg.alreadyRegistered = alreadyRegistered;
reg.error = '';
function alreadyRegistered() {
if(reg.regForm.email.$valid){
authFactory.doesUserExist(reg.user)
.then(function(response){
if(response.data.message === 'user exists') {
reg.regForm.email.$setValidity('userExists', false); // I am missing coverage of this line.
} else {
reg.regForm.email.$setValidity('userExists', true); // I am missing coverage of this line.
}
})
.catch(function(err){
reg.error = err;
});
} else { // Input is invalid for reasons other than existing email, do not delete ~ EFarley 9/23/15
reg.regForm.email.$setValidity('userExists', true); // Remove userExists validation error.
}
};
}
I have tried to unit test this but no matter what I do I can't access the regForm.email.$error property from my unit tests.
Lastly here's how I wrote my unit test:
describe('Registration Controller Tests', function() {
var $controller, $scope, defer, doesUserExistSpy, authFactory, Registration, $httpBackend, authReqHandler, loginReqHandler, userReqHandler, $state, goSpy, setValiditySpy;
beforeEach(module('enigma'));
beforeEach(inject(function (_$controller_, _$rootScope_, $q, $injector) {
$controller = _$controller_;
$scope = _$rootScope_;
defer = $q.defer();
// Create spies
doesUserExistSpy = jasmine.createSpy('doesUserExist').and.returnValue(defer.promise);
authFactory = {
doesUserExist: doesUserExistSpy
};
Registration = $controller('Registration', {
$scope: $scope,
authFactory: authFactory,
$state: $state
});
});
describe('check email field validity', function () {
var template, element, regForm;
beforeEach(inject(function ($templateCache, $compile) {
template = $templateCache.get('../public/modules/registration/registration.html');
element = angular.element(template);
$compile(element)($scope);
regForm = $scope.regForm;
}));
it('should set regForm.email.$error.userExists to true if /doesUserExist returns "user exists"', function () {
$httpBackend.whenPOST('/doesUserExist').respond('user exists');
Registration.alreadyRegistered;
$scope.$digest();
expect(regForm.email.$error.userExists).toEqual(true);
});
});
});
However template always equals undefined, which causes element to be undefined and lastly regForm to be undefined. Because everything is undefined I'm unable to access the $error property of the email field. How do I access the form in my test?