ReactJS – React Unit Test, Unselected “Push” of undefined reaction routers

I have a component that uses React Router, as shown below:

_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?

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(< 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.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5938 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.