Angular directive test throws an error - unit-testing

Here is the directive, that wraps jquery-ui autocomplete
angular.module('myApp.directives', [])
.directive('autocomplete', function () {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<input ng-model="autocomplete" type="text"/>',
link: function (scope, element, attrs) {
scope.$watch(attrs.typedvalue, function () {
element.autocomplete({
search: function (event) {
scope[attrs.typedvalue] = this.value;
scope[attrs.fullselection] = '';
scope[attrs.selectionid] = '';
scope[attrs.shortselection] = '';
scope.$apply();
},
source: scope.fetchList,
select: function (event, ui) {
scope[attrs.fullselection] = ui.item.label;
scope[attrs.selectionid] = ui.item.itemId;
scope[attrs.shortselection] = ui.item.value;
scope.$apply();
}
});
});
}
};
});
I'm trying to unit-test it with the following test (following instructions from here https://github.com/vojtajina/ng-directive-testing):
describe('Directives', function () {
beforeEach(module('myApp.directives'));
describe('autocomplete directive', function () {
var elm, scope;
beforeEach(inject(function ($rootScope, $compile) {
elm = angular.element('<autocomplete fullselection="fullDstn" shortselection="dstn" selectionid="geonameId" typedvalue="typedValue" id="DstnSlctr"/>');
scope = $rootScope;
$compile(elm)(scope);
scope.$digest();
}));
it('should create input', inject(function ($compile, $rootScope) {
expect(elm.id).toBe('DstnSlctr');
expect(elm.prop('tagName')).toBe('INPUT');
debugger;
}));
});
});
But I get an error:
TypeError: Object [[object HTMLInputElement]] has no method 'autocomplete'
at Object.fn (C:/Users/kmukhort/Documents/_files/TMate/AngularTest/a
pp/js/directives.js:13:33)
on the line element.autocomplete({
I suspect that jquery-ui functionality is not attached to the element while $compile.
I'm referring jquery-ui library in testacular.config
basePath = '../';
files = [
...
'app/lib/jquery-ui-*.js',
];
Could you, please, tell, what I'm doing wrong?
Thanks!
Ksenia

I think you need to replace:
element.autocomplete(...);
With:
$(element).autocomplete(...);

Related

Can't get a directive using a controller test to pass Jasmine : Error: [$injector:unpr] Unknown provider: utilsProvider <- utils

I've been searching a long time for an answer to the question I'm about to ask without success.
Let's say I have the following directive :
(function () {
'use strict';
angular
.module('com.acme.mymod')
.directive('myDirective', myDirective);
function myDirective() {
var directive = {
restrict: 'EA',
scope: {},
replace: true,
templateUrl: 'folder/myDirective/myDirective.tpl.html',
controller: "myDirectiveController",
controllerAs: 'vm',
bindToController: {
somedata: "#?",
endpoint: "#?"
},
link: link
};
return directive;
function link(scope, element) {
activate();
function activate() {
scope.$on('$destroy', destroy);
element.on('$destroy', destroy);
}
}
function destroy() {
}
}
})();
myDirectiveController is as follow:
(function () {
'use strict';
angular
.module('com.acme.mymod')
.controller('myDirectiveController', myDirectiveController);
myDirectiveController.$inject = ['utils', '$log'];
// utils is an external library factory in another module
function myDirectiveController(utils, $log) {
var vm = this;
vm.menuIsOpen = false;
function activate() {
vm.dataPromise = utils.getValuePromise(null, vm.somedata, vm.endpoint);
vm.dataPromise.then(function (result) {
vm.data = result.data;
}, function () {
$log.debug("data is not Valid");
});
}
activate();
}
})();
The spec file is as follow:
describe('myDirective Spec', function () {
'use strict';
angular.module('com.acme.mymod', []);
var compile, scope, directiveElem, utils;
beforeEach(function(){
module('com.acme.mymod');
inject(function($compile, $rootScope, $injector,utils){
compile = $compile;
scope = $rootScope.$new();
scope.$digest();
directiveElem = getCompiledElement();
//utils=utils;
console.log(utils.test());
});
});
function getCompiledElement(){
var element = angular.element('<div my-directive="" data-some-data=\' lorem\'></div>');
var compiledElement = compile(element)(scope);
scope.$digest();
return compiledElement;
}
it('should have a nav element of class', function () {
var navElement = directiveElem.find('nav');
expect(navElement.attr('class')).toBeDefined();
});
it('should have a valid json data-model' ,function () {
var data=directiveElem.attr('data-somedata');
var validJSON=false;
try{
validJSON=JSON.parse(dataNav);
}
catch(e){
}
expect(validJSON).toBeTruthy();
});
});
What I can't quite figure out is that every test I try to run, the directive is not compiled or created correctly I'm not sure.
I get :
Error: [$injector:unpr] Unknown provider: utilsProvider <- utils
I tried:
Injecting the controller or utils with $injector.get()
Looked at this post $injector:unpr Unknown provider error when testing with Jasmine
Any tips, pointers or clues as to what I'm doing wrong would be more than welcome
I found the solution after feeling a hitch in my brain :)
In the beforeEach function, all I needed to do is to reference my utils module name this way:
module('com.acme.myutilsmod');
This line "expose" modules components so consumers can use it.

testing directive, dom manipulation not occurring - Angularjs/jasmine

I am running my first test on a directive, and while it seems that the directive is being invoked (there is a GET request being made for the directive's template url), the DOM manipulation that is supposed to occur in the link function is not - well it's that or I'm just not testing it correctly.
// Generated by CoffeeScript 1.6.3
(function() {
var sprangularDirectives;
sprangularDirectives = angular.module('sprangularDirectives', []);
sprangularDirectives.directive('productDirective', function() {
return {
scope: {
product: '='
},
templateUrl: 'partials/product/_product.html',
link: function(scope, el, attrs) {
return el.attr('testattr', 'isthisclasshere');
}
};
});
}).call(this);
Test:
'use strict';
describe('productDirective', function() {
var scope, el, directive, $httpBackend, compiler, compiled, html;
beforeEach(angular.mock.module('sprangularApp'));
beforeEach(function() {
html = '<div data-product-directive product="currentProduct"></div>';
inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
// jasmine.getHTMLFixtures().fixturesPath='base/partials/product';
$httpBackend.when('GET', 'partials/product/_product.html').respond(
' <div class="product">'
+' {{ currentProduct.name }}'
+' {{ currentProduct.page }}'
+' </div>'
);
scope = $injector.get('$rootScope');
el = angular.element(html);
compiler = $injector.get('$compile');
compiled = compiler(el);
compiled(scope);
scope.$digest();
});
});
it('Should have an isolate scope', function() {
scope.currentProduct = {name: 'testing'};
console.log(el.attr('testattr'))
console.log(el.isolateScope())
expect(el.scope().product.name).toBe('testing');
});
});
console.log(el.attr('testattr')) returns undefined...even though when I boot my browser up it's there. Some help would be awesome :) thanks
The element you are using is the pre-compiled element reference. The element you want is returned from the "compiled(scope)" method call:
compiler = $injector.get('$compile');
compiled = compiler(el);
var element = compiled(scope); // <-- This guy!
I use this snippet as a testing helper method:
var compileTemplate = function (scope, rawTemplate) {
var template = angular.element(rawTemplate)
var element = $compile(template)(scope)
scope.$digest()
var new_scope = element.scope()
return [element, new_scope]
}

