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);
})
Test fails at last statement. Pre-last(with new find() call) passes.
Both cached wrapper and call to find() returns the same Wrapper instance
Windows 10, Node 8.12.0
| 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
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.
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.