Testing the href value from the link component in the presence of dynamic details

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

Here is a failed 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 should be equivalent to "/ my_route"

The React-router knowbody answered to see how they test Link, but they don’t have a dynamic context that can change the value of the href attribute.

So, I did something like this:

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 helper component:

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;
}

this.component, . ?

+4
1

react-router <Link /> :

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>
    )
}

, !

+3

Source: https://habr.com/ru/post/1628134/


All Articles