How to mock service for angularjs directive's controller - angularjs

I have a directive that i want to unit test. This works but i need to use the service in the controller. How do i go about this?
app.directive('myDirective', function() {
return {
restrict: 'EA',
scope: true,
templateUrl: 'my-directive.html',
controllerAs: 'md',
link:function (scope){
},
controller: function($scope, $element, $stateParams,service1) {
this.test = service1.testData;
}
}
});
And my unit test file so far
describe("Directive : my directive", function() {
var element, scope, controller, service1,stateParams;
beforeEach(module("app"));
beforeEach(module('src/templates/my-directive.html'));
beforeEach(function() {
service1 = {
method : {
name : "Test"
}
};
});
beforeEach(inject(function($compile, $rootScope ) {
scope = $rootScope.$new();
element = "<my-directive></my-directive>";
element = $compile(element)(scope);
scope.$digest();
controller = element.controller("myDirective");
}));
it("should do something to the scope", function() {
// test functions using service1
})
});
and my service
app.factory('service1', function(){
service1.testData = false;
service1.myPromise = function (t) {
var deferred = $q.defer();
$http.get("....")
.success(function(result) {
deferred.resolve(result)
})
.error(function(error) {
deferred.reject(error)
});
return deferred.promise;
}
});
How do i add my mocked service to my controller?

Before compiling the directive you can use the $provide service in a beforeEach block.
var service1;
beforeEach(module(function($provide) {
service1 = {
testData: 'Test data',
myPromise: function() {}
};
$provide.value('service1', service1);
}));
If your service method returns a promise, you can spy on the method and return a promise when the method is called.
var deferred;
beforeEach(inject(function($q) {
deferred = $q.defer();
spyOn(service1, 'myPromise').and.returnValue(deferred.promise);
}));
So later in your specs you can reject or resolve the promise.
deferred.resolve();
deferred.reject();

Related

How to write unit test case for scope and timeout function inside directive

I am using isolate scope in custom directive. I have updated plunker link. http://plnkr.co/edit/NBQqjxW8xvqMgfW9AVek?p=preview
Can someone help me in writing unit test case for script.js file.
script.js
var app = angular.module('app', [])
app.directive('myDirective', function($timeout) {
return {
restrict: 'A',
scope: {
content: '='
},
templateUrl: 'my-directive.html',
link: function(scope, element, attr) {
$timeout(function() {
element = element[0].querySelectorAll('div.outerDiv div.innerDiv3 p.myClass');
var height = element[0].offsetHeight;
if (height > 40) {
angular.element(element).addClass('expandable');
scope.isShowMore = true;
}
})
scope.showMore = function() {
angular.element(element).removeClass('expandable');
scope.isShowMore = false;
};
scope.showLess = function() {
angular.element(element).addClass('expandable');
scope.isShowMore = true;
};
}
}
})
(function() {
'use strict';
describe('Unit testing directive', function() {
var $compile, scope, element, compiledDirective, $rootScope, $timeout;
beforeEach(module("app"));
beforeEach(inject(function(_$compile_, _$rootScope_, _$timeout_) {
$compile = _$compile_;
scope = _$rootScope_.$new();
$timeout = _$timeout_;
element = angular.element(' <div my-directive content="content"></div>');
compiledDirective = $compile(element)(scope);
scope.$digest();
}));
it('should apply template', function() {
expect(compiledDirective.html()).toBe('');
});
it('check for timeout', function() {
$timeout.flush();
});
});
})();
Use $timeout.flush() function for writing testcase for $timeout
it('check for timeout', function() {
scope.digest();
// flush timeout(s) for all code under test.
$timeout.flush();
// this will throw an exception if there are any pending timeouts.
$timeout.verifyNoPendingTasks();
expect(scope.isShowMore).toBeTruthy();
});
Check this article for better understanding.

How to test a directive having hardcoded AJAX call?

