为什么我的反应单元测试导入不起作用?

时间:2018-02-02 08:56:45

标签: javascript reactjs unit-testing

运行npm run watch navList.js 是正确的。

但是当我尝试运行npm test时,日志只显示setArticleGroup: 1setArticleGroup: 2setArticleGroup: 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};

1 个答案:

答案 0 :(得分:1)

除非getArticlesFromDatabase是一个then函数作为属性的对象,否则我说你应该像这样调用它(注意parens):

getArticlesFromDatabase().then(function(value) {