运行npm run watch
时 navList.js 是正确的。
但是当我尝试运行npm test
时,日志只显示setArticleGroup: 1
(setArticleGroup: 2
和setArticleGroup: 3
缺失。)
我意识到getArticlesFromDatabase
在测试中不起作用,为什么会这样呢?
navList.test.js
it('function setArticleGroup() ', function () {
let app = shallow(<NavList sieve="info" />);
});
navList.js
import {getArticlesFromDatabase} from '../csrfData';
setArticleGroup(){
let that = this;
console.log('setArticleGroup: 1 ');
getArticlesFromDatabase.then(function(value) {
console.log('setArticleGroup: 2 ');
...//skip
that.setState({'articleGroup':articleGroup},()=>{
console.log('setArticleGroup: 3 ',articleGroup);
});
});
}
csrfData.js
var getArticlesFromDatabase = new Promise(function(resolve, reject) {
let articles;
axios.get('../api/articles/get-articles-list').then(response=>{
articles = response.data.list;
resolve(articles);
}).catch(function (error) {
console.log(error);
});
});
export {getArticlesFromDatabase,testCsrfData};
答案 0 :(得分:1)
除非getArticlesFromDatabase
是一个then
函数作为属性的对象,否则我说你应该像这样调用它(注意parens):
getArticlesFromDatabase().then(function(value) {