I am not sure how can I test this directive, would someone provide a code snippet? Here is my directive:
.directive('checkUnique', ['$http', function($http) {
return {
require: 'ngModel',
link: function(scope, ele, attrs, c) {
var origonalVal = attrs.ignoreUniqueValue;
scope.$watch(attrs.ngModel, function() {
var toURL= 'to/an/api';
$http({
method: 'GET',
url: toURL
}).success(function(isUnique,status,headers,cfg) {
var iu = isUnique == 'true' ? true : false;
c.$setValidity('unique', iu);
}).error(function(data,status,headers,cfg) {
c.$setValidity('unique', false);
});
});
}
}
}])
First of all it is not a good idea to have this logic in the link function of your directive. Here a setup that I would use (simplified and not tested):
var myApp = angular.module('myApp', []);
myApp.factory('dataService', function($q, $http){
return {
isUnique: function(){
return $q(function(resolve, reject){
$http({
method: 'GET',
url: 'to/an/api'
}).success(function(isUnique,status,headers,cfg) {
resolve(isUnique == 'true');
}).error(function(data,status,headers,cfg) {
reject();
});
});
}
}
});
myApp.controller('UniqueController', function($scope, dataService){
var vm = this,
unWatchNgModel;
unWatchNgModel = $scope.$watch('ngModel', onNgModelChanged);
$scope.$on('$destroy', onDestroy);
function onNgModelChanged(){
dataService.isUnique().then(function(unique){
vm.ngModelCtrl.$setValidity('unique', unique);
});
}
function onDestroy(){
unWatchNgModel();
}
});
myApp.directive('checkUnique', ['$http', function($http) {
return {
require: ['checkUnique', 'ngModel'],
scope: {
ngModel: '='
}
controller: 'UniqueController',
controllerAs: 'unique',
bindToController: true
link: link
};
function link(scope, ele, attrs, ctrls) {
var checkUniqueCtrl = ctrls[0],
ngModelCtrl = ctrls[1];
checkUniqueCtrl.ngModelCtrl = ngModelCtrl;
}
}]);
To test this (the ajax part), use a setup like this:
// Note that you need the 'ngMockE2E' module to have access to the $httpBackend service.
describe('dataService', function() {
'use strict';
var dataService;
beforeEach(function() {
module('myApp');
inject(function($injector) {
dataService = $injector.get('dataService');
$httpBackend = $injector.get('$httpBackend');
});
});
describe('isUnique', function() {
it('should return true if the API returns true as value.', function() {
// Setup
var successSpy = jasmine.createSpy('success');
$httpBackend.expectGET(endpoint).respond(200, 'true');
// Execute
dataService.isUnique(successSpy);
$httpBackend.flush();
// Test
expect(successSpy).toHaveBeenCalledWith(true);
});
it('should return false if the API does not return true as value.', function() {
// Setup
var successSpy = jasmine.createSpy('success');
$httpBackend.expectGET(endpoint).respond(200, 'bogus');
// Execute
dataService.isUnique(successSpy);
$httpBackend.flush();
// Test
expect(successSpy).toHaveBeenCalledWith(false);
});
});
});

Errors thrown when unit testing directive with Angular

I have a custom directive that uses a service. When I run my unit tests, I keep getting the thrown error 'Library does not exist on window'. How can I avoid getting that error in my Unit test?
example service
angular.module('example')
.factory('thirdParty', ['$window', function($window) {
if (typeof $window.thirdParty === 'undefined') {
throw new Error('Library does not exist on window');
} else {
return $window.thirdParty;
}
}]);
custom directive
angular.module('example')
.directive('customDirective', ['thirdParty',
function(thirdParty) {
var defaults, link;
link = function(scope, element, attrs, ctrls) {
// do something with thirdParty
};
return {
restrict: 'A',
require: 'ngModel',
link: link,
};
}]);
test
describe('customDirective', function() {
var element, compile, scope;
beforeEach(module('example'));
beforeEach(inject(function($compile, $rootScope) {
compile = $compile;
scope = $rootScope.$new();
}));
// Manually compile and link our directive
function getCompiledElement(template) {
var compiledElement;
var validTemplate = '<input ng-model="example.data" custom-directive />';
compiledElement = compile(template || validTemplate)(scope);
scope.$digest();
return compiledElement;
}
it('should do something', function() {
element = getCompiledElement();
// expects
});
});
You need to inject $window (stub that out too, for good measure) and stub/mock out the thirdParty lib.
var $window;
beforeEach(function () {
$window = {};
module('yourmodule', function ($provide) {
$provide.value('$window', $window);
});
$window.thirdParty = {};
});
it('throws', function () {
delete $window.thirdParty;
function fn () {
getCompiledElement();
}
expect(fn).to.throw(/does not exist on window/i);
});
it('just works™', function () {
function fn () {
getCompiledElement();
}
expect(fn).to.not.throw;
});

Using jasmine to test angular directive that uses a controller