How to Unit Test a directive which binds from controller

I have a simple directive which simply uppercases the binding from the controller.
My unit test code isn't running. Any ideas?
html:
<div ng-controller="MainCtrl as main">
<div uppercase>{{main.message}}</div>
</div>
controller:
app.controller('MainCtrl', function(){
this.message = 'hello world'
})
directive:
app.directive('uppercase', function($timeout) {
return {
restrict: 'A',
transclude: true,
template: '<span ng-transclude></span>',
link: function(scope, el, attr) {
$timeout(function(){
el.text(el.text().toUpperCase());
});
}
}
});
My Unit Test currently is:
describe("App", function() {
var $compile
, $rootScope
, $controller
, MainCtrl
, element;
beforeEach(module('app'))
beforeEach(inject(function(_$compile_, _$rootScope_, _$controller_){
$compile = _$compile_;
$rootScope = _$rootScope_;
$controller = _$controller_;
MainCtrl = $controller('MainCtrl');
}))
describe("MainCtrl", function() {
it("should have a message property", function() {
expect(MainCtrl.message).toBe('hello world');
});
});
describe("Uppercase directive", function() {
it("should return uppercase text", function() {
element = '<p superman>{{MainCtrl.message}}</p>';
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('HELLO WORLD');
});
});
});
I cannot use $timeout in my test, so how should I test it?
I haven't tested it, but I believe that before the expect(element.text()).toBe('HELLO WORLD');, you'll have to flush your timeout.
I mean, you'd have an extra var called $timeout (in the beforeEach) and then, before the expected mentioned above, you'd call $timeout.flush().

