Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

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

Open
Ayyappu opened this issue Jun 15, 2020 · 0 comments

Comments

@Ayyappu
Copy link

Ayyappu commented Jun 15, 2020

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant