来自()的rxjs note note concatmap()内执行

发布于 2025-01-30 11:33:42 字数 1005 浏览 3 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼窥荷 2025-02-06 11:33:42

因此,使用剪贴板时,您需要在执行剪贴板事件之前给予窗口焦点。

这解决了问题(虽然它在自动重新加载似乎都不起作用):

beforeAll(() => window.focus());

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):

beforeAll(() => window.focus());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文