I am trying to write a jasmine test that will test if an angular directive I've written is working.
Here is my spec file:
describe('blurb directive', function () {
var scope, httpMock, element, controller;
beforeEach(module('mdotTamcCouncil'));
beforeEach(module('mdotTamcCouncil.core'));
beforeEach(module('blurb'));
beforeEach(inject(function (_$httpBackend_, $rootScope, $compile) {
element = angular.element('<mcgi-blurb text-key="mainPageIntro"></mcgi-blurb>');
var httpResponse = '<textarea name="content" ng-model="content"></textarea>';
scope = $rootScope.$new();
httpMock = _$httpBackend_;
httpMock.whenGET('components/blurb/blurb.html').respond(httpResponse);
element = $compile(element)(scope);
scope.$digest();
}));
it('should have some content', function () {
expect(scope.content).toBeDefined();
});
});
The value "scope.content" is always undefined and when I look at the scope object it seems to be a generic scope object that doesn't have my custom attributes on it.
Here are the other related files:
blurb-directive.js
(function () {
'use strict';
angular.module('blurb')
.directive('mcgiBlurb', blurb);
function blurb() {
return {
restrict: 'E',
replace: true,
templateUrl: jsGlobals.componentsFolder + '/blurb/blurb.html',
controller: 'BlurbController',
controllerAs: 'blurb',
bindToController: false,
scope: {
textKey: "#"
}
};
};
})();
blurb-controller.js
(function () {
angular.module('blurb')
.controller('BlurbController', ['$scope', 'blurbsFactory', 'userFactory', function ($scope, blurbsFactory, userFactory) {
$scope.content = "";
$scope.blurbs = {};
$scope.currentUser = {};
this.editMode = false;
userFactory().success(function (data) {
$scope.currentUser = data;
});
blurbsFactory().success(function (data) {
$scope.blurbs = data;
$scope.content = $scope.blurbs[$scope.textKey];
});
this.enterEditMode = function () {
this.editMode = true;
};
this.saveEdits = function () {
this.editMode = false;
$scope.blurbs[$scope.textKey] = $scope.content;
};
}]);
})();
What am I doing wrong?
The directive has isolated scope, so the scope passed to its controller and link function (if there was one), is the isolated one, different than your scope.
You may have luck getting the scope of the directive using element.isolateScope(); you may not, because of the replace: true - try to make sure. You may also access the controller instance using element.controller('mcgiBlurb').

Unit testing Angular directive with $http

I have an Angular directive that, when attached to an <input>, waits one second after user input before querying an endpoint with $http. In short, it's meant to check username uniqueness.
It looks like this:
.directive('sgValidUsername', ['$http', function(http) {
var waitTimer;
var checkIfUserExists = function(e, ctrl) {
http.get('/publicapi/users/' + e.target.value + '/exists')
.success(function(data) {
ctrl.$setValidity('unique', !data.exists);
});
};
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
element.on('blur keyup', function(e) {
if (e.target.value) {
clearInterval(waitTimer);
waitTimer = setTimeout(function() {
checkIfUserExists(e, ctrl);
}, 1000);
}
});
}
};
}]);
I'm trying my best to write a good comprehensive Jasmine unit test suite, but it's not working out because I couldn't find an appropriate example to learn from. I end up reconstructing the directive in test form rather than actually testing the directive. Also, I get a 'no pending request to flush' error.
Any suggestions for the below?
describe('SignupForm directives', function () {
var form, // el to which directive is applied
$scope,
$httpBackend, // $http mock
usernameExistsHandler;
beforeEach(function() {
module('signupform.directives');
});
beforeEach(function() {
inject(function ($injector, $rootScope, $compile, $q, $timeout) {
$scope = $rootScope;
$httpBackend = $injector.get('$httpBackend');
usernameExistsHandler = $httpBackend.whenGET(/\/publicapi\/users\/.+?\/exists/);
var el = angular.element('<form name="form"><input type="text" name="username" ng-model="user.username" sg-username-is-valid /></form>');
$scope.user = { username: null };
$compile(el)($scope);
form = $scope.form;
});
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should invalidate with existing usernames', function() {
form.username.$setViewValue('username_in_use');
$scope.$digest();
expect($scope.user.username).toEqual('username_in_use');
usernameExistsHandler.respond('200', { exists: true });
$httpBackend.expectGET('/publicapi/users/' + $scope.user.username + '/exists/');
$httpBackend.flush();
expect(form.username.$valid).toBe(false);
});

Resources