2

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?

1 Answer 1

2

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').

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

4 Comments

I tried setting scope=element.isolateScope() but that is undefined. Also, element.controller() appears to be undefined as well. Ideas?
I guess it is because of the replace: true. Could you try using false and also try calling isolatedScope() and scope() on the child element of <mcgi-blurb> - it should be the <textarea>? Also, launch Karma in continous mode and try debugging - Batarang or angscope may come in handy.
By the way, it should be .controller('mcgiBlurb')!
I figured out it's a combination of what you suggested and the fact that I was using "templateUrl" in my directive. I changed it from using "templateUrl" to "template" and using isolateScope and it now displays the default value that I'm trying to check. I guess I have to get my karma setup to handle templates.

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.