【问题标题】:How test (using NgXs) that an action was dispatched after dispatching another action, using ofActionSuccessful?如何使用 ofActionSuccessful 测试(使用 NgXs)在调度另一个动作后调度一个动作?
【发布时间】:2022-01-11 11:57:40
【问题描述】:

组件逻辑

this.actions$
    .pipe(
       ofActionSuccessful(ProjectLoad),
       switchMap(() => interval(10000))
      )
      .subscribe(() => this.store.dispatch(new ProjecRefresh));

我想测试 ProjecRefresh 在调度 ProjectLoad 之后是否被调度,但我不知道该怎么做

【问题讨论】:

  • 您不能在您要测试的操作旁边添加 console.log 吗?
  • 这样做的目的是什么?
  • 日志可能会显示每个事件被调用的顺序。否则,如果您想进行适当的测试,请在此处寻求完整解决方案之前先尝试一下。这不是免费的编码提供商。
  • 我的问题更多是关于ofActionSuccessful,所以我不知道如何模拟ofActionSuccessful,或者让它在单元测试中工作

标签: angular unit-testing redux ngxs


【解决方案1】:

在我看来,调度 ProjectLoad 应该是状态逻辑的一部分,而不是组件:潜在的多个消费者和复杂的刷新逻辑可能会更快地强制执行此操作。

然后组件代码转换成类似的东西


...
ngOnInit() {
    this.store.dispatch(new ProjectAutoRefreshEnabled())
}

...

ngOnDestroy() {
    // state logic may actually track consumers when this is handled
    // and decide to keep refreshing for other purposes
    // so this is something that shouldn't be in a component IMHO
    this.store.dispatch(new ProjectAutoRefreshDisabled())
}

【讨论】:

  • 感谢您的努力,但我没有如何重构代码...
猜你喜欢
  • 2018-12-07
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 2016-02-02
  • 2017-07-14
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多