I am trying to unit test angular directive which has an external template. Sample code is given below.
var vsLogin = angular.module('vs-login', []);
vsLogin.directive('vsLogin', function (vsLoginService, dataService, applicationService, $state, viewService, vsModaldialogService, utilsService) {
return {
restrict: 'E',
replace: true,
scope: {
templateurl: "@",
loginDomain: "=?",
loginid: "=?",
loggedInUser: "=?"
},
controller: ['$scope', function ($scope) {
$scope.errorMsg = '';
$scope.status = {};
applicationService.clearLocalCache();
$scope.login = function (domain, loginid, password) {
if ( loginid === undefined || password === undefined || loginid.trim() === ''|| password.trim() === '') {
$scope.errorMsg = 'The login ID or password entered is invalid. Please try again.';
}
// verify the form is valid
if (!utilsService.isFormValid('merchant-login-form')) {
return;
}
var promise = vsLoginService.login(domain, loginid, password);
promise.then(function (res) {
// success
}
, function (res) {
// fail
});
}
}],
templateUrl: function (element, attrs) {
if (attrs.templateurl) {
return attrs.templateurl;
}
return viewService.getWidgetTemplateUrl("Login");
}
}
})
The test code using Jasmine + Chutz-pah is as follows.
/// <template path="./Login.cshtml" />
/// <reference path="./jquery-2.1.4.js" />
/// <chutzpah_reference path="./angular.min.js" />
/// <chutzpah_reference path="./angular-mock.js" />
/// <chutzpah_reference path="./jasmine.js" />
/// <chutzpah_reference path="./jasmine-html.js" />
/// <chutzpah_reference path="./blanket.js" />
/// <chutzpah_reference path="./Application.js" />
/// <chutzpah_reference path="./DataService.js" />
/// <chutzpah_reference path="./LoginService.js" />
/// <chutzpah_reference path="./ModalDialogService.js" />
/// <chutzpah_reference path="./uiFormValidation.js" />
/// <chutzpah_reference path="./utilsService.js" />
/// <chutzpah_reference path="./ViewService.js" />
/// <chutzpah_reference path="./stateMock.js" />
/// <chutzpah_reference path="./Login.js" />
describe("LoginTest with loginid and password", function () {
var $rootScope, $compile, element, scope, state, controller;
angular.module("vs-login", ["vsLogin"]);
beforeEach(function () {
module('stateMock');
module('uiFormValidation.services');
module(function ($provide) {
$provide.value('vsLoginService', vsLoginService);
$provide.value('dataService', dataService);
$provide.value('applicationService', applicationService);
$provide.value('viewService', viewService);
$provide.value('vsModaldialogService', vsModaldialogService);
});
inject(function ($injector, $rootScope, $controller, $httpBackend, $templateCache) {
var template = jQuery(".merch-login-form")[0].outerHTML;
$templateCache.put("/Login.cshtml", template);
$compile = $injector.get('$compile');
scope = $rootScope.$new();
element = angular.element("<div ng-app='appname'><vs-login loginDomain='somedomain' loginid='someuser'></vs-login></div>");
element = $compile(element)(scope);
scope.$digest();
controller = element.controller;
var html = element.html();
var directiveScope = element.isolateScope();
});
});
The issue here is the isolateScope always coming undefined and not able to test the method inside scope [i.e. scope.login method] .
Please suggest.