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):
Smartphone (please complete the following information):
Additional context
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 | });
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.
Most helpful comment
You're using shallow rendering - which means the
Alertwill not be rendered initially. You can usewrapper.find(Alert).shallow().text(), however.