【发布时间】:2018-02-22 14:27:28
【问题描述】:
我在我的组件中使用 NgbTypeAhead,其中回调必须是 (text: Observable<string>) => Observable<any[]> 。现在,我想测试搜索的副作用,但我正在努力用 jasmine 编写该测试。
有人可以帮忙吗?提前致谢。
component.ts
search = (text$: Observable<string>) =>
text$
.debounceTime(200)
.distinctUntilChanged()
.do((term: string) => {
console.log("do 1", term)
this.isSearching = true && term !== "";
this.cd.markForCheck();
})
.map(term => {
console.log("map", term)
return term === ""
? this.list
: this.filter(term)
})
.do(list => {
console.log("do 2", list)
this.searchResultsCount = list.length;
this.cd.markForCheck();
});
component.html
<input #initiativeSearch type="text" id="initiativeSearch"
[ngbTypeahead]="search"/>
component.spec.ts
it("should update searching flags", () => {
// how do I test this ? here is what I tried
let spyObj = jasmine.createSpyObj<Observable<string>>("text$", ["debounceTime", "distinctUntilChanged", "do", "map"]);
spyObj.debounceTime.and.returnValue(spyObj)
spyObj.distinctUntilChanged.and.returnValue(spyObj)
spyObj.do.and.returnValues(Observable.of("blabla"), Observable.of([]))
spyObj.map.and.returnValue(Observable.of([]))
component.searchInitiatives(spyObj);
expect(spyObj.debounceTime).toHaveBeenCalledWith(200);
expect(spyObj.distinctUntilChanged).toHaveBeenCalledTimes(1);
expect(spyObj.do).toHaveBeenCalledTimes(2);
});
这会在控制台中记录spy text$.do to have been called 2 times. It was called 1 times.,并且不会显示任何console.log
【问题讨论】:
-
我不这么认为,我的函数是一个回调,它本身不返回任何值,
标签: angular jasmine rxjs ng-bootstrap