admin管理员组文章数量:1208155
I have a component in React with an onChange event. In the code below, I need to assert that the correct method is called when
this.props.onChangeImage()
is invoked in the Gallery component.
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>);
}
}
Below, in the onChangeImage method, I am trying to assert that the sendEventToParentWindow method is called.
function mapDispatchToProps(dispatch) {
return {
onChangeImage: (event) => {
dispatch(createPinImage(event.target.value));
sendEventToParentWindow({
action: 'change-image',
description: 'Change image',
});
},
};
}
function sendEventToParentWindow(message) {
window.postMessage(message, window.location.href);
}
export default connect(mapStateToProps, mapDispatchToProps)(Form);
I've looked at a number of answers here, and while this one seemed closest, it's not quite getting me there: Jest - mocking a function call
EDIT: Here is my test which I believe is wrong because it's assigning the mocked function to be called directly onChange when it really should be calling the function that in turn calls the mock. I need somehow to invoke the onImageChange function and then verify that my spy was called.
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();
});
}
I have a component in React with an onChange event. In the code below, I need to assert that the correct method is called when
this.props.onChangeImage()
is invoked in the Gallery component.
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>);
}
}
Below, in the onChangeImage method, I am trying to assert that the sendEventToParentWindow method is called.
function mapDispatchToProps(dispatch) {
return {
onChangeImage: (event) => {
dispatch(createPinImage(event.target.value));
sendEventToParentWindow({
action: 'change-image',
description: 'Change image',
});
},
};
}
function sendEventToParentWindow(message) {
window.postMessage(message, window.location.href);
}
export default connect(mapStateToProps, mapDispatchToProps)(Form);
I've looked at a number of answers here, and while this one seemed closest, it's not quite getting me there: Jest - mocking a function call
EDIT: Here is my test which I believe is wrong because it's assigning the mocked function to be called directly onChange when it really should be calling the function that in turn calls the mock. I need somehow to invoke the onImageChange function and then verify that my spy was called.
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();
});
}
Share
Improve this question
edited Aug 11, 2020 at 12:48
Flip
6,7618 gold badges50 silver badges83 bronze badges
asked Jul 14, 2017 at 12:23
blueyodelerblueyodeler
1331 gold badge2 silver badges7 bronze badges
6
- Which testing library are you using? Enzyme? If yes then are you shallow rendering the component? – Hardik Modha Commented Jul 14, 2017 at 13:11
- @HardikModha, I do have Enzyme available to me and have tried shallow rendering and mounting the component. I didn't have any luck, but I am still learning and tinkering. Thanks for the tip! – blueyodeler Commented Jul 14, 2017 at 13:17
- 1 You can pass the mocked function (whose implementation contains a mock method call) as prop when you are shallow rendering the component. – Hardik Modha Commented Jul 14, 2017 at 13:19
- @HardikModha That seems to have done the trick. I was really close to the problem but just hadn't quite made it. Thank you! I'm build some tests with some other components and will repot back if I have any other questions. If all goes well, I'll post my answer. Thanks again! – blueyodeler Commented Jul 14, 2017 at 13:47
- Glad it helped you. :) – Hardik Modha Commented Jul 14, 2017 at 13:57
1 Answer
Reset to default 17As I mentioned in the comment, You can pass a mocked function as prop whose implementation will contain a call to your sendEventToParentWindow
function. i.e. You'll need to create two mocked function.
sendEventToParentWindow
mock function.onChangeImage
mock function with implementation, where implementation will only contain call to yoursendEventToParentWindow
mock function.
So the test will look something like this,
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();
});
}
Hope it helps :)
本文标签: javascriptHow to assert a function is called from within another functionStack Overflow
版权声明:本文标题:javascript - How to assert a function is called from within another function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738725053a2108984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论