Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shallow text() returns 'TouchableText' instead text itself. #2188

Closed
11 tasks
donghopark opened this issue Jul 11, 2019 · 14 comments
Closed
11 tasks

shallow text() returns 'TouchableText' instead text itself. #2188

donghopark opened this issue Jul 11, 2019 · 14 comments

Comments

@donghopark
Copy link

donghopark commented Jul 11, 2019

Thanks for reporting an issue to us! We're glad you are using and invested in Enzyme.
Before submitting, please read over our commonly reported issues to prevent duplicates!

All common issues

Notoriously common issues

If you haven't found any duplicated issues, please report it with your environment!

Current behavior

const text = 'some text';
const wrapper = shallow(<Text>text</Text>);
expect(wrapper.text()).toBe(text);

Enzyme shallow returns '<TouchableText />'

Expected behavior

const text = 'some text';
const wrapper = shallow(<Text>{text}</Text>);
expect(wrapper.text()).toBe(text);

Enzyme shallow returns 'some text'

Your environment

API

  • [ v ] shallow
  • mount
  • render

Version

library version
enzyme 3.10.0
react 16.8.6
react-dom 16.8.6
react-test-renderer 16.8.3
adapter (below) 1.14.0

Adapter

  • [ v ] enzyme-adapter-react-16
  • enzyme-adapter-react-16.3
  • enzyme-adapter-react-16.2
  • enzyme-adapter-react-16.1
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )
@ljharb
Copy link
Member

ljharb commented Jul 11, 2019

This is expected because a shallow wrapper of X is whatever X renders, not X itself - additionally, shallow takes a react element, not a string.

What happens if you shallow wrap a component that renders your desired text?

@donghopark
Copy link
Author

The problem here is that it returns 'TouchableText'. It looks happening only in some environment. In other environments, it returns 'some text' correctly.

@ljharb
Copy link
Member

ljharb commented Jul 11, 2019

One issue i see is that your react-test-renderer version is 16.3 while your react is 16.8; those always need to be the same minor.

Separately though, what does shallow(<Text />).debug() print out? If it renders a TouchableText component that in turn renders text, then you'd need to do shallow(<TouchableText />).text() to get that text.

@donghopark
Copy link
Author

react and react-test-renderer minor versions are all 8. 16.8.3 and 16.8.6. Do you mean that all patch ( the latest) should be same?

@ljharb
Copy link
Member

ljharb commented Jul 11, 2019

Oh, nvm, i misread. In that case that’s not it - the other point still stands, tho.

@ParkDongJo
Copy link

I have same Issue.

@ParkDongJo
Copy link

ParkDongJo commented Jul 12, 2019

One issue i see is that your react-test-renderer version is 16.3 while your react is 16.8; those always need to be the same minor.

Separately though, what does shallow(<Text />).debug() print out? If it renders a TouchableText component that in turn renders text, then you'd need to do shallow(<TouchableText />).text() to get that text.

TouchableText is what? i can't search about TouchableText element in google or anywhere..

also impossible about that shallow(<TouchableText />).text(), because can't import TouchabeText from anywhere

@ljharb
Copy link
Member

ljharb commented Jul 12, 2019

Can you share the code for “Text”?

If this is the react native primitive, without an actual react native enzyme adapter, you won’t be able to reliably use enzyme with react - follow #1436 for that.

@donghopark
Copy link
Author

donghopark commented Jul 12, 2019

shallow().debug() results in below.

<TouchableText forwardedRef={{...}} accessible={true} allowFontScaling={true} ellipsizeMode="tail">
some text
</TouchableText>

@ljharb
Copy link
Member

ljharb commented Jul 12, 2019

And the code for the Text component?

@donghopark
Copy link
Author

What do you mean by 'the code for the Text component'?

const text = 'some text';
const wrapper = shallow(<Text>text</Text>);
expect(wrapper.text()).toBe(text);

is the code to test the enzyme and the Text component. 'Text' is the primitive RN component.

@ljharb
Copy link
Member

ljharb commented Jul 12, 2019

OK, so then the answer is

If this is the react native primitive, without an actual react native enzyme adapter, you won’t be able to reliably use enzyme with react - follow #1436 for that.

enzyme does not support React Native without a react-native adapter, which as far as I know does not yet exist.

Closing in lieu of #1436.

@ljharb ljharb closed this as completed Jul 12, 2019
@matborowiak
Copy link

matborowiak commented Oct 18, 2022

this should work

const text = 'some text';
const wrapper = shallow(<Text>{text}</Text>);
const textNode = wrapper.find('Text')

expect(textNode.children().text()).toEqual(text);

@RahulkumarRV
Copy link

const text = 'some text';
const wrapper = shallow({text});
const textNode = wrapper.find('Text')

expect(textNode.children().text()).toEqual(text);
it works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants