私は単純にぼかしの入力をクリアする次の関数を持っています。私は角度のある素材を使用しています。私はそれが起こりたい時に追加する指示を作成しています。ジャスミンを使用してネストされたタイムアウトを使用してJavaScript関数をテストするには
function clearTextOnBlurLink(scope, element, attrs, controller) {
$timeout(function() {
var input = element.find('input');
input.on('blur', function(e){
setTimeout(function(){
input.val('');
input.triggerHandler('input');
}, 100);
});
},0);
}
私はそれをテストしたいが、それを動作させることはできない。
は、ここに私のテストです:
beforeEach(inject(function(_$rootScope_, _$compile_, _$timeout_) {
$scope = _$rootScope_.$new();
$compile = _$compile_;
$timeout = _$timeout_;
$scope.searchText = 'blah';
element = angular.element('\
<div mx-clear-on-blur> <input ng-model="searchText1"> </div>\
');
element = $compile(element)($scope);
$scope.$apply();
$timeout.flush();
}));
it('clears text on blur', function() {
var input = element.find('input').eq(0);
expect(input.val()).toBe('blah');
expect($scope.searchText).toBe('blah');
input1.triggerHandler('blur');
expect(input.val()).toBe('');
expect($scope.searchText).toBe('');
});
[OK]を、だから私はそれが$タイムアウトへのsetTimeoutを変える動作するようになりました。 2回のタイムアウトがなければ、それほど厄介な方法はありませんか?
:
ジャスミンもwaitsForメソッドを持っている必要がありますコードが非同期になりました。 onValueChangeフックで入力要素をモックしようとするかもしれません。または、関数内でsetTimeoutの代わりに$ timeoutを使用するだけです。 – Nosyara