@fiverr/react-fetch-mock-provider

0.1.0 • Public • Published

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>

Readme

Keywords

Package Sidebar

Install

npm i @fiverr/react-fetch-mock-provider

Weekly Downloads

75

Version

0.1.0

License

MIT

Unpacked Size

553 kB

Total Files

4

Last publish

Collaborators

  • fiverrit
  • omrilotan
  • michael5r