Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(remix-testing): cast types to use Remix type definitions + allow passing context #6065

Merged
merged 5 commits into from
Apr 13, 2023

Conversation

mcansh
Copy link
Collaborator

@mcansh mcansh commented Apr 13, 2023

before if you tried to pass a loader/action directly to the route object it would fail as context is optional in RR, but required in Remix (types for each are also different, bug would remain if required), so you would have to manually pass context: {}

import { json, type DataFunctionArgs } from "@remix-run/node";
import { unstable_createRemixStub as createRemixStub } from "@remix-run/testing";

export async function loader(args: DataFunctionArgs) {
  return json({ hi: "there" });
}

createRemixStub([
  {
    path: "/example",
    loader: args => loader({ ...args, context: {} }),
  },
]);

now we're using the Remix types for loader/action, but we need to cast them on the way out back to RR for use with createMemoryRouter

import { json, type DataFunctionArgs } from "@remix-run/node";
import { unstable_createRemixStub as createRemixStub } from "@remix-run/testing";

export async function loader(args: DataFunctionArgs) {
  return json({ hi: "there" });
}

createRemixStub([
  {
    path: "/example",
    loader,
  },
]);

this PR also introduces the ability to pass context to your stubbed loaders/actions via a second argument to createRemixStub

unstable_createRemixStub(
  [
    {
      path: "/",
      index: true,
      element: <App />,
      loader({ context }) {
        return json(context);
      },
    },
  ],
  { context: "hello" }
);

closes #6054

  • Docs
  • Tests

Testing Strategy: new tests

mcansh added 2 commits April 13, 2023 12:14
Signed-off-by: Logan McAnsh <logan@mcan.sh>
Signed-off-by: Logan McAnsh <logan@mcan.sh>
@changeset-bot
Copy link

changeset-bot bot commented Apr 13, 2023

🦋 Changeset detected

Latest commit: 9e3d7f8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
@remix-run/testing Patch
create-remix Patch
remix Patch
@remix-run/architect Patch
@remix-run/cloudflare Patch
@remix-run/cloudflare-pages Patch
@remix-run/cloudflare-workers Patch
@remix-run/css-bundle Patch
@remix-run/deno Patch
@remix-run/dev Patch
@remix-run/eslint-config Patch
@remix-run/express Patch
@remix-run/netlify Patch
@remix-run/node Patch
@remix-run/react Patch
@remix-run/serve Patch
@remix-run/server-runtime Patch
@remix-run/vercel Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Signed-off-by: Logan McAnsh <logan@mcan.sh>
mcansh added 2 commits April 13, 2023 13:25
Signed-off-by: Logan McAnsh <logan@mcan.sh>
[skip ci]

Signed-off-by: Logan McAnsh <logan@mcan.sh>
@mcansh mcansh merged commit 223240f into dev Apr 13, 2023
@mcansh mcansh deleted the logan/remix-testing-context branch April 13, 2023 18:35
@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version v0.0.0-nightly-987ee78-20230414 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

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

Successfully merging this pull request may close these issues.

3 participants