我需要测试哪个值将返回闭包。例子:onCell = (index) => (row, rowIndex) => ({ style: { fontWeight: 600 }, ref: rowIndex === 0 && this.cellRef(index) })我希望回报是{ style: { fontWeight: 600 }, ref: null || this.cellRef(index)}
1 回答
![?](http://img1.sycdn.imooc.com/5333a1a90001c8d802000200-100-100.jpg)
慕的地10843
TA贡献1785条经验 获得超8个赞
将对象作为参数与索引一起传递给外部函数,而不是使用this. 然后,您可以将jest.fn()带有适当返回值的a 传递给它,并确保结果是您想要的并且该函数被调用:
onCell = (obj, index) => (row, rowIndex) => ({
style: { fontWeight: 600 },
ref: rowIndex === 0 && obj.cellRef(index)
});
然后在测试中:
const mock = {
cellRef: jest.fn(),
};
mock.cellRef.mockReturnValue(true);
const cb = onCell(mock, 5);
等等等等。
添加回答
举报
0/150
提交
取消