我有一个阵营JS以下情况。我问过类似的问题,并试图以同样的方法适用于本:
className={'custom-grid-buttons tran-button enrollment-button'}
onClick={() => {this.props.openBatchUpdateLOAModal()}}
>
Batch Update
</button>
<button
className={'custom-grid-buttons tran-button enrollment-button'}
onClick={() => {this.props.getSelectedLOAs().then(() => {
this.props.selectedLOAs && this.props.selectedLOAs.length > 0 ? this.props.openDownloadLOAModal() : alert('Please select at least one LOA.')})}}
>
Download By Custodian
</button>
得到以下错误:方法“模拟”是指被1个节点上运行。 0找到代替。我张贴这里的大部分测试文件,但我认为主要的错误就是从这一行来:
wrapper.find(".custom-grid-buttons tran-button enrollment-button").simulate("click");
优点都被设置:
// jest mock functions (mocks this.props.func)
const setFromStatusList = jest.fn();
const openBatchUpdateLOAModal = jest.fn();
const getSelectedLOAs = jest.fn();
const getDynamicRender = jest.fn();
const openDownloadLOAModal = jest.fn();
const onClick = jest.fn();
// defining this.props
const baseProps = {
location: {
pathname:[],
},
services :{
Counterparty :{
URL : "TEST URL",
subscription_key: "test key",
},
},
setFromStatusList,
openBatchUpdateLOAModal,
getSelectedLOAs,
backgroundapp:{},
getDynamicRender,
openDownloadLOAModal,
onClick,
selectedLOAS:[],
}
beforeEach(() => wrapper = shallow(<BrowserRouter><LOA {...baseProps} /></BrowserRouter>));
it("should call openBatchUpdateLOAModal click", () => {
// Reset info from possible previous calls of these mock functions:
baseProps.openBatchUpdateLOAModal.mockClear();
baseProps.getSelectedLOAs.mockClear();
wrapper.setProps({
selectedLOAS: null
});
// Find the button and call the onClick handler
wrapper.find(".custom-grid-buttons tran-button enrollment-button").simulate("click");
// Test to make sure prop functions were called via simulating the button click
expect(baseProps.openBatchUpdateLOAModal).toHaveBeenCalled();
expect(baseProps.getSelectedLOAs).toHaveBeenCalled();
很可能只是缺少在其他类名的前面.
:
wrapper.find(".custom-grid-buttons .tran-button .enrollment-button").simulate("click");
虽然这可能可以简化为:
wrapper.find(".enrollment-button").simulate("click");
除非你有你的页面上有多个按键招生。