Ui-grid testing wrapped in another directive

I am trying to create a directive that will allow me to set certain configurations in the ui-grid and also add some functionality to it. I have something basic, but the problem is that the jasmine test is hard for me.

The JS code looks like this:

angular.module('myGridDirective', ['ui.grid'])

.directive('myGrid', function() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: 'templates/my-grid.html'
    };
});

      

The template looks like this:

<div><div id="test-id" class="gridStyle" ui-grid="gridOptions"></div></div>

      

And the test looks like this:

describe('my grid directive', function() {
    var $compile, 
        $rootScope;

    beforeEach(module('myGridDirective'));

    beforeEach(module('templates/my-grid.html'));

    beforeEach(function() {
        inject(function(_$compile_, _$rootScope_) {
            $compile = _$compile_;
            $rootScope = _$rootScope_;
        });
    });

    it('Replaces the element with an ui-grid directive element', function() {
        var element = $compile("<my-grid></my-grid>")($rootScope);

        $rootScope.$digest();

        expect(element.html()).toEqual('<div id="test-id" class="gridStyle" ui-grid="gridOptions"></div>');
    });
});

      

The problem is, while the directive works (i.e. using <my-grid></my-grid>

anywhere in my html file works), the test fails. I am getting the error:

TypeError: $scope.uiGrid is undefined in .../angular-ui-grid/ui-grid.js (line 2879)

      

Relevant line in ui-grid.js (first line is 2879):

if (angular.isString($scope.uiGrid.data)) {
        dataWatchCollectionDereg = $scope.$parent.$watchCollection($scope.uiGrid.data, dataWatchFunction);
      }
      else {
        dataWatchCollectionDereg = $scope.$parent.$watchCollection(function() { return $scope.uiGrid.data; }, dataWatchFunction);
      }

      

The point is, if I replace the array ['ui.grid] in the directive module creation with an empty array, the test will pass. The only problem is that if I do this, I have to include "ui.grid" wherever the directive is used, or else the directive stops working, which I cannot do.

I've already tried translating, but it doesn't seem to help, let alone the directive itself works, so it's logical not to just do it just for the test.

Any thoughts?

+3


source to share


1 answer


Ok I figured out the solution.

First found one way to solve this problem:

  • Initialize gridOptions variable with some data so that ui-grid is built


However, once I got this to work, I tried to add "expect" statements when it hit me, now I have a lot of third party html to test, which I don't want. The final solution was to decide to mock the internal directive (which needs to be tested elsewhere) and use the html mock instead.

Since ngMock does not support directives, I found this great article explaining how to mock directives with $ compileProvider , which completely solved my problem.

+2


source







All Articles