Jasmine - TypeError: undefined не является конструктором - PullRequest
0 голосов
/ 07 мая 2018

У меня есть директива для отображения всплывающей подсказки, например:

angular.module('cui.directives.cuiEllipsisTooltip', [])
  .directive('cuiEllipsisTooltip', ['$compile', '$timeout',
    function($compile, $timeout) {
      'use strict';

      return {
        restrict: 'A',
        scope: true,
        link: function(scope, element, attrs) {
          var raw = element[0];
          var hasTooltip = false;
          var leaveEventPromise = null;

          element.on('mouseenter', function() {
            if (raw.offsetWidth < raw.scrollWidth) {
              if (hasTooltip) {
                scope.enabled = true;
              } else {
                scope.tooltip = element.text();
                scope.enabled = true;

                element.attr('data-toggle', 'tooltip')
                  .attr('data-original-title', scope.tooltip)
                  .removeAttr('cui-ellipsis-tooltip');

                $compile(element)(scope);

                hasTooltip = true;

                element.tooltip('show');

                $timeout(function() {
                  element.trigger('mouseenter');
                });
              }

              leaveEventPromise = null;
            } else {
              scope.enabled = false;
            }
          });

          element.on('mouseleave', function() {
            element.tooltip('hide');
            if (leaveEventPromise === null) {
              leaveEventPromise = $timeout(function() {
                element.trigger('mouseleave');
              }, 50);
            }
          });

          scope.$watch(function() {
            return element.text();
          }, function(newVal, oldVal) {
            if (newVal !== oldVal) {
              scope.tooltip = newVal;
            }
          });
        }
      };
    }
  ]);

и я пишу модульный тест для проверки директивы, как показано ниже:

it('should show tooltip when the text is truncated', function() {
  // Given
  var text = 'testestestestestestestestestest';
  element = angular.element('<div cui-ellipsis-tooltip style="width: 30px;">' + text + '</div>');
  element.appendTo(document.body);
  $compile(element)($scope);
  $scope.$digest();

  // When
  element.trigger('mouseenter');
  // $timeout.flush();

  // Then
  var tooltip = element.next();
  expect(tooltip.length).toBe(1);
  expect(tooltip.hasClass('tooltip')).toBe(true);
  expect(tooltip.text().trim()).toEqual(text);
});

Это дает мне ошибку

TypeError: undefined is not a constructor (evaluating 'element.tooltip('show')').

Я выполняю поиск в StackOverflow и обнаружил, что эта ошибка возникает при попытке вызвать функцию, которая не определена. Итак, этот function tooltip('show') из JQuery не распознается Карма-Жасмин. Что мне делать?

...