茉莉花角单位测试等待间谍组件有望解决

发布于 2025-01-20 15:54:33 字数 547 浏览 1 评论 0原文

我们对一个 Angular 组件进行单元测试,该组件的目的是选择和上传文件。 在组件中我们将文件转换为base64字符串(后端要求) 并做一些其他检查。

仅当文件转换为 Base64 后,模板才会更新并显示 待上传附件。

这就是我们想要进行单元测试的内容:要上传的 Base64 编码文件列表的可见性。

以下是我们的 MWE 的链接(为了简洁起见,我们删除了与我们的问题无关的特定逻辑): https://stackblitz.com/edit/angular-jasmine-promise-await-bjvxuc?file=src%2Fapp%2Fapp.component.html

有没有办法让 jasmine 等待我们的 Angular 组件返回已解决的承诺,以便我们确定性地知道模板将重新渲染,以便我们可以预期附件列表已渲染?

We unit-test an Angular component that has the purpose to select and upload files.
In the component we convert files to base64 strings (backend requirement)
and do some other checks.

Only after the files have been converted to base64, the template updates and shows
to-be uploaded attachments.

This is what we want to unit-test: the visibility of a list of base64 encoded files to be uploaded.

Here is a link to our MWE (for brevity we removed specific logic that is not relevant to our question): https://stackblitz.com/edit/angular-jasmine-promise-await-bjvxuc?file=src%2Fapp%2Fapp.component.html

Is there a way to make jasmine wait for our Angular components returned promise to have resolved, such that we deterministically know that the template will have re-rendered, so that we can expect the attachment list to have been rendered?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

つ低調成傷 2025-01-27 15:54:34

您正在寻找的是 Angular 解决方案,很可能不是 jasmine 解决方案。

在 Angular 中有多种等待 Promise 的方法:

1.) asyncawait Fixture.whenStable()

it('should do xyz', async () => {
  // do some stuff that creates promises and puts them in queue

  // wait for pending promises to complete before continuing
  await fixture.whenStable();

  // promises completed, continue with assertions or other stuff
});

2.) waitForAsyncfixture.whenStable()

waitForAsync 辅助方法在完成测试之前等待创建的 Promise 得到解析

it('should do xyz', waitForAsync(() => {
  // do some stuff that creates promises and puts them in queue

  fixture.whenStable().then(() => {
    // do your assertions here when the promises complete
  });
}));

3.) fakeAsynctick()

it('should do xyz', fakeAsync(() => {
  // do some stuff that creates promises and puts them in queue

  // wait for promises to resolve before continuing
  tick();

  // do assertions or other stuff
}));

https://www. digitalocean.com/community/tutorials/angular-testing-async-fakeasync

以上是一篇好文章。您可以继续学习 fakeAsynctickfixture.whenStable()waitForAsync

====编辑=====

it('shows attachments in attachmentslist', fakeAsync(() => {
    // arrange
    // in our test we actually construct and use a mockFileDataTransfer object here
    // and trigger a dispatchEvent(new InputEvent('change'));
    // to better mimic user interaction, we left all of this out for brevity

    // act
    // this is not in our own test, but for demonstration purposes we directly call
    // the method here.
    component.onFilesDropped();
    fixture.detectChanges();

    // add tick() to make the pending promises complete
     tick();
    // add tick(#) to make the timer proceed in a fake way that duration
     tick(250);
    
    // the rest

}));

What you're looking for are Angular solutions and most likely not jasmine solutions.

There are many ways to wait for promises in Angular:

1.) async and await fixture.whenStable()

it('should do xyz', async () => {
  // do some stuff that creates promises and puts them in queue

  // wait for pending promises to complete before continuing
  await fixture.whenStable();

  // promises completed, continue with assertions or other stuff
});

2.) waitForAsync and fixture.whenStable()

waitForAsync helper method waits for promises created to be resolved before completing the test

it('should do xyz', waitForAsync(() => {
  // do some stuff that creates promises and puts them in queue

  fixture.whenStable().then(() => {
    // do your assertions here when the promises complete
  });
}));

3.) fakeAsync and tick()

it('should do xyz', fakeAsync(() => {
  // do some stuff that creates promises and puts them in queue

  // wait for promises to resolve before continuing
  tick();

  // do assertions or other stuff
}));

https://www.digitalocean.com/community/tutorials/angular-testing-async-fakeasync

The above is a good article. You can continue learning about fakeAsync and tick, fixture.whenStable() and waitForAsync.

==== Edit =====

it('shows attachments in attachmentslist', fakeAsync(() => {
    // arrange
    // in our test we actually construct and use a mockFileDataTransfer object here
    // and trigger a dispatchEvent(new InputEvent('change'));
    // to better mimic user interaction, we left all of this out for brevity

    // act
    // this is not in our own test, but for demonstration purposes we directly call
    // the method here.
    component.onFilesDropped();
    fixture.detectChanges();

    // add tick() to make the pending promises complete
     tick();
    // add tick(#) to make the timer proceed in a fake way that duration
     tick(250);
    
    // the rest

}));

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