繁体   English   中英

Angular 指令中的单元测试 $formatters

[英]Unit test $formatters in Angular directive

我有一个简单的验证器,可以根据正则表达式检查输入:

.directive('test', function () {
return {
    restrict: 'A',
    require: 'ngModel',
    link: function ($scope, element, attrs, ctrl) {

        ctrl.$setValidity('namePattern', true);

        function checkValid(name) {
            console.log('checkValid executed');
            if (!name || ctrl.$pristine) {
                ctrl.$setValidity('namePattern', true);
                return name;
            }
            var test = /^[A-Za-z0-9_-]+$/;
            ctrl.$setValidity('namePattern', test.test(name));
            return name;
        }

        // called when value changes via code/controller
        ctrl.$formatters.unshift(checkValid);
        // called when value changes in input element
        ctrl.$parsers.unshift(checkValid);
    }
  };
});

活生生的例子

我想对该指令进行单元测试,并具有以下内容:

function initState() {
  angular.mock.module('app');
  angular.mock.inject(function($compile, $rootScope, $timeout){
    $scope = $rootScope.$new();

    $rootScope.safeApply = function(cb) {
      $timeout(function() {
        $scope.$digest();
      });
    };

    $scope.model = {
      instanceName: ''
    };

    var tmpl = angular.element('<form name="form">' +
      '<input ng-model="model.instanceName" name="instanceName" test>' +
      '</form>'
    );

    element = $compile(tmpl)($scope);
    $scope.$apply();
    form = $scope.form;
  });
}
beforeEach(initState);

但是,对模型的更改不会触发checkValid 我试过直接在模型上设置一个属性:

it('should trigger a change resulting in an invalid state', function () {
  $scope.model.instanceName = 'this is an invalid name';
  $scope.$digest();
  expect(form.instanceName.$valid).to.be.false;
});

以及$modelValue

it('should trigger a change resulting in an invalid state', function () {
  form.instanceName.$modelValue = 'this is an invalid name';
  $scope.$digest();
  expect(form.instanceName.$valid).to.be.false;
});

我也尝试通过element.triggerHandler触发input事件。

如何触发模型更改以便checkValid通过$formatters运行?

(这是使用 Angular 1.2.23)

看起来您的指令将输入设置为有效,如果它是原始的。 在您编写的单元测试的情况下,它始终是原始的,因为用户还没有与之交互。

如果您希望指令继续现在的状态,那么在测试测试中,您可以强制输入不是原始的:(我在这里使用 Jasmine)

it('should trigger a change resulting in an invalid state', function () {
  $scope.model.instanceName = 'this is an invalid name';
  form.instanceName.$pristine = false;
  $scope.$digest();
  expect(form.instanceName.$valid).toEqual(false);
});

这可以在http://plnkr.co/edit/TEd91POjw9odNHSb6CQP?p=preview上看到

由于您的指令明确假定输入对于处于原始状态的输入的模型更改有效,因此实际上我认为更有价值的测试是明确测试:

it('should ignore invalid values on a pristine input', function () {
  $scope.model.instanceName = 'this is an invalid name';
  form.instanceName.$setPristine();
  $scope.$digest();
  expect(form.instanceName.$valid).toEqual(true);
});

通过引用表单名称和输入名称来使用$setViewValue

$scope.form.instanceName.$setViewValue('hello');

然后断言。

expect($scope.model.instanceName).toEqual('something');
expect($scope.form.instanceName.$valid).toBe(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM