0

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");
    });
});

1 Answer 1

1

See updated jsfiddle

A couple points. Your directive should be named creditCardDisplay instead of CreditCardDisplay. See angular guide on naming

.directive('creditCardDisplay', function () {

If you want to use the controllerAs syntax, you should use the latest angular. I think it was only introduced in version 1.3. Your jsfiddle was using angular 1.2.

Althought irrelevant to this test case, note that element.isolateScope is a function so you should be calling element.isolateScope().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.