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