Proper use of getAllByText to find the target matching elementJanuary 20, 2020 at 8:35pm (Edited 2 months ago)
I have a document with two buttons. One is 'save' and the other is 'save as'. In the test to make sure the 'save' button appears on the page, I use 'getAllByText' to find elements that match /save/i. What is the best way to make sure I am looking at the 'save' button instead of the 'save as' button?
Index in the array: I know that the 'save' button comes before the 'save as' button. So I could just look at getAllByText(/save/i). But that seems like bad practice, because it will return the wrong element if I decide to rearrange the buttons.
Array.find: I could use Array.find to find e.g. an element with textContent.length === 4. This feels a little redundant, like if I'm doing that I should just pass a custom matcher function to the query in the first place. But it will work and doesn't rely on the component order.
Are either of these best practice, or is there another method?
January 20, 2020 at 10:18pm