Open SalahAdDin opened 1 year ago
I've encountered the same issue before as well. Back then, I investigated the code, and it appears that this is probably not a bug but likely by design. The reason for this specification is unclear, but as you can see in the following code, when you pass the url prop to MemoryRouterProvider, it's implemented to replace the singleton router with an isolate router.
To resolve this issue, you can create a wrapper for MemoryRouterProvider. Set the url passed in via props using setCurrentUrl as shown below and pass the remaining props other than url to MemoryRouterProvider:
import singletonRouter from 'next-router-mock';
import { MemoryRouterProvider } from 'next-router-mock/MemoryRouterProvider';
import type { MemoryRouterProviderProps } from 'next-router-mock/dist/MemoryRouterProvider/MemoryRouterProvider';
const SingletonRouterProvider = ({ url, ...rest }: MemoryRouterProviderProps) => {
useEffect(() => {
if (!url) {
return;
}
singletonRouter.setCurrentUrl(url);
}, [url]);
return <MemoryRouterProvider {...rest} />;
};
const renderWithRouter = (ui: React.ReactElement, { route = "/" } = {}) => {
const wrapper = ({ children }: { children: React.ReactNode }) => (
<SingletonRouterProvider url={route}>{children}</SingletonRouterProvider>
);
return {
user: userEvent.setup(),
...render(ui, { wrapper }),
};
};
@scottrippey
The reason for this specification is unclear, but as you can see in the following code, when you pass the url prop to MemoryRouterProvider, it's implemented to replace the singleton router with an isolate router.
It appears that this implementation was added in the following PR, but could you please explain the reason behind it?
@scottrippey Are you here?
I'm trying to text links with this package and I am getting inconsistent behavior.
I defined a custom render as follows:
And I'm using it on my test like this:
Logging the router path we get:
Yeah, it should be "Current Path /streaming".
Also, after clicking on the button, it should navigate easily, but it seems the route is always overwritten to be the given one and it does not change.
When using it without
url
property it works well, and navigates correctly.It seems I got the property wrong, it should work as an initial route, right? Could you write an example of writing it as a provider with options using testing library?