How to use the fetch-mock.put function in fetch-mock

To help you get started, we’ve selected a few fetch-mock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jamesplease / react-request / test / setup.js View on Github external
Enzyme.configure({ adapter: new Adapter() });

// We need an AbortSignal that can be instantiated without
// an error.
global.AbortSignal = function() {};

var hangingPromise = (global.hangingPromise = function() {
  return new Promise(() => {});
});

fetchMock.get('/test/hangs', hangingPromise());
fetchMock.get('/test/hangs/1', hangingPromise());
fetchMock.get('/test/hangs/2', hangingPromise());
fetchMock.post('/test/hangs', hangingPromise());
fetchMock.put('/test/hangs', hangingPromise());
fetchMock.patch('/test/hangs', hangingPromise());
fetchMock.head('/test/hangs', hangingPromise());
fetchMock.delete('/test/hangs', hangingPromise());

// This could be improved by adding the URL to the JSON response
fetchMock.get('/test/succeeds', () => {
  return new Promise(resolve => {
    resolve(jsonResponse());
  });
});

fetchMock.get(
  '/test/succeeds/cache-only-empty',
  () =>
    new Promise(resolve => {
      resolve(successfulResponse());
github coalescejs / coalesce / tests / acceptance / simple-hierarchy.spec.js View on Github external
expect(parentNewPost.id).to.eq(newPost.id);

    //
    // Updating
    //
    session = parentSession.child();
    let user2 = session.fetchBy(User, { id: 7 });
    newPost = session.get(newPost);

    newPost.title = 'Updated Title';
    newPost.user = user2;
    let promise = session.flush();

    expect(parentSession.get(newPost).title).to.eq('New Post');

    fetchMock.put('/posts/3', (url, { body }) => {
      body = JSON.parse(body);
      expect(body.title).to.eq('Updated Title');
      expect(body.user).to.eq(7);
      return JSON.stringify({
        type: 'post',
        id: 3,
        rev: 2,
        title: 'Updated Title',
        user: 7
      });
    });
    await promise;

    expect(newPost.title).to.eq('Updated Title');
    expect(newPost.user).to.eq(user2);
    expect(parentSession.get(newPost).title).to.eq('Updated Title');
github redradix / redux-base-app / src / mocks / endpoints.js View on Github external
const size = 3

/* eslint indent: 0 */
// NOTE: This was commented to deploy the application with mock data.
// TODO: Remove after api endpoints are working properly
// if (process.env.NODE_ENV === 'development') {
  fetchMock.get(new RegExp('/api/data/initial'), {data: {}})
  fetchMock.get(new RegExp('/api/session'),  {type: 'session', data: {user: {name: 'miguel', surname: 'martin', email: 'a@a.com', role: 'admin'}}})
  fetchMock.get(new RegExp('/api/users/\\d+'), function() {
    return {
      type: 'user',
      data: users[0]
    }
  })
  fetchMock.put(new RegExp('/api/users/\\d+'), function() {
    return {
      type: 'user',
      data: users[0]
    }
  })
  fetchMock.get(new RegExp('/api/user/list'), function(url) {
    const [, search] = url.split('?')
    let page = 0
    if (search) page = parseInt(search.split('=')[1], 10)
    const start = page * size
    const end = start + size
    return {
      type: 'list',
      data: {
        users: users.slice(start, end),
        pagination: {
github redradix / redux-base-app / src / mocks / index.js View on Github external
import fetchMock from 'fetch-mock'
import realFetch from 'isomorphic-fetch'

import { endpoint as usersEndpoint, getUser, getUsers, createUser, updateUser, deleteUser } from 'mocks/endpoints/users'

if (process.env.NODE_ENV === 'development') {
  // users endpoints
  fetchMock.get(new RegExp(`${usersEndpoint}/\\d+/?$`), getUser)
  fetchMock.get(new RegExp(`${usersEndpoint}/?`), getUsers)
  fetchMock.post(new RegExp(`${usersEndpoint}/?$`), createUser)
  fetchMock.put(new RegExp(`${usersEndpoint}/\\d+/?$`), updateUser)
  fetchMock.delete(new RegExp(`${usersEndpoint}/\\d+/?$`), deleteUser)
  // misc
  fetchMock.get(new RegExp('/api/data/initial'), {data: {}})
  fetchMock.get(new RegExp('/api/session'),  {type: 'session', data: {user: {name: 'miguel', surname: 'martin', email: 'a@a.com', role: 'admin'}}})
  fetchMock.delete(new RegExp('/api/session'), {type: 'session', data: []})
  fetchMock.post(new RegExp('/api/session'), {type: 'session', data: {token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoibWlndWVsIiwic3VybmFtZSI6Im1hcnRpbiIsImVtYWlsIjoiYUBhLmNvbSIsImlhdCI6MTQ4NzcwMTEyOCwiZXhwIjoxNDg3NzI5OTI4fQ.SUUccKC13c_gdlxUf5FN1o4xeIxF9lyWSJNn3N0PNiw'}})
  .catch((unmatchedUrl, options) => {
    return realFetch(unmatchedUrl, options)
  })
}
github nano3labs / fetch-redux-crud / tests / actions.spec.js View on Github external
it('creates PUT request and dispatches actions', () => {
      fetchMock.put(`${apiUrl}/photos`, { photo: { id: 1, some_attr: 'yoooO123' } })

      const expectedActions = [
        {
          type: actionTypes.updateStart,
          data: undefined,
          record: {
            id: 1, someAttr: 'yoooO123'
          }
        },
        {
          type: actionTypes.updateSuccess,
          data: 1,
          record: {
            id: 1, someAttr: 'yoooO123'
          }
        }
github coralproject / talk / test / client / coral-admin / actions / assets.js View on Github external
it('should update an asset', () => {

      fetchMock.put('*', JSON.stringify(assets[0]));

      return actions.updateAssetState('123', 'status', 'open')(store.dispatch)
        .then(() => {
          expect(store.getActions()[0]).to.have.property('type', 'UPDATE_ASSET_STATE_REQUEST');
          expect(store.getActions()[1]).to.have.property('type', 'UPDATE_ASSET_STATE_SUCCESS');
        });

    });
github werwolfby / monitorrent / test / unit / specs / api / api.trackers.spec.js View on Github external
it(`'save' should throw NotFound error on 404 error`, async () => {
            const responseError = {title: 'NotFound', description: `Page not found`}
            fetchMock.put(`/api/trackers/tracker1.com`, {status: 404, body: responseError})

            const model = {username: 'username', password: 'password'}
            const error = await expect(api.trackers.save('tracker1.com', model)).to.eventually.rejectedWith(Error)

            expect(error.message).to.be.equal('NotFound')
            expect(error.description).to.be.equal('Page not found')
        })
github coalescejs / coalesce / tests / adapter.spec.js View on Github external
beforeEach(function() {
        fetchMock.put('/posts/1', JSON.stringify({ type: 'post', id: 1, title: 'More Persistent' }));
      });
github gilbarbara / react-spotify-web-playback / test / index.spec.tsx View on Github external
};

    fetchMock.get(/contains\?ids=*/, [true]);
    fetchMock.get('https://api.spotify.com/v1/me/player/devices', {
      devices: [
        {
          id: externalDeviceId,
          name: 'Jest Player',
        },
      ],
    });
    fetchMock.get('https://api.spotify.com/v1/me/player', () => playerStatusResponse);
    fetchMock.delete('https://api.spotify.com/v1/me/tracks', 200);
    fetchMock.put('*', 204);
    fetchMock.post('*', 204);
    fetchMock.put('https://api.spotify.com/v1/me/tracks', 200);
  });
github Flexget / webui / src / plugins / lists / base / EntryList / EntryCard.spec.tsx View on Github external
beforeEach(() => {
      fetchMock
        .put(`glob:/api/${prefix}/1/entries/*`, {})
        .delete(`glob:/api/{prefix}/1/entries/*`, {})
        .get('/api/tasks', 200)
        .catch();
    });