Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Jest : Test method in reactjs component

I have functional component FilterBar -

const FilterBar=()=>{

....
....
useEffects..
states...

function apiCallToFilters()
{
  ...
  ...
}

...
..

}

Since entire code cannot be shared , I have shared a structure and a method which I want to test from jest.

I have written FilterBar.test.tsx -

describe('Filter Bar', () => {
  it('All Api Calls', () => {
    const wrapper = shallow(<FilterBar />);
    expect(wrapper.instance().apiCallToFilters //not able to find apiCallToFilters method here
  });
});

I have followed this question from SO.

After having wrapper.instance(). I am getting below properties and methods -

context
forceUpdate
getSnapshotBeforeUpdate
props
render
setState
shouldComponentUpdate
state

How can I test apiCallToFilters method in FilterBar from jest?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Enzyme - the library that gives programmatic access to a component instance's properties can only give access to properties that are accessible from outside as it cannot violate JavaScript's encapsulation principles.

I cannot see what kind of a component you are using from in your example code, but I would strongly advise you to convert it to a class component and change the apiCallToFilters function into a method (by removing the function keyword).

Link to a thread where this is called out can be found here.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!