Paratron / hookrouter

The flexible, and fast router for react that is entirely based on hooks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

currentPath in router.js is not updated and remain "/" when we set location.pathname from unit test

Ayyappu opened this issue · comments

I tried to mock the browser url as follows in my test:

window.history.replaceState({}, "Test", "/abc");

However, since the currentPath value is set (in router.js) before the url above is set, currentPath remains "/" even after chaning the location.pathname = "/abc".

let currentPath = isNode ? '' : location.pathname;

It occurs only in unit testing. While loading the application from browser, the currentPath is set as "/abc" (whatever url is loaded) correctly.

Due to this, when I call navigate("/") though with location.pathname = "/abc", the currentPath is still "/".

export const navigate = (url, replace = false, queryParams = null, replaceQueryParams = true) => {
url = interceptRoute(currentPath, resolvePath(url));

if (!url || url === currentPath) {
	return;
}
...

CurrentPath should be "/abc" but it is actually "/". Since, the url and currentPath are same i.e., "/", it is returned and not navigated.

Could we have the check to be as follows?

if (!url || url === location.pathname) {
	return;
}
...

If we change the code as above, navigation would happen as getWorkingPath ("/abc") is not equal to the navigate url ("/").

Also, please advise how to set currentPath to be "/abc" from my test file so that we can avoid the update above?