我有一个包装aggrid
的组件。周围的组件导入了我的DataHolder
类,该类具有一个名为getVariablesLength()
的功能。但是,每当我进行测试时,都会出现错误消息TypeError: Cannot read property 'getVariablesLength' of undefined
。
这是特定于对ag-grid的反应。我正在使用酶和玩笑进行测试。我已经研究了各种模拟导入的es6模块的方法,但是目前我不确定我是否正确理解了该过程。
//AGVariableGrid.ui.test.js
import AGVariableGrid from 'components/AGVariableGrid.jsx';
import Adapter from 'enzyme-adapter-react-16';
import { AgGridReact } from 'ag-grid-react';
import AgDataHolder from 'components/AgDataHolder.js';
//...
jest.mock(AgDataHolder);
beforeEach((done) => {
console.log(mockDataHandler);
component = mount(<AGVariableGrid/>);
// get agGridReact instance in AGVariableGrid
agGridReact = component.find(AgGridReact).instance();
// ensure grid api has been set and other aggrid specific logic
});
test('all rows selected', () => {
// no rows are selected initially
expect(agGridReact.api.getSelectedRows().length).toEqual(0);
// simulate a user clicking on the select all button
component.find('#selectAll').simulate('click', {
// no actual event data is needed for this particular event/use case
});
expect(agGridReact.api.getSelectedRows().length).toEqual(3);
});
在AGVariableGrid内
// ...
import AgDataHolder from 'components/AgDataHolder.js';
// ...
// in componentDidMount
this.dataHolder = new DataHolder();
const potato = this.dataHolder.getVariablesLength() // error on dataHolder being undefined
// ...
我真的很想知道为什么未定义它,或者如何正确模拟导入的类的属性。