Socket
Socket
Sign inDemoInstall

storybook-addon-mock

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

storybook-addon-mock

A storybook addon to mock fetch/XHR request


Version published
Weekly downloads
166K
decreased by-7.1%
Maintainers
1
Weekly downloads
 
Created

What is storybook-addon-mock?

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.

What are storybook-addon-mock's main functionalities?

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'."
}

Other packages similar to storybook-addon-mock

Keywords

FAQs

Package last updated on 15 Apr 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc