How to force an input change trigger with ng-model-options debounce in AngularJS?

I am testing my library. The problem is I have debounce on the textbox to avoid frequent updates.

how

<input ... ng-model-options="{debounce: {'default': 500, 'blur': 0} }"

      

But I cannot turn it off in tests, even I tried to call blur

it("""test input set with debounce""", function(){
    scope.obj = {
        name: 'John'
    }
    el = compileTemplate("<span><input ng-model=\"obj.name\" ng-model-options=\"{debounce: {'default': 500, 'blur': 0} }\"></input></span>")
    scope.$digest()
    input = el.find('input');
    expect(input.val()).toEqual('John');
    angular.element(input).val('Max').trigger('change').trigger('blur')
    scope.$apply()
    expect(scope.obj.name).toEqual('Max');
})

      

This will fail because I need to add $ timeout. So 10 tests = 5 seconds delay, which is inappropriate.

How can I make the trigger change

avoid debounce or trigger blur

?

+3


source to share


2 answers


The spoofed service $timeout

has a method flush

that can be used to trigger an update in unit tests.



it('should set with debounce', inject(function ($timeout) {
    input.val('Max').triggerHandler('input');
    $timeout.flush();
    expect(scope.obj.name).toEqual('Max');
}));

      

+2


source


It looks like using $timeout

even without delay

does the trick:



$timeout(function(){
    expect(scope.obj.name).toEqual('Max');
})

      

+1


source







All Articles