How to use the fetch-mock.get 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 Rsullivan00 / apollo-link-json-api / src / __tests__ / jsonApiLink.ts View on Github external
it.skip('with correctly encoded params', async () => {
    // expect.assertions(4);

    const link = new JsonApiLink({ uri: '/api' });
    const posts1 = [{ id: '1', title: 'Love apollo' }];
    // This is an invalid URL because it doesn't have an encoded space, this is to prove
    // we didn't encode it if it wasn't prefixed by '?' or '&' ?{args} or &{args}
    fetchMock.get('/api/posts?name=Love apollo', posts1);
    // This URL is legacy (shouldn't be called)
    fetchMock.get('/api/posts?name=Love+apollo', posts1);
    // This URL is correctly encoded
    fetchMock.get('/api/posts?name=Love%20apollo', posts1);
    // Uses a query string & a path replacement
    fetchMock.get('/api/posts/1?comments=5', posts1);

    const nonEncodedQuery = gql`
      query postQuery($name: String) {
        posts(name: $name)
          @jsonapi(type: "Post", path: "/posts?name={args.name}") {
          id
          title
        }
      }
    `;
    const encodedQuery = gql`
      query postQuery($name: String) {
        posts(name: $name) @jsonapi(type: "Post", path: "/posts?{args}") {
          id
github blockstack / blockstack.js / tests / unitTests / src / unitTestsStorage.js View on Github external
'name': 'Ken Liao',
              'apps': {
                'http://localhost:8080': 'https://gaia.blockstack.org/hub/1DDUqfKtQgYNt722wuB4Z2fPC7aiNGQa5R/'
              }
            }
          },
          'signature': 'UyQNZ02kBFHEovbwiGaS-VQd57w9kcwn1Nt3QbW3afEMArg1OndmeplB7lzjMuRCLAi-88lkpQLkFw7LwKZ31Q'
        }
      }
    ]
    /* eslint-enable */
    FetchMock.get(profileUrl, profileContent)

    const fileUrl = 'https://gaia.blockstack.org/hub/1DDUqfKtQgYNt722wuB4Z2fPC7aiNGQa5R/file.json'
    const fileContents = JSON.stringify({ key: 'value' })
    FetchMock.get(fileUrl, fileContents)

    const options = {
      username: 'yukan.id',
      app: 'http://localhost:8080'
    }

    getFile(path, options)
      .then((file) => {
        t.ok(file, 'Returns file content')
        t.same(JSON.parse(file), JSON.parse(fileContents))
      })

    const optionsNoApp = {
      username: 'yukan.id'
    }
github google / skia-buildbot / golden / modules / last-commit-sk / last-commit-sk-demo.ts View on Github external
import './index';

import fetchMock from 'fetch-mock';
import { testOnlySetSettings } from '../settings';
import { $$ } from 'common-sk/modules/dom';
import { exampleStatusData } from './demo_data';

testOnlySetSettings({
  baseRepoURL: 'https://github.com/flutter/flutter',
});

fetchMock.get('/json/v1/trstatus', JSON.stringify(exampleStatusData));

// Now that the mock RPC is setup, create the element
const ele = document.createElement('last-commit-sk');
$$('#container')!.appendChild(ele);
github google / skia-buildbot / golden / modules / changelists-page-sk / changelists-page-sk-demo.ts View on Github external
changelistSummaries_5.offset = 10;
changelistSummaries_5.size = 10;
changelistSummaries_5.total = 15;

const open = deepCopy(changelistSummaries_5);
open.changelists = open.changelists!.slice(0, 3);
open.offset = 0;
open.size = 3;
open.total = 3;

const fakeRpcDelayMillis = 300;

fetchMock.get('/json/v1/changelists?offset=0&size=10', delay(ten, fakeRpcDelayMillis));
fetchMock.get(
    '/json/v1/changelists?offset=0&size=10&active=true', delay(open, fakeRpcDelayMillis));
fetchMock.get(
    '/json/v1/changelists?offset=10&size=10', delay(changelistSummaries_5, fakeRpcDelayMillis));
fetchMock.get('glob:/json/v1/changelists*', delay(empty(), fakeRpcDelayMillis));
fetchMock.get('/json/v1/trstatus', JSON.stringify(exampleStatusData));

// By adding these elements after all the fetches are mocked out, they should load ok.
const newScaf = new GoldScaffoldSk();
newScaf.testingOffline = true;
// Make it the first element in body.
document.body.insertBefore(newScaf, document.body.childNodes[0]);
const page = document.createElement('changelists-page-sk');
page.setAttribute('page_size', '10');
newScaf.appendChild(page);
github n1ru4l / graphql-schema-generator-rest / src / __tests__ / rest-link.js View on Github external
it('can not get an user that does not exist (400)', async () => {
    expect.assertions(1)

    fetchMock.get('/users/2', 400)

    const userProfileQuery = gql`
      query userProfile($id: ID!) {
        userProfile(id: $id)
          @rest(type: "User", route: "/users/:id", params: { id: $id }) {
          __typename
          id
          login
        }
      }
    `

    const link = createRestLink()

    const data = await makePromise(
      execute(link, {
github Rsullivan00 / apollo-link-json-api / src / __tests__ / jsonApiLink.ts View on Github external
it.skip('should work with several layers of nesting', async () => {
    fetchMock.get('/api/posts/1', [posts[0]]);
    fetchMock.get('/api/posts/2', [posts[1]]);
    fetchMock.get('/api/posts/3', []);
    fetchMock.post('/graphql', authors);
    const { clientLink, jsonApiLink, httpLink } = buildLinks();

    const link = from([jsonApiLink, clientLink, httpLink]);

    const combinedQuery = gql`
      query {
        authors {
          id
          lastViewedAuthor @client {
            id @export(as: "id")
            posts
              @jsonapi(type: "[Post]", path: "/posts/{exportVariables.id}") {
              title
github atilafassina / blogium / tests / blogium.js View on Github external
beforeEach(() => {
    fetchMock.get('*', { "hello": "world"});

    global.wrapper = document.createElement('div');
    global.wrapper.setAttribute('class', 'mediumWrap');

    global.moreBtn = document.createElement('button');
    global.moreBtn.id = 'moreBtn';

    document.body.appendChild(global.wrapper);
    document.body.appendChild(global.moreBtn);

    blog = new Blogium({
      host: 'atilafassina.com'
    });
  });
github auru / unity-api / test / call.spec.js View on Github external
test.serial('400 reponse', async t => {
    fetchMock.get(matcher, Response400);

    const result = await callAPI();

    t.true(result instanceof Error, 'instance of Error');
    t.true(result instanceof errorAPI, 'instance of APIError');
    t.deepEqual(result.body, { sample: 'not found'}, 'correct error body');

    fetchMock.restore();
});
github openfun / richie / src / frontend / js / data / useStaticFilters / index.spec.tsx View on Github external
it('includes a course config when requested', async () => {
    fetchMock.get('/api/v1.0/filter-definitions/', staticFilterDefinitions);
    render();

    let filters;
    await act(async () => (filters = await getLatestHookValues()()));
    expect(filters).toEqual({
      ...staticFilterDefinitions,
      courses: {
        base_path: null,
        human_name: 'Courses',
        is_autocompletable: true,
        is_searchable: true,
        name: 'courses',
        position: 99,
      },
    });
  });
github huridocs / uwazi / app / api / auth / specs / routes.spec.js View on Github external
beforeEach(() => {
      const stream = new Readable();
      stream.push('captchaImage');
      stream.push(null);
      backend.restore();
      backend
      .get('http://secret.place.io/captcha', { body: stream, headers: { 'set-cookie': ['connect.ssid: 12n32ndi23j4hsj;'] } }, { sendAsJson: false });
    });