2

I have a unit test that uses that contains function which works. Is it possible to select an node from a collection and then take the text property? I tried this:

it('should be equaling name property', () => {
        const props = {
            myprops: [{
                email: "blaat@mail.com",
                name: "Coco Chanel"
            }]
        };
        const wrapper = shallow(<ServiceDetails {...props} />);
        expect(wrapper.find('div')[0].text).equals('Cici Chanel');
 });

This is part of my reactjs component:

  return (
          <section>
                <div>{email}</div>
                <div>{name}</div>
          </section>
  )

However I am getting this error:

 TypeError: Cannot read property 'text' of undefined

How can I select the second div and take the value that is in that div?

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
bier hier
  • 20,970
  • 42
  • 97
  • 166

2 Answers2

1

I believe you would need to uniquely identify the div:

expect(wrapper.find('section div:nth-of-type(1)').text()).equals('Cici Chanel');

or better:

return (
      <section>
            <div className="div1">{email}</div>
            <div>{name}</div>
      </section>
)

expect(wrapper.find('.div1').text()).equals('Cici Chanel');

Also text is function, not property.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
0

For me, the following code works

const component = wrappper.find('section.div')
expect(component.at(0).text()).toEqual('Cici chanel')
Ali
  • 2,702
  • 3
  • 32
  • 54