I've followed a number of guides for unit testing a (simple) custom directive, but am puzzled by the fact that the directive seemingly doesn't get executed (Directive controller never fires). I am calling scope.$digest to execute a digest cycle.
Fiddle :
//--- CODE --------------------------
angular.module("foo", [])
.directive('CreditCardDisplay', function () {
return {
restrict: 'E',
scope: {
card: '@'
},
controllerAs: 'vm',
bindToController: true,
controller: function () {
var mask = null;
console.log("in CreditCardDisplay, card: " + this.card);
if (!_.isEmpty(this.card)) {
var length = this.card.length;
var card = this.card.slice(-4);
switch (length) {
case 14:
mask = "****-******-";
break;
case 15:
mask = "****-******-*";
break;
case 16:
mask = "****-****-****-";
break;
default:
var l1 = Math.floor((length - 4) / 4);
var l2 = length - 4 - (l1 * 4);
mask = _.repeat("****-", l1) + _.repeat("*", l2) + "-";
}
}
this.mask = mask ? mask + card : card;
},
template: '<span>{{vm.mask}}</span>'
};
});
// ---SPECS-------------------------
describe("directive: gpCreditCardDisplay", function () {
var element, scope, innerScope, elementCtrl;
beforeEach(function () {
module('foo');
element = angular.element('<credit-card-display card="12345678901234"/>');
inject(function($rootScope, $compile) {
scope = $rootScope.$new();
$compile(element)(scope);
scope.$digest();
innerScope = element.isolateScope;
elementCtrl = innerScope.vm;
});
});
// credit card
it("when getting a 14 digit credit card number should return correctly formatted", function () {
expect(element.text()).toContain("****-******-1234");
});
});