Security News
TC39 Advances 10+ ECMAScript Proposals: Key Features to Watch
TC39 is meeting in Tokyo this week and they have approved nearly a dozen proposals to advance to the next stages.
storybook-addon-mock
Advanced tools
storybook-addon-mock is an addon for Storybook that allows you to mock API requests and responses. This is particularly useful for developing and testing UI components in isolation without relying on a live backend.
Mocking API Requests
This feature allows you to mock API requests and responses. In the code sample, the MockedProvider decorator is used to wrap the component, and mockData is provided to simulate an API response for a GET request to '/api/data'.
json
{
"title": "Mocking API Requests",
"code": "import { MockedProvider } from 'storybook-addon-mock';\nimport { storiesOf } from '@storybook/react';\nimport MyComponent from './MyComponent';\n\nstoriesOf('MyComponent', module)\n .addDecorator(MockedProvider)\n .add('default', () => <MyComponent />, {\n mockData: [\n {\n url: '/api/data',\n method: 'GET',\n status: 200,\n response: { data: 'mocked data' }\n }\n ]\n });",
"description": "This feature allows you to mock API requests and responses. In the code sample, the MockedProvider decorator is used to wrap the component, and mockData is provided to simulate an API response for a GET request to '/api/data'."
}
Mocking Multiple Endpoints
This feature allows you to mock multiple API endpoints. In the code sample, two endpoints are mocked: a GET request to '/api/data' and a POST request to '/api/other'.
json
{
"title": "Mocking Multiple Endpoints",
"code": "import { MockedProvider } from 'storybook-addon-mock';\nimport { storiesOf } from '@storybook/react';\nimport MyComponent from './MyComponent';\n\nstoriesOf('MyComponent', module)\n .addDecorator(MockedProvider)\n .add('default', () => <MyComponent />, {\n mockData: [\n {\n url: '/api/data',\n method: 'GET',\n status: 200,\n response: { data: 'mocked data' }\n },\n {\n url: '/api/other',\n method: 'POST',\n status: 201,\n response: { result: 'success' }\n }\n ]\n });",
"description": "This feature allows you to mock multiple API endpoints. In the code sample, two endpoints are mocked: a GET request to '/api/data' and a POST request to '/api/other'."
}
Conditional Mocking
This feature allows you to conditionally mock API responses based on the request. In the code sample, the response for the GET request to '/api/data' is determined by the query parameter 'id'.
json
{
"title": "Conditional Mocking",
"code": "import { MockedProvider } from 'storybook-addon-mock';\nimport { storiesOf } from '@storybook/react';\nimport MyComponent from './MyComponent';\n\nstoriesOf('MyComponent', module)\n .addDecorator(MockedProvider)\n .add('default', () => <MyComponent />, {\n mockData: [\n {\n url: '/api/data',\n method: 'GET',\n status: 200,\n response: (req) => {\n if (req.query.id === '1') {\n return { data: 'mocked data for id 1' };\n }\n return { data: 'default mocked data' };\n }\n }\n ]\n });",
"description": "This feature allows you to conditionally mock API responses based on the request. In the code sample, the response for the GET request to '/api/data' is determined by the query parameter 'id'."
}
Mock Service Worker (msw) is a library for mocking network requests in both browser and Node.js environments. It intercepts requests on the network level, providing more flexibility and control over the mocked responses. Compared to storybook-addon-mock, msw can be used outside of Storybook and offers more advanced features for request interception and mocking.
fetch-mock is a library for mocking fetch requests. It allows you to define how fetch calls should be handled, providing a way to simulate different network conditions and responses. While fetch-mock is not specifically designed for Storybook, it can be integrated into Storybook stories to achieve similar functionality to storybook-addon-mock.
axios-mock-adapter is a library for mocking axios requests. It allows you to define how axios requests should be handled, providing a way to simulate different network conditions and responses. Like fetch-mock, axios-mock-adapter is not specifically designed for Storybook but can be used within Storybook stories to mock API requests.
This addon allows you to mock fetch or XMLHttprequest requests in storybook. If your component depends on backend requests, and your backend requests are not ready yet to feed your component, this addon provides mock response to build your component.
There are few packages those help the developers to mock the backend requests while building components. But those packages aren't integrated properly in storybook and also there's no scope to play with those requests in the storybook. Mostly, there's no playground to modify the response and see the changes in the storybook.
storybook-addon-mock
provides the following features.
Older(2.*) version documentation
This project is licensed under the MIT License - see the LICENSE file in the source code for details.
FAQs
A storybook addon to mock fetch/XHR request
The npm package storybook-addon-mock receives a total of 132,800 weekly downloads. As such, storybook-addon-mock popularity was classified as popular.
We found that storybook-addon-mock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 is meeting in Tokyo this week and they have approved nearly a dozen proposals to advance to the next stages.
Security News
Our threat research team breaks down two malicious npm packages designed to exploit developer trust, steal your data, and destroy data on your machine.
Security News
A senior white house official is urging insurers to stop covering ransomware payments, indicating possible stricter regulations to deter cybercrime.