Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

componentWillReceiveProps is always invoked in shallow, even getDerivedStateFromProps exists #1692

Closed
1 of 3 tasks
MrFatt opened this issue Jun 27, 2018 · 2 comments
Closed
1 of 3 tasks

Comments

@MrFatt
Copy link

MrFatt commented Jun 27, 2018

Description

As I tested, the componentWillReceiveProps will always be invoked when after calling shallowWrapper.setProps(), even though getDeriveStateFromProps exists. But referred to this, all the UNSAFE lifecycle methods should not be invoked when getDerivedStateFromProps exists.

By the way, componentWillUpdate and componentWillMount behaves correctly in shallow.
And, for mount, all the UNSAFE methods won't be invoked when component have getDerivedStateFromProps method

Code

import SomeComponent from '../SomeComponent';
import Enzyme, { render, shallow, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import React from 'react';

describe("enzyme tests life cycles", () => {
  Enzyme.configure({adapter: new Adapter()});

  let renderSpy, getDerivedStateFromPropsSpy, componentDidMountSpy,
    shouldComponentUpdateSpy, componentDidUpdateSpy, componentWillUnmountSpy, componentWillMountSpy,
    componentWillUpdateSpy, componentWillReceivePropsSpy;
  beforeEach(() => {
    renderSpy = jest.spyOn(SomeComponent.prototype, 'render');
    getDerivedStateFromPropsSpy = jest.spyOn(SomeComponent, 'getDerivedStateFromProps');
    componentDidMountSpy = jest.spyOn(SomeComponent.prototype, 'componentDidMount');
    shouldComponentUpdateSpy = jest.spyOn(SomeComponent.prototype, 'shouldComponentUpdate');
    componentDidUpdateSpy = jest.spyOn(SomeComponent.prototype, 'componentDidUpdate');
    componentWillUnmountSpy = jest.spyOn(SomeComponent.prototype, 'componentWillUnmount');
    componentWillMountSpy = jest.spyOn(SomeComponent.prototype, 'componentWillMount');
    componentWillUpdateSpy = jest.spyOn(SomeComponent.prototype, 'componentWillUpdate');
    componentWillReceivePropsSpy = jest.spyOn(SomeComponent.prototype, 'componentWillReceiveProps');
  });
  afterEach(() => {
    jest.clearAllMocks();
  });


  it('shallow called hooks', async () => {
    const wrapper = shallow(<SomeComponent/>);
    expect(renderSpy).toHaveBeenCalledTimes(1);
    expect(getDerivedStateFromPropsSpy).toHaveBeenCalledTimes(1);
    expect(componentWillMountSpy).not.toHaveBeenCalled();
    expect(componentDidMountSpy).toHaveBeenCalledTimes(1);
    expect(shouldComponentUpdateSpy).not.toHaveBeenCalled();
    expect(componentDidUpdateSpy).not.toHaveBeenCalled();
    expect(componentWillUnmountSpy).not.toHaveBeenCalled();

    await wrapper.setProps({a: 1});
    expect(getDerivedStateFromPropsSpy).toHaveBeenCalledTimes(2);
    expect(componentWillReceivePropsSpy).not.toHaveBeenCalled();
    expect(shouldComponentUpdateSpy).toHaveBeenCalledTimes(1);
    expect(componentWillUpdateSpy).not.toHaveBeenCalled();
    expect(componentDidUpdateSpy).toHaveBeenCalledTimes(1);
    expect(componentWillUnmountSpy).not.toHaveBeenCalled();

  });
});

I am expecting the tests to pass, but the result shows that

image

expect(componentWillReceivePropsSpy).not.toHaveBeenCalled(); failed.

Your environment

API

  • shallow
  • mount
  • render

Version

Tool Easea
React 16.4.1
jest 20.0.4
Enzyme 3.3.0
enzyme-adapter-react-16 1.1.1
@koba04
Copy link
Contributor

koba04 commented Jul 2, 2018

@MrFatt You are right. Because enzyme calls componentWillReceiveProps directly in order to call all lifecycle methods correctly with shallow.
I think this will be fixed by #1192.

@ljharb
Copy link
Member

ljharb commented Jul 7, 2018

Closing, since #1192 is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants