Jest - 模拟嵌套函数

时间:2017-08-20 17:10:09

标签: reactjs enzyme jest

我正在为模块编写JEST / Enzyme测试用例。

我在组件中测试的一个功能是保存xml文件。然后从' react-file-download'

调用库函数fileDownload
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = "DatabaseHelper";

    public static final String DATABASE_NAME = "expense.db";
    public static final String TABLE_NAME = "expense_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "AMOUNT";
    public static final String COL_3 = "DATE";
    public static final String COL_4 = "NOTES";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 3);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String amount, String date, String notes) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, amount);
        contentValues.put(COL_3, date);
        contentValues.put(COL_4, notes);
        long result = db.insert(TABLE_NAME, null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }

    public Cursor getData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
        return res;
    }

    public Cursor getItemID(String name){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT " + COL_1 + " FROM " + TABLE_NAME + " WHERE " + COL_2 + " = '" + name + "'";
        Cursor data = db.rawQuery(query, null);
        return data;
    }

    public boolean updateData(String id, String amount, String date, String notes) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1, id);
        contentValues.put(COL_2, amount);
        contentValues.put(COL_3, date);
        contentValues.put(COL_4, notes);
        db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{id});
        return true;
    }

    public Integer deleteData(String id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME, "ID = ?", new String[]{id});
    }


}

当我编写测试用例时,它调用saveContentToXML,然后调用fileDownload。这导致例外。

const saveContentToXML = () => {
    if(this.props.message && this.props.message.details){
        fileDownload(this.props.message.details, this.props.message.title);
    }
}

我的测试用例看起来像

TypeError: window.URL.createObjectURL is not a function

如何测试此功能?

1 个答案:

答案 0 :(得分:1)

你应该模拟react-file-download,然后声明它被调用

// Default mock will just make fileDownload a jest mock function
jest.mock('react-file-download')

import fileDownload from 'react-file-download'

test('Save Content as XML Test', () =>{
    const component = shallow(<Message details={details} />);
    component.instance().saveContentToXML();
    expect(fileDownload).toHaveBeenCalledWith('details', 'title');
    fileDownload.mockClear() // resets the mock function so the call is not used in assertions for other tests
});

关于模拟函数的jest文档是一个很好的资源来引用:https://facebook.github.io/jest/docs/mock-functions.html