Enzyme: cached find() result is not updated after re-render

Created on 25 May 2019  路  3Comments  路  Source: enzymejs/enzyme

In short: if I put find() result into interm variable it is not updated after wrapper is re-rendered. Meanwhile fresh call to find returns actual element.

Having really simple component:

const List = ({ options = [] }) => 
    <>
       {options.map((i, index) => <span key={index}>{i}</span>)}
    </>

it('updates first item on updating options', () => {
    const options = [1];
    const wrapper = shallow(<List options={options} />);
    const first = wrapper.find('span').slice(0, 1);
    expect(first.props().children).toEqual(1);
    wrapper.setProps({ options: [2] });
    expect(wrapper.find('span').slice(0, 1).props().children).toEqual(2);
    expect(first.props().children).toEqual(2);  
})

Current behavior

Test fails at last statement. Pre-last(with new find() call) passes.

Expected behavior

Both cached wrapper and call to find() returns the same Wrapper instance

Your environment

Windows 10, Node 8.12.0

API

  • [x] shallow
  • [ ] mount
  • [ ] render

Version

| library | version
| ------------------- | -------
| enzyme | 3.9.0
| react | 16.8.6
| react-dom | 16.8.6
| react-test-renderer | 16.8.6
| adapter (below) | 1.13.1

Adapter

  • [x] enzyme-adapter-react-16
  • [ ] enzyme-adapter-react-16.3
  • [ ] enzyme-adapter-react-16.2
  • [ ] enzyme-adapter-react-16.1
  • [ ] enzyme-adapter-react-15
  • [ ] enzyme-adapter-react-15.4
  • [ ] enzyme-adapter-react-14
  • [ ] enzyme-adapter-react-13
  • [ ] enzyme-adapter-react-helper
  • [ ] others ( )
question v3 expected difference

Most helpful comment

This is intended behavior in enzyme v3 - see https://github.com/airbnb/enzyme/blob/master/docs/guides/migration-from-2-to-3.md#element-referential-identity-is-no-longer-preserved.

So yes, exactly - everything must be re-found from the root if anything has changed.

All 3 comments

as far as I understand, there is nothing like BOM's "live collection". So after re-render occurs each previous find() may be outdated(say some elements does not exist in tree anymore). And there is no easy solution to update all those collection from within Enzyme's wrapper. Is it correct reasoning?
So find() result better never being stored into intermediate variable to avoid such a cases. Right?

This is intended behavior in enzyme v3 - see https://github.com/airbnb/enzyme/blob/master/docs/guides/migration-from-2-to-3.md#element-referential-identity-is-no-longer-preserved.

So yes, exactly - everything must be re-found from the root if anything has changed.

I see. Thank you.
It's about reasoning. and there https://github.com/airbnb/enzyme/blob/master/docs/guides/migration-from-2-to-3.md#calling-props-after-a-state-change is right my case described.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blainekasten picture blainekasten  路  3Comments

aweary picture aweary  路  3Comments

heikkimu picture heikkimu  路  3Comments

amcmillan01 picture amcmillan01  路  3Comments

andrewhl picture andrewhl  路  3Comments