0

So basically I want to test an page, and I want to insert a specific URL when rendering on my test file, and then access this value I applied on my test using useLocattion(), the code is below. So I used the path='/user, on my Router when rendering on my test, and I wanted to access '/user' on my app.js using useLocation.pathname. So I expcted to console.log('/user'), but instead I am logging this :''

    //add.test.js
    import {BrowserRouter as Router} from 'react-router-dom'

    test('render page with an specific url', () => {

      render(
    test('render input with the value of the token you want to edit', async() => {
        const history = createMemoryHistory({initialEntries:['/user']});
        const { getByText,getAllByRole,getByLabelText } =  render(
        <Router path='/user' history={history} ><Add/></Router>);
        });
      )



    //app.js
     console.log('current URL️', window.location.href);
    const location = useLocation();
    console.log('hash', location.hash);
    console.log('pathname', location.pathname);// should log /user,is logging:''
    console.log('search', location.search);
        
        )
    }
  • What did you get? What do you expect? What's `Router`? Is it an alias of `BrowserRouter`? Show complete, minimal code – Lin Du Aug 15 '22 at 02:05
  • I believed I did what you asked, but sorry in advance for broken English, not my native language. Can you help me out? I basically want to pass parameters on my app.test file and accesses on my app.js,so I expect to console.log('pathname '/users') but I am only logging (pathname ). – Fabio Schitini Aug 17 '22 at 12:06

1 Answers1

0

I think the problems is that BrowserRouter uses its own History, so it's ignoring the history prop you're passing it. The soulution could be to import Router instead of BrowserRouter or you could use

window.history.pushState({}, '', yourUrl);

before the render to make a manual push.

There's a stackoverflow thread about differences between Router and BrowserRouter: BrowserRouter vs Router with history.push()