0

次のjsbinがありますhttp://jsbin.com/uvipos/1/edit

親要素に .active を追加するカスタム ディレクティブを使用すると、含まれるリンクが現在のページへのリンクである場合 (たとえば、ブートストラップ用) に親要素に追加されます。ディレクティブは問題なく機能しますが、テスト方法を理解するのに苦労しています。

ディレクティブ:

  app.directive('whenChildActive', ['$location', function ($location) {
    return {
      link: function postLink(scope, element, attrs) {
        scope.$on( '$routeChangeSuccess', function () {
          var literalLink = element.find('a').attr( 'href' );

          var currentPath = $location.path();
          var hashPath = '#' + currentPath;
          if ( currentPath === literalLink || hashPath === literalLink) {
            element.addClass( 'active' );
          } else {
            element.removeClass( 'active' );
          }
        });
      }
    };
  }]);

私のテストの試み:

describe('Directive: whenChildActive', function () {
  beforeEach(module('myNgApp'));

  var element;
  var envs = [
    'http://server/#/app',
  ], count = 0;

  beforeEach(inject(function($browser){
    $browser.url(envs[count++]);
  }));

  it('should add class when child href matches location', inject(function ($rootScope, $compile) {
    element = angular.element('<li when-child-active><a href="#/app">Some Text</a>/li>');
    element = $compile(element)($rootScope);

    expect(element.hasClass('active')).toBe(true);
  }));
});

テストが失敗しましたが、その理由がわかりません

4

1 に答える 1