You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How am I supposed to test this without mocking useLoaderData?
For components, I've managed to use createMemoryRouter to provide loader's data inside hydrationData option and rendering RouterProvider. Something like:
// result.current === undefinedconst{ result }=renderHook(()=>useCustomHook(),{wrapper: ({ children })=>{constactionData={};constloaderData={};consterrorData={};constrouter=createMemoryRouter([{id: 'main',path: '/',element: children,},],{initialEntries: ['/'],hydrationData: {actionData: {main: actionData},loaderData: {main: loaderData},},},);return<RouterProviderrouter={router}/>;},});
It would be nice to use MemoryRouter there, but it does not support data API. So, any suggestions to sort this out without mocking useLoaderData?
EDIT: nevermind, I made it work. The problem was I was also passing an empty object to hydrationData.errors and it was causing the router not to render the right thing.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to test a React custom hook that uses
useLoaderData
hook inside of it. Something like:How am I supposed to test this without mocking
useLoaderData
?For components, I've managed to use
createMemoryRouter
to provide loader's data insidehydrationData
option and renderingRouterProvider
. Something like:But that just doesn't work with hooks:
It would be nice to use
MemoryRouter
there, but it does not support data API. So, any suggestions to sort this out without mockinguseLoaderData
?EDIT: nevermind, I made it work. The problem was I was also passing an empty object to
hydrationData.errors
and it was causing the router not to render the right thing.Beta Was this translation helpful? Give feedback.
All reactions