Test class for type testing

I am trying to test a react class that renders several other react classes. They all work great, besides I'm not really sure about the best practice in terms of testing this.

code:

Parent class:

module.exports = function (deps) {

var MixLink = require('views/components/mixLink')(deps);

return React.createClass({

    render: function() {
        return (
            <div class="packshotData mixesPage" data-view="mixlist">
            {
                this.props.mixes.map(function (mix) {
                    return <MixLink mix={mix} />;
                })
            }
            </div>
        );
    }
});

      

};

Child class:

module.exports = function (deps) {

var Link = require('views/components/link')(deps);

var mixLink = React.createClass({

    render: function() {
        return (
            <div className="packshotData-item packshotData-item-overlay">
                <Link className="packshotData-item-link" href={this.props.mix.href} data-control="mixplay">
                    <img className="packshotData-item-image packshotHeadphones" src={this.props.mix.thumbnailHref} />
                    <div className="packshotData-item-title-overlay">
                        <span className="packshotData-item-title-text">{this.props.mix.name}</span>
                    </div>
                </Link>
            </div>
        );
    }
});

return mixLink;

      

};

Test:

describe('mixLinkList Component', function () {

var MixLinkList = require('views/components/mixLinkList')({}),
    MixLink = require('views/components/mixLink')({}),
    TestUtils = React.addons.TestUtils;

it('is a function', function () {
    expect(MixLinkList).to.be.a(Function);
});

it('is create a MixLink for every mix', function () {

    var mixes = [
        {
            href: 'http://mixlink.com/One',
            name: "thunderbirds",
            thumbnailHref: 'someUri'
        },
        {
            href: 'http://mixlink.com/Two',
            name: "captain scarlet",
            thumbnailHref: 'someOtherUri'
        }
    ],
        renderedMixLinkList,
        renderedComponents;

    renderedMixLinkList = TestUtils.renderIntoDocument(
        <MixLinkList mixes={mixes} />
    );

    renderedComponents = TestUtils.findAllInRenderedTree(renderedMixLinkList, function (elem) {

        return TestUtils.isCompositeComponentWithType(elem, MixLink);
    });

    expect(renderedComponents.length).to.be(2);
});

      

});

The test is currently failing.

I can achieve my goals by grabbing the DOM node and checking the actual HTML, which seems messy to me, since the HTML generated by MixLink is the problem of the MixLink class, not the MixLinkList class.

What I would like to do is just ask for a rendered component and check that it has two MixLink children. Is it possible?

Thanks in advance!

+3


source to share


1 answer


For the most part, your test seems fine. I think the reason this fails is because it MixLinkList

creates its own MixLink

component separate from the one in the test. You create one component MixLink

on line 2 of the parent class and another on line 3 of the test. Thus, the displayed list does not contain test items MixLink

, but rather items from the list of native link components.

Your dependency injection method is both a problem and a solution. Just change the parent class to start:

module.exports = function (deps) {

var MixLink = deps.MixLink;

      

The test can then then add a MixLink component as follows:



var MixLink = require('views/components/mixLink')({}),
    MixLinkList = require('views/components/mixLinkList')({
        MixLink: MixLink
    }),
    TestUtils = React.addons.TestUtils;

      

You might also consider using more specific TestUtils methods such as scryRenderedComponentsWithType

( docs ), which makes the last part of your test easier to read:

renderedComponents = TestUtils.scryRenderedComponentsWithType(
    renderedMixLinkList,
    MixLink
);

      

+4


source







All Articles