storybook/docs/snippets/angular/documentscreen-story-msw-graphql-query.ts.mdx
2021-07-08 22:46:28 +01:00

107 lines
2.6 KiB
Plaintext

```ts
// YourPage.stories.ts
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { Story, Meta, moduleMetadata } from '@storybook/angular';
import { graphql } from 'msw';
import DocumentScreen from './YourPage.component';
import DocumentList from './DocumentList.component';
import DocumentHeader from './DocumentHeader.component';
import PageLayout from './PageLayout.component';
import { MockGraphQLModule } from './mock-graphql.module';
export default {
component: DocumentScreen,
decorators: [
moduleMetadata({
declarations: [DocumentList, DocumentHeader, PageLayout],
imports: [CommonModule, HttpClientModule, MockGraphQLModule],
}),
],
title: 'Mock GraphQL query with Storybook and MSW',
} as 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',
},
{
id: 2,
userID: 1,
title: 'Something else',
content:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
status: 'awaiting review',
},
{
id: 3,
userID: 2,
title: 'Another document',
content:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
status: 'approved',
},
{
id: 4,
userID: 2,
title: 'Something',
content:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
status: 'approved',
},
],
};
const PageTemplate: Story<DocumentScreen> = (args) => ({
props: args,
});
export const MockedSuccess = PageTemplate.bind({});
MockedSuccess.parameters = {
msw: [
graphql.query('AllInfoQuery', (req, res, ctx) => {
return res(ctx.data(TestData));
}),
],
};
export const MockedError = PageTemplate.bind({});
MockedError.parameters = {
msw: [
graphql.query('AllInfoQuery', (req, res, ctx) => {
return res(
ctx.delay(800),
ctx.errors([
{
message: 'Access denied',
},
])
);
}),
],
};
```