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

Add support for MSW and API mocking #505

Merged
merged 6 commits into from
Sep 22, 2023
Merged

Add support for MSW and API mocking #505

merged 6 commits into from
Sep 22, 2023

Conversation

tajo
Copy link
Owner

@tajo tajo commented Sep 22, 2023

Docs will follow.

Get started

.ladle/config.mjs:

export default {
  addons: {
    msw: {
      enabled: true,
    },
  },
};

Note: Ladle automatically creates public/mockServiceProvider.js (in your public folder) and keeps it up to date.

Usage

import { useEffect, useState } from "react";
import type { Story } from "@ladle/react";
import { msw } from "@ladle/react";


const fetchData = async (url: string, setData: (data: any) => void) => {
  try {
    const data = await fetch(url);
    const json = await data.json();
    setData(json);
  } catch (e) {
    console.error(e);
  }
};


const FETCH_URL = "/todos.json";

export const Mocked: Story = () => {
  const [todos, setTodos] = useState([]);
  useEffect(() => {
    fetchData(FETCH_URL, setTodos);
  }, []);
  return (
    <>
      <h1>Todos</h1>
      <ul>
        {todos.map((todo: { id: number; title: string }) => (
          <li key={todo.id}>{todo.title}</li>
        ))}
      </ul>
    </>
  );
};

Mocked.msw = [
  msw.rest.get(FETCH_URL, (_, res, ctx) => {
    return res(ctx.json([{ id: 1, title: "msw todo" }]));
  }),
];

You can also define msw for all stories through

export default {
  msw: []
}

Story-level msw replaces export default msw.

We re-export the whole msw package so you can import it through @ladle/react (no need to install your own msw dep)

import { msw } from "@ladle/react";

@changeset-bot
Copy link

changeset-bot bot commented Sep 22, 2023

🦋 Changeset detected

Latest commit: e152bc9

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

This PR includes changesets to release 13 packages
Name Type
@ladle/react Minor
example Patch
test-addons Patch
test-babel Patch
test-config Patch
test-config-ts Patch
test-css Patch
test-decorators Patch
test-playwright Patch
test-programmatic Patch
test-provider Patch
test-baseweb Patch
test-msw 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

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Sep 22, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: e152bc9
Status: ✅  Deploy successful!
Preview URL: https://00fe3602.ladle.pages.dev
Branch Preview URL: https://msw.ladle.pages.dev

View logs

@tajo tajo merged commit 740378d into main Sep 22, 2023
5 checks passed
@tajo tajo deleted the msw branch September 22, 2023 17:18
@tajo tajo mentioned this pull request Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant