mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-08 11:11:53 +08:00
69 lines
1.5 KiB
Plaintext
69 lines
1.5 KiB
Plaintext
```ts
|
|
// YourPage.stories.ts|tsx
|
|
// Replace your-framework with the name of your framework (e.g. nextjs, vue3-vite)
|
|
import type { Meta, StoryObj } from '@storybook/your-framework';
|
|
|
|
import { http, HttpResponse, delay } from 'msw';
|
|
|
|
import { MyComponent } from './MyComponent';
|
|
|
|
const meta = {
|
|
component: DocumentScreen,
|
|
} satisfies Meta<typeof MyComponent>;
|
|
|
|
export default meta;
|
|
type Story = StoryObj<typeof meta>;
|
|
|
|
// 👇 The mocked data that will be used in the story
|
|
const TestData = {
|
|
user: {
|
|
userID: 1,
|
|
name: 'Someone',
|
|
},
|
|
document: {
|
|
id: 1,
|
|
userID: 1,
|
|
title: 'Something',
|
|
brief: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
|
status: 'approved',
|
|
},
|
|
subdocuments: [
|
|
{
|
|
id: 1,
|
|
userID: 1,
|
|
title: 'Something',
|
|
content:
|
|
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
|
|
status: 'approved',
|
|
},
|
|
],
|
|
};
|
|
|
|
export const MockedSuccess: Story = {
|
|
parameters: {
|
|
msw: {
|
|
handlers: [
|
|
http.get('https://your-restful-endpoint/', () => {
|
|
return HttpResponse.json(TestData);
|
|
}),
|
|
],
|
|
},
|
|
},
|
|
};
|
|
|
|
export const MockedError: Story = {
|
|
parameters: {
|
|
msw: {
|
|
handlers: [
|
|
http.get('https://your-restful-endpoint', async () => {
|
|
await delay(800);
|
|
return new HttpResponse(null, {
|
|
status: 403,
|
|
});
|
|
}),
|
|
],
|
|
},
|
|
},
|
|
};
|
|
```
|