Find Second To Last Child With Enzyme
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 = elementsArray.at(elementsArray.length - 1);
Thanks in advance.
Answer
@EIDuderino
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 = elementsArray.at(elementsArray.length -
2);
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.
- https://enzymejs.github.io/enzyme/docs/api/ShallowWrapper/get.html
- https://enzymejs.github.io/enzyme/docs/api/ShallowWrapper/at.html
I hope, this helps.
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM