来自()的rxjs note note concatmap()内执行
我正在尝试使用Jasmine进行测试,以单击将值复制到剪贴板的按钮。然后,我尝试阅读保存在剪贴板中的值。
我不确定我在这里做错了什么,但是我在控制台中获得的输出是:
timer 1
我希望在控制台中看到的是:
timer 1
read
timer 2
我使用的茉莉代码看起来像这样:
it('should copy input value', () => {
const click = component.clickButton('buttonElRef');
click.subscribe(value => {
console.log('v', value);
});
});
角度测试组件看起来像这样:
class CopyToClipboardTest {
clickButton(which: 'buttonObjRef' | 'buttonElRef'): Observable<string> {
this[which].nativeElement.click();
return timer(100).pipe(
tap(() => console.log('timer 1')),
concatMap(() => this.getClipboard()),
tap(() => console.log('timer 2')),
);
}
getClipboard(): Observable<string> {
return from(navigator.clipboard.readText()).pipe(tap(() => console.log('read')));
}
}
I am trying to run a test with Jasmine to click a button which copies a value to the clipboard. I'm then trying to read the value that was saved in the clipboard.
I am not sure what I am doing wrong here, but the output that I am getting in the console is this:
timer 1
What I am expecting to see in the console is this:
timer 1
read
timer 2
The Jasmine code that I am using looks like this:
it('should copy input value', () => {
const click = component.clickButton('buttonElRef');
click.subscribe(value => {
console.log('v', value);
});
});
The Angular test component looks like this:
class CopyToClipboardTest {
clickButton(which: 'buttonObjRef' | 'buttonElRef'): Observable<string> {
this[which].nativeElement.click();
return timer(100).pipe(
tap(() => console.log('timer 1')),
concatMap(() => this.getClipboard()),
tap(() => console.log('timer 2')),
);
}
getClipboard(): Observable<string> {
return from(navigator.clipboard.readText()).pipe(tap(() => console.log('read')));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,使用剪贴板时,您需要在执行剪贴板事件之前给予窗口焦点。
这解决了问题(虽然它在自动重新加载似乎都不起作用):
So, when using the clipboard you need to give the window focus before executing clipboard events.
This fixes the issue (it doesn't seem to work on auto reload though):