-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
id: msw | ||
title: MSW | ||
--- | ||
|
||
Ladle has a build-in support for [MSW](https://mswjs.io/). It allows API mocking by intercepting requests on the network level. | ||
|
||
You just have to enable it in your `.ladle/config.mjs`: | ||
|
||
```js | ||
/** @type {import('@ladle/react').UserConfig} */ | ||
export default { | ||
addons: { | ||
msw: { | ||
enabled: true, | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
Ladle automatically sets up the MSW service worker for you - copying it over into the public folder. Ladle also re-exports MSW library so you don't have to install it yourself. | ||
|
||
## Usage | ||
|
||
```tsx | ||
import type { Story } from "@ladle/react"; | ||
import { msw } from "@ladle/react"; | ||
import { useEffect, useState } from "react"; | ||
|
||
const FETCH_URL = "/api-url.json"; | ||
|
||
// Set default handlers for all stories | ||
export default { | ||
msw: [ | ||
msw.rest.get(FETCH_URL, (_, res, ctx) => { | ||
return res(ctx.json([{ id: 1, title: "msw post default" }])); | ||
}), | ||
], | ||
}; | ||
|
||
export const Mocked: Story = () => { | ||
const [posts, setPosts] = useState([]); | ||
useEffect(() => { | ||
try { | ||
const data = await fetch(FETCH_URL); | ||
const json = await data.json(); | ||
setPosts(json); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}, []); | ||
return ( | ||
<> | ||
<h1>Posts</h1> | ||
<ul> | ||
{posts.map((post: { id: number; title: string }) => ( | ||
<li key={post.id}>{post.title}</li> | ||
))} | ||
</ul> | ||
</> | ||
); | ||
}; | ||
|
||
// handlers for this story only | ||
// Mocked.msw = []; | ||
``` | ||
|
||
You can also use MSW to mock GraphQL requests. Check their [documentation](https://mswjs.io/docs/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ module.exports = { | |
"background", | ||
"controls", | ||
"links", | ||
"msw", | ||
"source", | ||
"width", | ||
], | ||
|