Correct way to mock an AngularJS Service in a unit test?

I have the following controller and service that I am trying to write tests around in Jasmine. I am fairly new to this, and just wanted to see if I am taking the correct approach in testing and mocking my services.
Controller
(function () {
'use strict';
var app = angular.module('cs');
app.controller('PlateCheckCtrl', ['$scope', 'PlateCheckService', function ($scope, PlateCheckService) {
var plateCheck = {
plateNumber: '',
message: '',
alertClass: '',
checkPlate: function (plateNumber) {
var _this = this;
PlateCheckService.checkPlate(plateNumber).then(function (response) {
_this.message = response.message;
_this.alertClass = response.alertClass;
});
}
};
$scope.plateCheck = plateCheck;
}]);
}());
Service
(function () {
'use strict';
var app = angular.module('cs');
app.service('PlateCheckService', ['$http', function ($http) {
return {
checkPlate: function (plateNumber) {
return $http.post('PlateCheck/Index', {
plateNumber: plateNumber
}).then(function (response) {
return {
message: response.data.VehicleAtl === null ? 'Clean' : 'Hot',
alertClass: response.data.VehicleAtl === null ? 'alert-success' : 'alert-danger'
};
});
}
};
}]);
}());
Controller Test
describe('Spec Template', function () {
var scope,
controller;
beforeEach(function () {
module('cs');
inject(function ($rootScope, $controller, $q) {
scope = $rootScope.$new();
controller = $controller('PlateCheckCtrl', {
$scope: scope, PlateCheckService: {
checkPlate: function (plateNumber) {
var d = $q.defer();
if (plateNumber === '123') {
d.resolve({
message: 'Clean',
alertClass: 'alert-success'
})
} else {
d.resolve({
message: 'Hot',
alertClass: 'alert-danger'
})
}
return d.promise;
}
}
});
});
});
it('Should return "Clean" result', function () {
scope.plateCheck.checkPlate('123');
scope.$apply();
expect(scope.plateCheck.message).toBe('Clean');
expect(scope.plateCheck.alertClass).toBe('alert-success');
});
it('Should return "Hot" result', function () {
scope.plateCheck.checkPlate('123456');
scope.$apply();
expect(scope.plateCheck.message).toBe('Hot');
expect(scope.plateCheck.alertClass).toBe('alert-danger');
});
});
Service Tests
describe('Plate Check Service', function () {
var httpBackend,
service;
beforeEach(function () {
module('cs');
inject(function ($httpBackend, PlateCheckService) {
httpBackend = $httpBackend;
httpBackend.whenPOST('PlateCheck/Index', { plateNumber: '123' }).respond({ VehicleAtl: null });
httpBackend.whenPOST('PlateCheck/Index', { plateNumber: '123456' }).respond({ VehicleAtl: {} });
service = PlateCheckService;
});
});
afterEach(function () {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it('Should send the request to the server', function () {
httpBackend.expectPOST('PlateCheck/Index', { plateNumber: '123' });
service.checkPlate('123');
httpBackend.flush();
});
it('Should return a "Clean" result', function () {
var result;
service.checkPlate('123').then(function (response) {
result = response;
});
httpBackend.flush();
expect(result.message).toBe('Clean');
expect(result.alertClass).toBe('alert-success');
});
it('Should return a "Hot" result', function () {
var result;
service.checkPlate('123456').then(function (response) {
result = response;
});
httpBackend.flush();
expect(result.message).toBe('Hot');
expect(result.alertClass).toBe('alert-danger');
});
});
Your controller doesnt actually have any functionality only the service does. So you shouldnt be testing the service functionality in the controller.
describe('Spec Template', function () {
var scope,
controller,
PlateCheckServiceMock = {
checkPlate: function () {}
};
beforeEach(function () {
module('cs');
inject(function ($rootScope, $controller, $q) {
scope = $rootScope.$new();
spyOn(PlateCheckService, 'checkPlate').andCallThrough();
controller = $controller('PlateCheckCtrl', {
$scope: scope,
PlateCheckService: PlateCheckServiceMock
}
});
});
});

Faking a Angular Factory in a directive in jasmine

Question: How do I fake my pointFactory so I can Jasmine Unit Test it.
I have the Following Directive.
It takes the html sends it to a factory and the uses the response for some logic
CommonDirectives.directive('TextEnrichment',['PointFactory','appSettings', function (pointFactory,settings) {
return {
restrict: 'A',
link : function (scope, element, attrs) {
var text = element.html();
pointFactory.getPoints(text).then(function(response){
})}}}]);
So far my unit tests looks like this, however it doesn't work since I'm not injecting the factory.
beforeEach(module('app.common.directives'));
beforeEach(function () {
fakeFactory = {
getPoints: function () {
deferred = q.defer();
deferred.resolve({data:
[{"Text":"Some text"}]
});
return deferred.promise;
}
};
getPointsSpy = spyOn(fakeFactory, 'getPoints')
getPointsSpy.andCallThrough();
});
beforeEach(inject(function(_$compile_, _$rootScope_,_$controller_){
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
it('Factory to have been Called', function () {
var element = $compile('<div data-text-enrichment=""> Text </div>')($rootScope)
expect(getPointsSpy.callCount).toBe('1');
});
Update
Following advice from Felipe Skinner I have updated the test with the following
beforeEach(function(){
module(function($provide){
$provide.factory('PointFactory',getPointsSpy)
})
});
However I get the following error:
TypeError: 'undefined' is not a function (evaluating
'pointFactory.getPoints(text)')
You can use the $provide to inject your controller dependencies.
Here's my beforeEach for example:
describe('MyCtrl', function() {
var $controller,
$scope,
$httpBackend,
windowMock,
registerHtmlServiceMock,
mixPanelServiceMock,
toastMock;
beforeEach(function() {
windowMock = { navigator: {} };
registerHtmlServiceMock = {};
mixPanelServiceMock = jasmine.createSpyObj('mixpanel', ['track']);
toastMock = jasmine.createSpyObj('toast', ['error']);
module('myModule');
module(function($provide) {
$provide.value('$window', windowMock);
$provide.value('RegisterHtmlService', registerHtmlServiceMock);
$provide.value('MixPanelService', mixPanelServiceMock);
$provide.value('ToastService', toastMock);
});
inject(function(_$controller_, _$rootScope_, _$httpBackend_) {
$scope = _$rootScope_.$new();
$controller = _$controller_('CourseSelectionCtrl', { $scope: $scope });
$httpBackend = _$httpBackend_;
});
});
// my test cases
});
I haven't tried mocking a function that returns some value. Those two mocks (mixpanel-track and toast-error) are for "void" functions.
UPDATE:
Try changing the previous $provide with this type of injection then.
Change from this:
module(function($provide) {
$provide.value('$window', windowMock);
$provide.value('RegisterHtmlService', registerHtmlServiceMock);
$provide.value('MixPanelService', mixPanelServiceMock);
});
inject(function(_$controller_, _$rootScope_, _$httpBackend_) {
$scope = _$rootScope_.$new();
$controller = _$controller_('CourseSelectionCtrl', { $scope: $scope });
$httpBackend = _$httpBackend_;
});
To this:
beforeEach(inject(function(_$controller_, _$rootScope_, _$httpBackend_) {
mixPanelService = mixPanelServiceMock;
$scope = _$rootScope_.$new();
$controller = _$controller_('MyCtrl', { $scope: $scope, MixPanelService: mixPanelService });
$httpBackend = _$httpBackend_;
}));
The rest of the code should be the same, except for that. Let me know if this works