Testing React-Router v1.0.0 href

Currently using Jest with React-Router to validate and validate an element has href with the expected value.

following the instructions here

jest.dontMock('../BasicPage')

describe('BasicPage', function() {
  let BasicPage = require('../BasicPage')
  let TestUtils = require('react-addons-test-utils')
  let ReactDOM = require('react-dom')
  let React = require('react')

  it('renders the Login button if not logged in', function() {
    let page = TestUtils.renderIntoDocument(<BasicPage />)
    let button = TestUtils.findRenderedDOMComponentWithTag(page, 'button')
    expect(ReactDOM.findDOMNode(button).textContent).toBe('Login')
  })

  it('renders the Account button if logged in', function() {
    let page = TestUtils.renderIntoDocument(<BasicPage authenticated={true} />)
    let button = TestUtils.findRenderedDOMComponentWithTag(page, 'button')
expect(ReactDOM.findDOMNode(button).getAttribute('href')).toBe('/admin')
  })
})

the element has a link like:

<Button bsStyle="primary"><Link to="/admin">Login</Link></Button>

when console.log'in on the href button is missing and the test returns null!

Has anyone found a way around this?

+4
source share
2 answers

You need to do things under the instance <Router>if you want, for example. <Link>to actually work. <Router>provides an instance of the story in the context that it <Link>uses to generate hrefand processes clicks.

<Link> , , , .

+1

expect(ReactDOM.findDOMNode(button).getAttribute('href')).toBe('/admin')

? ?

-1

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


All Articles