RxJS debounceTime(0)导致单元测试失败,求修复方案
问题:添加debounceTime(0)后UI正常但单元测试失败
我给dimensions$流添加了debounceTime(0),因为不加这行的话UI无法正确反映数据变化;另外代码里combineLatest([this.resize$])其实可以简化成直接使用this.resize$,不过当前核心问题是:加了debounceTime(0)后,原本正常的单元测试开始失败了。
相关代码如下:
get nativeWindow(): Window { return window; } private resize$ = fromEvent(this.nativeWindow, 'resize').pipe( debounceTime(50), map(event => event.target as Window), startWith(this.nativeWindow), untilDestroyed(this) ); protected dimensions$: Observable<{ width: number; height: number }> = combineLatest([this.resize$]).pipe( withLatestFrom(this.elementRef$), debounceTime(0), map(([, ref]) => ({ width: ref.nativeElement.offsetWidth, height: ref.nativeElement.offsetHeight })), filter(dim => dim.width > 0) );
原本正常的单元测试示例(使用@ngneat/spectator):
it('should display cropped image', () => { host.setHostInput({ assetCrop: { type: 'CROP', x: 100, y: 100, width: 200, height: 200 } }); expect(crop?.backgroundPosition).toBe('-100px -100px'); expect(crop?.backgroundSize).toBe('1000px 1000px'); });
我已经尝试过使用fakeAsync、tick以及whenStable来解决,但都没有效果。
内容的提问来源于stack exchange,提问作者Paweł Ostromecki
相关产品推荐
相关产品推荐

