mercredi 28 juin 2017

AngularJS testing with RxJS 5 debounceTime

I Have a Component that renders an label and input And on the $postLink I add an Overver to my input focus, and if the focus event is triggered, I set an internal variable focused to true

$postLink() { 
    Rx.Observable.fromEvent(myInputElement, 'focus')
      .debounceTime(200)
      .subscribe(() => {
      this.focused = true;
      this.$scope.$evalAsync();
    });
}

In my test file (I'm using Jasmine), I have something like this:

it('must set "focused" to true when input get focused', () => {
  // The setupTemplate return a angular.element input, my component controller and scope
  const { input, controller, parentScope } = setupTemplate();
  input[0].dispatchEvent(new Event('focus'));
  $timeout.flush(600);
  parentScope.$digest();
  expect(controller.focused).toBe(true);
});

But my test fails.

If I remove the debounceTime from my component method, the test pass.

What can I do to simulate the debounceTime?

Aucun commentaire:

Enregistrer un commentaire