Using Jest, Typescript, React, MobX and I have the following function that I am trying to test in Jest:
(使用Jest,Typescript,React,MobX,我尝试在Jest中测试以下功能:)
search(searchText: string) : Promise<void> {
this.searchInputCache = searchText;
this.searchLoading = true;
const promises = [] as Promise<any>[];
const results = {} as {[itemType in DatabaseItemType]: DatabaseItemRef[]};
for (const [item, checked] of this.filterChecks.entries()) {
if (checked) {
promises.push(SearchService.searchForItem(this._rootStore.databaseStore, searchText, item as DatabaseItemType, SearchTypeFlags.nxSearchType_Name)
.then(values => results[item] = values));
}
}
return Promise.all(promises).then(() => this.populateFilterTreeStore(results));
I've tried testing it with await, but I am trying to test that the inner function is properly called as such:
(我已经尝试过等待测试它,但是我正在尝试测试内部函数是否正确地这样调用:)
it('searches', () => {
filterTreeStore.filterChecks.set(DatabaseItemType.Signal, true);
filterTreeStore.filterChecks.set(DatabaseItemType.Frame, true);
filterTreeStore.filterChecks.set(DatabaseItemType.ECU, false);
filterTreeStore.filterChecks.set(DatabaseItemType.PDU, false);
filterTreeStore.filterChecks.set(DatabaseItemType.Cluster, false);
const searchText = 'a';
filterTreeStore.search('a').then(() => expect(searchMock).toBeCalledWith(databaseStore, searchText, DatabaseItemType.Signal,SearchTypeFlags.nxSearchType_Name))
});
Ultimately I keep getting TypeError: Cannot read property 'then' of undefined no matter what I have tried.
(最终,我不断收到TypeError:无论我尝试了什么,都无法读取未定义的属性'then'。)
79 | for (const [item, checked] of this.filterChecks.entries()) {
80 | if (checked) {
> 81 | return promises.push(SearchService.searchForItem(this._rootStore.databaseStore, searchText, item as DatabaseItemType, SearchTypeFlags.nxSearchType_Name)
| ^
82 | .then(values => results[item] = values));
83 | }
84 | } `
Any suggestion on how to test this functionality is appreciated!
(任何有关如何测试此功能的建议,不胜感激!)
ask by Roxana Karami translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…