简体   繁体   English

使用React JS遍历DOM

[英]Traversing the DOM with React JS

I am trying to test this piece of HTML generated by React JS: 我正在尝试测试React JS生成的这段HTML:

<div class="media-img">
    <img src='images-name.jpg' />
</div>

As you can see, the image has no class name, and I can't put one on. 如您所见,该图像没有类名,我不能穿上它。 I am using Jasmine to try and test that the src of the image is correct. 我正在使用Jasmine尝试测试图像的src是否正确。 But I can't seem to get hold of the image element in React. 但是我似乎无法掌握React中的image元素。 In jQuery this would be really easy, I could use $('.media-img img'), is there a way to traverse the DOM in React? 在jQuery中,这真的很容易,我可以使用$('。media-img img'),有没有办法在React中遍历DOM?

This is what I have so far: 这是我到目前为止的内容:

var React = require('react'),
    TestUtils = React.addons.TestUtils,
    Component = require('component'),
    renderedComponent;

function renderComponent(data) {
    data = data || {};
    renderedComponent = TestUtils.renderIntoDocument(
        React.createElement(Component, data)
    );
}

var imageURL = 'http://lorempixel.com/g/400/200/',
    image,
    imageSection;

renderComponent({
    src: imageURL
});

imageSection = TestUtils.scryRenderedComponentsWithType(renderedComponent, 'media-img');
image = TestUtils.findRenderedDOMComponentWithTag(imageSection, 'img');

expect(image.src).toBe(imageURL);

But it isn't working. 但这不起作用。 It seems that you can't pass a React component into another as I've tried to do at the bottom. 似乎无法像我在底部尝试的那样将React组件传递给另一个组件。 So how can you traverse the virtual DOM? 那么如何遍历虚拟DOM?

I found an answer. 我找到了答案。 Instead of attempting to pass a React component into another to test a subsection of the rendered DOM, I looked through the entire rendered DOM for 'img'. 我没有尝试将React组件传递到另一个组件中以测试呈现的DOM的一个子部分,而是查看了整个呈现的DOM中的“ img”。 Normally this would be a bit inefficient as the rendered DOM could be huge and full of 'img' tags, but as this is a unit test with limited data, the rendered DOM is small and predictable. 通常,由于渲染的DOM可能很大且充满'img'标签,因此效率会有些低下,但是由于这是数据有限的单元测试,因此渲染的DOM很小且可预测。 I used this: 我用这个:

image = TestUtils.findRenderedDOMComponentWithTag(renderedContributor, 'img');

Then I didn't need the 'imageSection' variable at all. 然后,我根本不需要'imageSection'变量。 Then I could call the DOM Node, then look at its 'src', by adding: 然后,我可以调用DOM节点,然后通过添加以下内容来查看其“ src”:

image.getDOMNode().src;

So my complete amended code looks like this: 因此,我完整的修改后的代码如下所示:

var React = require('react'),
    TestUtils = React.addons.TestUtils,
    Component = require('component'),
    renderedComponent;

function renderComponent(data) {
    data = data || {};
    renderedComponent = TestUtils.renderIntoDocument(
        React.createElement(Component, data)
    );
}

var imageURL = 'http://lorempixel.com/g/400/200/',
    image;

renderComponent({
    src: imageURL
});

image = TestUtils.findRenderedDOMComponentWithTag(renderedComponent, 'img');

expect(image.getDOMNode().src).toBe(imageURL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM