javascript - 使用 RxJS 5 debounceTime 进行 AngularJS 测试

标签 javascript angularjs testing jasmine rxjs

我有一个呈现标签和输入的组件 在 $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

最佳答案

感谢@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);
});

关于javascript - 使用 RxJS 5 debounceTime 进行 AngularJS 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44805419/

相关文章:

javascript - 将类似的 JSON 对象分组到数组中

javascript - 如何跟踪 iframe 内的点击或交互

javascript - 下载 JS 文件并使用 NativeScript 作为后台服务运行

javascript - 如何共享变量并跟踪 Angular v4 ts 中多个组件的变化

spring - 我无法在物化 View 上使用 dbunit 执行测试

ruby-on-rails - 我如何让 Rails 在我运行测试时加载我的测试环境变量?

testing - 使用 cucumber 测试 omniauth 登录时没有哈希错误的方法 uid

javascript - 如何用 node.js 做 AOP?

javascript - 根据文本字段值显示\隐藏选择字段选项

php - 使用 angularjs php mysql 进行实时通知