enzymejs / chai-enzyme

Chai.js assertions and convenience functions for testing React Components with enzyme
MIT License
787 stars 72 forks source link

Assertions on render() wrapped components don't work for elements that aren't an array #192

Open cableray opened 7 years ago

cableray commented 7 years ago

For example:

const wrapper = render(<span><a href ="foo">first</a><a href ="bar">second</a></span>)
const secondLink = wrapper.find('a')[1]
expect(secondLink).to.have.attr('href', 'bar')

fails (because it expects secondLink to respond to first(), but it isn't an array of results)

This makes it impossible to assert on specific items in an array of results.

ljharb commented 7 years ago

[1] doesn't give you a wrapped element. I think you want .at(1).

cableray commented 7 years ago

Strangely, .at is undefined on wrapper and the result of find('a') 🤔. Is that normal?

Versions:

ljharb commented 7 years ago

ah, sorry, you're using render. You want .eq(1) then, just like in jquery. Array bracket syntax gets you a raw, unwrapped thing.

cableray commented 7 years ago

Ok, thanks @ljharb! It is a bummer that this doesn't work with other chai tools like chai-things because of not being a real array, but I'm not sure that would be an easy thing to do.

ryanblakeley commented 7 years ago

cheerio@v1.0.0-rc.2 is published. Could we have that as an acceptable peer?