Find Second To Last Child With Enzyme

- 1 answer

How to look for the second to last child node returned by the find() method of Enzyme? The last child could be found with .last(), but the one before that?

I know it doesn't return an array, but what I would expect to do is something like:

const elementsArray = wrapper.find('element');
const secondToLastElement = - 1);

Thanks in advance.




Ideally, your solution should also work by subtracting 2 from total length, since '.find' return iterable object.

Please find below as updates:

const elementsArray = wrapper.find('element');
const secondToLastElement = - 

Alternatively, I believe we can use below solution to find count and then use '.at' to find the second last element. which is not the ideal solution but can be of help.

let totalElements = 0;
component.find('MyInnerComponent').forEach( (node) => { 
    totalElements++; });

once we do have total count, we can make use of code you have suggested.

Below are links which might help add to the answer

React Enzyme find second (or nth) node.

I think alternatively you can use '.get' as well, if this helps the scenario. However, I am sure you already been through the enzyme documentation, but putting the link for reference on '.get' and '.at' respectively.

I hope, this helps.