Skip to content
Branch: master
Find file History
skidding Publish
 - @react-mock/fetch@0.3.0
 - @react-mock/localstorage@0.1.2
 - @react-mock/state@0.1.6
 - @react-mock/xhr@0.2.0
Latest commit 507c07d Oct 15, 2018
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
src
.npmignore Ignore setup-global-fetch.js Oct 12, 2018
README.md feat: Add support for multiple Fetch/XHR mocks (#11) Oct 15, 2018
package.json
setup-global-fetch.js feat: Fetch mock (#4) Oct 12, 2018

README.md

Fetch requests

A declarative wrapper for the wonderful fetch-mock.

Note: FetchMock mocks the global Fetch API, so only one FetchMock instance should be rendered at once.

import { FetchMock } from '@react-mock/fetch';

// Passing fetch-mock options
render(
  <FetchMock options={{ matcher: '/login', response: 401, method: 'POST' }}>
    <MyComponent />
  </FetchMock>
);

// Passing fetch-mock config
render(
  <FetchMock
    matcher="/posts"
    response={200}
    config={{ fallbackToNetwork: true }}
  >
    <MyComponent />
  </FetchMock>
);

Multiple mocks

render(
  <FetchMock
    mocks={[
      { matcher: '/users', response: [{ id: 123 }] },
      { matcher: '/user/123', response: { name: 'Jessica' } }
    ]}
  >
    <MyComponent />
  </FetchMock>
);

Inspection

See fetch-mock's inspection methods to check how fetch was called.

Note: Import fetchMock from @react-mock/fetch to ensure you're inspecting on the right fetch-mock instance.

import { fetchMock } from '@react-mock/fetch';

const [, { body }] = fetchMock.lastCall('/login', 'POST');
expect(JSON.parse(body)).toEqual({ user: 'harry' });
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.