【问题标题】:AngularJS testing with RxJS 5 debounceTimeAngularJS 测试与 RxJS 5 debounceTime
【发布时间】:2017-06-28 14:28:54
【问题描述】:

我有一个渲染标签和输入的组件 在 $postLink 上,我为输入焦点添加了 Overver,如果触发了焦点事件,我将内部变量 focused 设置为 true

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

在我的测试文件(我正在使用 Jasmine)中,我有这样的内容:

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);
});

但是我的测试失败了。

如果我从组件方法中删除debounceTime,则测试通过。

如何模拟debounceTime

【问题讨论】:

  • 测试是异步的,所以你可以使用it('...', done => {}),然后手动调用done()
  • @martin THAAANKS!!!,就是这样 =DD

标签: javascript angularjs testing jasmine rxjs


【解决方案1】:

感谢@martin,我的测试是绿色的。

我刚刚在回调中添加了一个 done,并在 setTimeout

中调用
it('must set "focused" to true when input get focused', (done) => {
  const { input, controller, parentScope } = setupTemplate();
  input[0].dispatchEvent(new Event('focus'));
  setTimeout(() => {
    expect(controller.focused).toBe(true);
    done();
  }, 300);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2019-03-19
    • 2017-05-29
    • 1970-01-01
    • 2019-07-12
    相关资源
    最近更新 更多