-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Unable to get text()
from a string wrapped with curly braces
#1514
Comments
mount(), rather than shallow(), provides what you want. Shallow-renderer doesn't render the internal components. Or, dive() also would help you in general cases, Try the following codes: it('test deep node', () => {
function Script(props) {
return <span>{props.text}</span>;
}
function App(props) {
return (
<div>
<Script type="text/html" id="store" text={JSON.stringify(props)}></Script>
</div>
);
}
const state = { "hello": "HELLO" };
expect(mount(<App {...state} />).find('#store').text()).toEqual(JSON.stringify(state));
expect(shallow(<App {...state} />).find('#store').dive().text()).toEqual(JSON.stringify(state));
}); EDIT: |
This is not an issue with shallow. As I indicated above I tried markup that does not use any prop values, aside from what I really wanted with the innerHTML. The string |
@DaveStein However, HTML with no Here's my test code: function App(props) {
return (
<div>
<script type="text/html" id="store" dangerouslySetInnerHTML={{__html: "HELLO"}}></script>
</div>
);
}
it('test render', () => {
expect(render(<App />).find('#store').text()).not.toBe(''); // NG
});
it('test mount', () => {
expect(mount(<App />).find('#store').text()).not.toBe(''); // OK
});
it('test shallow', () => {
expect(shallow(<App />).find('#store').text()).not.toBe(''); // NG
}); |
This is a good point; Separately, it's possible that |
@karak in your example, did you meant to set |
@DaveStein No, just set "HELLO" and got empty in some cases, as far as I tested, on react 16.2.0 and enzyme 3.3.0. @ljharb ADD: |
@ljharb I found Cheerio 1.0.0-rc.2, depended by Enzyme 3.3.0, is going to have a breaking change by cheeriojs/cheerio#1050. Is the dependency considered to be valid? |
|
Current behavior
I have a couple of lines like this in my test:
The markup is this:
<script type="text/html" id="store" dangerouslySetInnerHTML={{__html: JSON.stringify(this.props)}}></script>
.I believe it's failing because it translates to a string with curly braces. For example,
.text()
will return..."Hello"
from<script type="text/html" id="store">Hello</script>
""
from<script type="text/html" id="store">{}</script>
Expected behavior
A way to get the contents, even if they are JSON/JS.
Your environment
API
Version
Adapter
The text was updated successfully, but these errors were encountered: