Surya Deepak
Surya Deepak

Reputation: 431

Unit testing Angular js Directive with Dependencies

This is my directive

.directive('xDirective', ['x', 'y', function (x, y) {
        return {
            restrict: 'CA',
            link: function (scope, element, attrs, messaging) {
                //console.log(scope);
                //console.log(element);
                //console.log(attrs);

                if (x.isResponsive && x.responsiveMode === y.responsive.mode.phone) {

                    //set the height of the header based on the device height
                    var offset = (attrs.heightOffset ? attrs.heightOffset : 0);                    
                    element.css("height", (x.device.height - offset) + 60 + "px");
                }
            }
        };
    }])

where x and y are the dependencies to the directive..i want to unit test this directive..how do i go ahead using jasmine.

Thanks.

Upvotes: 1

Views: 2520

Answers (1)

Surya Deepak
Surya Deepak

Reputation: 431

I figured it out using the below stackoverflow thread

Testing angularjs directive with dependencies

Below is the code:

describe('Directive: AppVersion', function () {
    beforeEach(module('MyApp'));

    var element;

    it('should have element text set to config value', inject(function ($rootScope, $compile, x,y) {
        var scope = $rootScope;
        element = $compile('<div class="someClass" xDirective >some Content</div>')(scope);
        expect($(element).find('.someClass').css('height')).toBe(config.version);
    }));
});

inside the it block i'm injecting 'x' and 'y' modules which are dependencies to the directive

Thanks

Upvotes: 3

Related Questions