Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing href value from a link component while having dynamic props

Tags:

react-router

I am looking for a solution in order to still be able to use Link from react-router instead of a when testing href attribute value. Indeed, I have some components which change of route according to the context. However, when I am testing the href attribute value, the only thing returned is null. However, when I use an a, it returns me the expected value.

Here is an failing test:

import React from 'react';
import {Link} from 'react-router';
import TestUtils from 'react-addons-test-utils';
import expect from 'must';

const LINK_LOCATION = '/my_route';

class TestComponent extends React.Component {

    render() {
        return (
            <div>
                <Link className='link' to={LINK_LOCATION}/>
                <a className='a' href={LINK_LOCATION}/>
            </div>
        );
    }
}

describe('Url things', () => {
    it('should return me the same href value for both link and a node', () => {
        const test_component = TestUtils.renderIntoDocument(<TestComponent/>);
        const link = TestUtils.findRenderedDOMComponentWithClass(test_component, 'link');
        const a = TestUtils.findRenderedDOMComponentWithClass(test_component, 'a');
        expect(link.getAttribute('href')).to.eql(a.getAttribute('href'));
    });
});

Output: AssertionError: null must be equivalent to "/my_route"

knowbody from React-router answered to see how they test Link, but they do not have dynamic context which can change value of the href attribute.

So I have done something like that:

class ComponentWrapper extends React.Component {
    constructor(props) {
        super(props);
        this.state = {};
    }

    set_props(props) {
        this.setState({props});
    }

    render() {
        if (this.state.props) {
            return <Component {...this.state.props}/>;
        }
        return null;
    }
}

But now, from my component helper:

render_into_document() {
    const full_component_props = {
        location: this.location,
        widget_categories: this.widget_categories
    };
    node = document.createElement('div');
    this.component = render((
        <Router history={createHistory('/')}>
            <Route path='/' component={ComponentWrapper} />
        </Router>
    ));
    this.component.set_props(full_component_props);
    return this;
}

I am not able to lay hand on this.component in order to changes props. How could I do that?

like image 223
Finstairn Avatar asked Feb 10 '16 14:02

Finstairn


1 Answers

I just looked at how react-router tests <Link /> and came up with this for my case:

import test from 'ava'
import React from 'react'
import { render } from 'enzyme'
import { Router, Route } from 'react-router'
import createHistory from 'history/lib/createMemoryHistory'
import SkipToXoom from '../skip-to-xoom'


test('the rendered button redirects to the proper URL when clicked', t => {
    const toCountryData = { countryName: 'India', countryCode: 'IN' }
    const div = renderToDiv({ toCountryData, disbursementType: 'DEPOSIT', userLang: 'en_us' })
    const { attribs: { href } } = div.find('a')[0]
    t.true(href.includes(encodeURIComponent('receiveCountryCode=IN')))
    t.true(href.includes(encodeURIComponent('disbursementType=DEPOSIT')))
    t.true(href.includes(encodeURIComponent('languageCode=en')))
})

/**
 * Render the <SkipToXoom /> component to a div with the given props
 * We have to do some fancy footwork with the Router component to get
 * the Link component in our SkipToXoom component to render out the href
 * @param {Object} props - the props to apply to the component
 * @returns {Element} - the div that contains the element
 */
function renderToDiv(props = {}) {
    return render(
        <Router history={createHistory('/')}>
            <Route path="/" component={() => <SkipToXoom {...props} userLang="en" />} />
        </Router>
    )
}

I hope that's helpful!

like image 114
kentcdodds Avatar answered Nov 29 '22 02:11

kentcdodds