Socket
Book a DemoInstallSign in
Socket

@fiverr/react-fetch-mock-provider

Package Overview
Dependencies
Maintainers
6
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fiverr/react-fetch-mock-provider

Mock http requests made using fetch from your react component

0.1.0
latest
Source
npmnpm
Version published
Weekly downloads
1
Maintainers
6
Weekly downloads
 
Created
Source

react-fetch-mock-provider

Example

import FetchMockProvider from '@fiverr/react-fetch-mock-provider';

<FetchMockProvider mocks={[{
    request: {
        url: /users/,
        method: 'GET'
    },
    response: {
        body: {
            users: {
                1: {id: 1, name: 'one'},
                2: {id: 2, name: 'two'},
                3: {id: 3, name: 'three'},
                4: {id: 4, name: 'four'},
                5: {id: 5, name: 'five'},
            }
        },
        status: 200
    }}]}>
    <Users/>
</FetchMockProvider>

response props can also be a function that will receive the fetch request params and should return the same object shown above that includes body, status.

Functinal Examples

import FetchMockProvider from '@fiverr/react-fetch-mock-provider';

<FetchMockProvider mocks={[{
    request: {
        url: /users/,
        method: 'GET'
    },
    response: (request) => ({
        // Build server response based on the GET param in the url!
        body: getResponseByAttachmentsIDsParam(request.split('attachment_id=')[1]),
        status: 200
    })}]}>
    <Users/>
</FetchMockProvider>
import FetchMockProvider from '@fiverr/react-fetch-mock-provider';

<FetchMockProvider mocks={[{
    request: {
        url: /users/,
        method: 'POST'
    },
    response: (request, payload) => {
        const {comments} = JSON.parse(payload.body);
        // Build server keys for comments based on the POST payload!
        const keysMapping = reduce(comments, (acc, comment) => {
            acc[comment.id] = uid();
            return acc;
        }, {});

        return {
            body: keysMapping,
            status: 200
        };
    }}]}>
    <Users/>
</FetchMockProvider>

Keywords

react

FAQs

Package last updated on 01 Jan 2019

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.