mercredi 28 octobre 2015

Angular directive testing scope.$$childHead

I'm trying to write some tests to cover an angular directive which has no controller specified and simply uses link as below:

directive code:

(function () {

    'use strict';

    angular
        .module('pages.projections')
        .directive('spPlanProjectionDateFilter', spPlanProjectionDateFilter);

    spPlanProjectionDateFilter.$inject = ['projectionsModel',
        'moment',
        'projectionsService',
        '_'];

    function spPlanProjectionDateFilter(projectionsModel,
                                        moment,
                                        projectionsService,
                                        _) {

        var directive = {
            restrict: 'E',
            link: linker,
            templateUrl: 'app/pages/projections/sp-plan-projection-date-filter.html',
            scope:{}
        };
        return directive;

        function linker(scope, elem) {

            scope.dateFrom = moment();

In my tests I want to chect the dateFrom is equal to today or a different date after some action....

test code:

describe.only('spPlanProjectionDateFilter', function () {
    'use strict';
    var $compile, $rootScope;
    var element = '<sp-plan-projection-date-filter></sp-plan-projection-date-filter>';
    var scope;

    beforeEach(module('pages.projections'));
    beforeEach(module('app.services'));
    beforeEach(inject(function (_$compile_, _$rootScope_, _$controller_) {
        $compile = _$compile_;
        $rootScope = _$rootScope_;
        scope = $rootScope.$new();
    }));

    describe('when initialised', function () {
        it('should set dateFrom to today', function(done) {
            var compileDirective = $compile(element)(scope);

            scope.$digest();
            console.log(compileDirective);
            expect(scope.dateFrom).equals('ddd');
        });
    });
});

Why do I have to drill all the way down to scope.$$childHead to see the properties and functions I have hanging off my linker? I.e. when I am in console and I interrogate scope variable I see no properties & functions and I only see them when I expand the $$childScope object in my scope variable. I thought I would be able to assert on my "scope" variable that I have in the test? Is there a cleaner way to test this directive?

Cheers

Aucun commentaire:

Enregistrer un commentaire