mercredi 26 septembre 2018

How to know the current url when using MemoryRouter

I'm trying to figure out how to use a test to assert what the current location pathname is.

As an experiment I wrote the following test

test(Can navigate MemoryRouter, (done) => { const logAll = (p, ...args) => { console.log(SOME OUTPUT,p, ...args) return p.location.pathname }

const div = document.createElement('div')

const Test = () => (
  <MemoryRouter>
    <React.Fragment>
      <Link to="/foo/bar" id="click-me" />
      <Route render={logAll} />
    </React.Fragment>
  </MemoryRouter>
)
ReactDOM.render(React.createElement(Test), div)
console.log('click')
Simulate.click(div.querySelector('#click-me'))

})

I would expect because the logAll route matches all changes for it to log once at / (which it does) and then again at /foo/bar. But the latter never logs before the test times out (about 5 seconds).

What am I missing?

Aucun commentaire:

Enregistrer un commentaire