Javascript 如何断言一个函数是从另一个函数中调用的?
我有一个组件正在与onChange事件进行反应。在下面的代码中,我需要声明在Javascript 如何断言一个函数是从另一个函数中调用的?,javascript,reactjs,unit-testing,jestjs,frontend,Javascript,Reactjs,Unit Testing,Jestjs,Frontend,我有一个组件正在与onChange事件进行反应。在下面的代码中,我需要声明在 this.props.onChangeImage() 在库组件中调用 export class Form extends React.PureComponent { componentDidMount = () => { this.props.getUser(); this.props.getImages(); this.props.getBoards(); } rend
this.props.onChangeImage()
在库组件中调用
export class Form extends React.PureComponent {
componentDidMount = () => {
this.props.getUser();
this.props.getImages();
this.props.getBoards();
}
render() {
if (this.props.pin === null) {
let boards = [];
boards = this.props.boards;
boards = boards.data.map(
(item) => <MenuItem key={item.id.toString()} value={item.name} primaryText={item.name} />
);
return (
<div>
<Helmet
title="Form"
meta={[
{ name: 'description', content: 'Description of Form' },
]}
/>
<Gallery images={this.props.images} onChange={this.props.onChangeImage} />
</div>
);
}
return (<div className="spinner-container"><CircularProgress /></div>);
}
}
我在这里看到了许多答案,虽然这一个似乎最接近,但我还是没有达到目的:
编辑:这是我的测试,我认为这是错误的,因为它将被模拟的函数指定为在更改时直接调用,而实际上它应该调用该函数,而该函数反过来调用模拟。我需要以某种方式调用onImageChange函数,然后验证是否调用了我的spy
import Gallery from '../index';
import * as formIndex from '../../../containers/Form';
describe('<Gallery />', () => {
it('Expect sendMessageToParentWindow to be called on image change', () => {
const sendEventToParentWindowMock = jest.spyOn(formIndex, 'sendEventToParentWindow');
const gallery = shallow(<Gallery images={imagesMockData} onChange={sendEventToParentWindowMock} />);
gallery.find('input#image-1').simulate('change');
expect(sendEventToParentWindowMock).toBeCalled();
});
}
从“../index”导入库;
从“../../../containers/Form”导入*作为formIndex;
描述(“”,()=>{
它('期望在图像更改时调用sendMessageToParentWindow',()=>{
const sendEventToParentWindowMock=jest.spyOn(formIndex,'sendEventToParentWindow');
const gallery=shallow();
gallery.find('input#image-1')。simulate('change');
expect(sendEventToParentWindowMock).toBeCalled();
});
}
正如我在评论中提到的,您可以将模拟函数作为prop传递,其实现将包含对您的sendEventToParentWidow
函数的调用。也就是说,您需要创建两个模拟函数
sendEventToParentWindow
mock函数onChangeImage
mock函数及其实现,其中实现将只包含对sendEventToParentWidow
mock函数的调用describe('<Gallery />', () => {
it('Expect sendMessageToParentWindow to be called on image change', () => {
const sendEventToParentWindowMock = jest.fn();
const onChangeImageMock = jest.fn(() => {
sendEventToParentWindowMock();
});
const gallery = shallow(<Gallery images={imagesMockData} onChange={onChangeImageMock} />); // Passing the mocked onChangeImage as prop
gallery.find('input#image-1').simulate('change');
expect(sendEventToParentWindowMock).toBeCalled();
});
}
描述(“”,()=>{
它('期望在图像更改时调用sendMessageToParentWindow',()=>{
const sendteventtoparentwindowmock=jest.fn();
const onChangeImageMock=jest.fn(()=>{
sendEventToParentWindowMock();
});
const gallery=shallow();//将模拟的onChangeImage作为道具传递
gallery.find('input#image-1')。simulate('change');
expect(sendEventToParentWindowMock).toBeCalled();
});
}
希望有帮助:)你在使用哪个测试库?酶?如果是,那么你是浅层渲染组件吗?@HardikModha,我确实有酶可用,并尝试过浅层渲染和安装组件。我运气不好,但我仍在学习和修补。谢谢提示!你可以通过模拟函数(其实现包含模拟方法调用)当你在浅层渲染组件时作为道具。@HardikModha这似乎成功了。我真的很接近这个问题,但还没有完全解决。谢谢!我正在用一些其他组件构建一些测试,如果有任何其他问题,我会回复。如果一切顺利,我会发布我的答案。再次感谢!很高兴它帮助了你。:)这就是我所缺少的理解!我将在周一回到办公室时尝试一下,但再次感谢您的耐心和示例代码!能够看到一个可靠的示例真的很有帮助。这就是缺少的部分!再次感谢。我已经接受了答案。
describe('<Gallery />', () => {
it('Expect sendMessageToParentWindow to be called on image change', () => {
const sendEventToParentWindowMock = jest.fn();
const onChangeImageMock = jest.fn(() => {
sendEventToParentWindowMock();
});
const gallery = shallow(<Gallery images={imagesMockData} onChange={onChangeImageMock} />); // Passing the mocked onChangeImage as prop
gallery.find('input#image-1').simulate('change');
expect(sendEventToParentWindowMock).toBeCalled();
});
}