_viewCompany(companyId) {
this.context.router.push(`/admin/companies/${companyId}`);
}
It worked well in the component, but I encountered an error while writing tests for the component .This is the test that caused the problem (I use enzyme, jest, sinon):
it('should call _viewCompany', () => {
jest. spyOn(CompaniesList.prototype,'_viewCompany');
wrapper = mount();
const viewButton = wrapper.find('.btn-info'). at(0);
viewButton.simulate('click');
expect(CompaniesList.prototype._viewCompany).toHaveBeenCalled();
});
This The test returns an error, stating the following:
Undefined property’push’ cannot be read
What can I do to simulate it or create an empty function for the test?
function Router () {
this.router = [];
this.push = function(a) {
this.router.push( a);
};
this.get = function(index){
return this.router[index];
}
this.length = function(){
return this.router.length;
}
}
function History () {
this.history = [];
this.push = function(a) {
this.history.push(a);
};
this.get = function( index){
return this.history[index];
}
this.length = function(){
return this.history.length;
}
}
it('should call _viewCompany', () => {
jest.spyOn(CompaniesList.prototype,'_viewCompany');
wrapper = mount(< CompaniesList {...props}/>,{context:{router: new
Router(), history: new History()}});
co nst viewButton = wrapper.find('.btn-info').at(0);
viewButton.simulate('click');
expect(CompaniesList.prototype._viewCompany).toHaveBeenCalled();
expect(wrapper.context().router.length()).toEqual('1');
});
You can even test the context object. Just like me Do it like above.
I have a component that uses React Router, as shown below:
_viewCompany(companyId) {
this.context.router.push(`/admin/companies/${companyId}`);
}
It works well in the component, but I encountered an error while writing a test for the component. This is the test that caused the problem (I use enzyme, jest, sinon):
it('should call _viewCompany', () => {
jest.spyOn(CompaniesList.prototype,'_viewCompany');
wrapper = mount();
const viewButton = wrapper.find ('.btn-info').at(0);
viewButton.simulate('click');
expect(CompaniesList.prototype._viewCompany).toHaveBeenCalled();
}) ;
This test returns an error, stating the following:
Undefined property’push’ cannot be read
What can I do to simulate it or for testing Create an empty function?
You need to pass the context object when installing the component.
function Router () {
this.router = [];
this.push = function(a) {
this.router.push(a);
};
this.get = function(index){
return this.router[index];
}
this.length = function(){
return this. router.length;
}
}
function History () {
this.history = [];
this.push = function(a) {
this.history.push(a);
};
this.get = function(index){
return this.history[ index];
}
this.length = function(){
return this.history.length;
}
}
it ('should call _viewCompany', () => {
jest.spyOn(CompaniesList.prototype,'_viewCompany');
wrapper = mount(,{context :{router: new
Router(), history: new History())));
const viewButton = wrapper.find('.btn-info').at(0);
viewButton.simulate('click');
expect(CompaniesList.prototype._viewCompany).toHaveBeenCalled();
expect(wrapper.context().router.length()).toEqual(' 1');
});
You can even test the context object. Just like I did above.