createRemixStub: provide a way to modify requests #7083
Closed
kentcdodds
started this conversation in
Proposals
Replies: 1 comment 3 replies
-
Oh, I just realized something... I can do this! const cookieHeader = await getSessionCookieHeader(user)
const App = createRemixStub([
{
id: 'root',
path: '/',
loader: async args => {
// add the cookie header to the request
args.request.headers.set('cookie', cookieHeader)
return rootLoader(args)
},
children: [
{
path: '/users/:username',
element: <UsernameRoute />,
loader: async args => {
// add the cookie header to the request
args.request.headers.set('cookie', cookieHeader)
// @ts-expect-error https://github.com/remix-run/remix/issues/7082
return loader(args)
},
},
],
},
]) This works fine. Way better than my other idea. Maybe I'm tired. Please ignore 😅 |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Sometimes, I want to test full routes with
createRemixStub
(this would apply to testing react-router apps as well btw). When I do, it looks something like this:This test allows me to easily get coverage on the loaders as well as the UI they power.
However, often the loaders need to access state (like cookies) from the request to determine whether the user is logged in. But we have no way to set those headers on the request.
Unfortunately no, setting
document.cookie
does not work (not even in a real browser):I'm not sure what can be done about this issue. Perhaps another configuration option? Just an idea:
Beta Was this translation helpful? Give feedback.
All reactions