在我的 Angular 单元测试中,我想myComponent.items$
在调用 subject 时检查是否有 0 个结果myComponent.clearInput$.next()
。在此之前,我想确保有一些记录。myComponent.items$
可以由 填充myComponent.nameInput$.next("test")
。
不幸的是,两个订阅都是在两个主题都被调用后触发的,所以myComponent.items$
总是 0。
it("when control touched then clear input value", done => {
myComponent.items$.pipe(first()).subscribe(result => {
expect(result.length).toBe(2);
// it's always 0 because of myComponent.clearInput$.next(); from last line
})
myComponent.nameInput$.next("test");
// Now should wait after code from above will finish then the rest should be executed after that.
myComponent.items$.pipe(first()).subscribe(result => {
expect(result.length).toBe(0);
done(); // test should be finished here
})
myComponent.clearInput$.next();
});
这就是那些主题调用 items$ 的方式
this.items$ = merge(
this.nameInput$.pipe(
switchMap((name: string) =>
iif(() => this._partyType === "PT",
this.someService.getByName(name),
this.otherService.getByOtherName(name)
)
)
),
this.clearInput$.pipe(
switchMapTo(of([]))
)
);