Enzyme: text() returns element instead of text

Created on 29 Aug 2018  ยท  2Comments  ยท  Source: enzymejs/enzyme

Describe the bug
text() returns element instead of text

To Reproduce
Steps to reproduce the behavior:

src/namaskaara.tsx

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import './app.css'; // contains a single line @import '~bootstrap/dist/css/bootstrap.min.css';

import { Alert } from 'reactstrap';

export const namaskaara =
          <div>
            <Alert color="info">namaskaara react !</Alert>
          </div>;

src/test/namaskaara.spec.tsx

import React from 'react';
import { shallow } from 'enzyme';
import { Alert } from 'reactstrap';

import { namaskaara } from '../namaskaara';

it('renders correctly', () => {
  const wrapper = shallow(namaskaara);
  expect(wrapper.find('div').equals(<div><Alert color="info">namaskaara react !</Alert></div>)).toBe(true); // passes
  expect(wrapper.find(Alert).equals(<Alert color="info">namaskaara react !</Alert>)).toBe(true); // passes
  expect(wrapper.find(Alert).text()).toEqual('namaskaara react !'); // fails
  console.log(wrapper.find(Alert).text()); // shows <Alert /> instead of namaskaara react !
});

Expected behavior
expect that text() returns 'namaskaara react !'

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Linux
  • Browser google chrome
  • Version latest

Smartphone (please complete the following information):

  • Device: NA
  • OS: NA
  • Browser NA
  • Version NA

Additional context

  • jest output *
    ```
    โ— renders correctly
expect(received).toEqual(expected)

Expected value to equal:
  "namaskaara react !"
Received:
  "<Alert />"

  15 |   expect(wrapper.find('div').equals(<div><Alert color="info">namaskaara react !</Alert></div>)).toBe(true);
  16 |   expect(wrapper.find(Alert).equals(<Alert color="info">namaskaara react !</Alert>)).toBe(true);
> 17 |   expect(wrapper.find(Alert).text()).toEqual('namaskaara react !');
     |                                      ^
  18 |   console.log(wrapper.find(Alert).text());
  19 | });
question

Most helpful comment

You're using shallow rendering - which means the Alert will not be rendered initially. You can use wrapper.find(Alert).shallow().text(), however.

All 2 comments

You're using shallow rendering - which means the Alert will not be rendered initially. You can use wrapper.find(Alert).shallow().text(), however.

wrapper.dive().dive().text() worked for me. You can chain as many dives as you need.

Was this page helpful?
0 / 5 - 0 ratings