Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test componentDidUpdate()?

This is an example implementation:

export class Person extends Component {   componentDidMount() {     const { onLoadProfile, onLoadPolicy, person } = this.props     onLoadProfile(person.profile.uri)     onLoadPolicy(person.policy.uri)   }    componentDidUpdate(prevProps) {     const { onLoadProfile, onLoadPolicy, person } = this.props     const prevPerson = prevProps.person.uri     const curPerson = person.uri      // If person has changed, update Person component     if (prevPerson !== curPerson) {       onLoadProfile(person.profile.uri)       onLoadPolicy(person.policy.uri)     }   } } 

On componentDidMount(), I've managed to test it like this:

describe('<Person />', () => {   let props   let mountedPerson   const mockLoadProfile = jest.fn()   const mockLoadPolicy = jest.fn()    const person = () => {     if (!mountedPerson) {       mountedPerson = mount(<Person {...props} />)     }      return mountedPerson   }    beforeEach(() => {     props = {       onLoadProfile = mockLoadProfile,       onLoadPolicy = mockLoadPolicy     }     mountedPerson = undefined   })    afterEach(() => {     mockLoadProfile.mockClear()     mockLoadPolicy.mockClear()   })    describe('componentDidMount', () => {     it('loads profile', () => {       person().instance().componentDidMount()       expect(mockLoadProfile).toBeCalled()     })      it('loads policy', () => {       person().instance().componentDidMount()       expect(mockLoadPolicy).toBeCalled()     })   }) }) 

On componentDidUpdate(), I'd need the component to attempt to render() twice in order to verify if it updates when it should and vice-versa, yet I couldn't find a proper way to do it.

What is the correct approach to test a componentDidUpdate() method in React?

PS.: I'm using jest, enzyme and React 15.

like image 802
Filipe Gorges Reuwsaat Avatar asked Oct 22 '18 14:10

Filipe Gorges Reuwsaat


People also ask

How do I use componentDidUpdate?

ReactJS componentDidUpdate() Method. The componentDidUpdate() method allows us to execute the React code when the component is updated. All the network requests that are to be made when the props passed to the component changes are coded here.

What triggers componentDidUpdate?

The componentDidUpdate event is triggered whenever there is a state or props update. ComponentDidUpdate() has access to three properties, two of which are most often used more than the third. These are prevProps, prevState and lastly, snapshot, which is used less often.

Does componentDidUpdate run before render?

componentDidUpdate is always the last lifecycle to get called for a React component. componentDidUpdate does not get called after the first initial render() lifecycle.

Is componentDidUpdate called after every render?

componentDidUpdate() is invoked immediately after updating occurs. This method is not called for the initial render. Use this as an opportunity to operate on the DOM when the component has been updated.


1 Answers

I am using a different approach but you can copy the idea. You need to make a change in the props, I used the setProps() function:

describe('componentDidUpdate', () => {     it('loads profile', () => {          const wrapper = shallow(<Person  {...props} />) as any;         wrapper.setProps({ person: { uri: "something_different" } });         expect(wrapper.instance().props.onLoadProfile).toBeCalled();     }) }) 

I can see the pink in the coverage tests page is gone in the componentDidUpdate after running the test

like image 191
Bonomi Avatar answered Sep 18 '22 13:09

Bonomi