How to use jest-fetch-mock - 10 common examples

To help you get started, we’ve selected a few jest-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 yuriykuzin / crowdin-helper / test / commands / delete-old-branches.spec.js View on Github external
test('', async () => {
    mockedChildProcess.__setResponse('git rev-parse --abbrev-ref HEAD', 'feature/my-feature-branch');

    const deleteOldBranches = require('../../lib/commands/delete-old-branches');

    mockedChildProcess.__setResponse('git fetch', ' ');
    mockedChildProcess.__setResponse('git log -1 --format=%cd origin/master', 'Fri May 4 10:40:15 2018 +0300');
    mockedChildProcess.__setResponse('git ls-remote --heads', 'refs/heads/master');

    mockedFetch.mockResponses(
      [
        // https://api.crowdin.com/api/project/info
        JSON.stringify({
          files: [
            {
              node_type: 'branch',
              name: 'sample-old-branch',
              files: [{
                node_type: 'file',
                name: 'sample-file-name.json',
                last_updated: 'Fri May 4 10:40:15 2018 +0300'
              }]
            }
          ]
        }),
        { status: 200 }
github yuriykuzin / crowdin-helper / test / commands / upload-sources.spec.js View on Github external
test('should perform upload source and then download and unzip translation', async () => {
    mockedChildProcess.__setResponse('git rev-parse --abbrev-ref HEAD', 'feature/my-feature-branch');

    const uploadSources = require('../../lib/commands/upload-sources');
    expect(consoleData.indexOf('Working on git branch: feature/my-feature-branch') !== -1).toBeTruthy();
    consoleData = '';

    mockedFetch.mockResponses(
      [
        // https://api.crowdin.com/api/project/my-project-name/add-directory
        JSON.stringify({ success: true }),
        { status: 200 }
      ],
      [
        // https://api.crowdin.com/api/project/my-project-name/add-directory
        JSON.stringify({ success: true }),
        { status: 200 }
      ],
      [
        // https://api.crowdin.com/api/project/my-project-name/add-file
        JSON.stringify({ success: true }),
        { status: 200 }
      ],
      [
github yuriykuzin / crowdin-helper / test / commands / download-translations.spec.js View on Github external
test('should perform upload source and then download and unzip translation', async () => {
    mockedChildProcess.__setResponse('git rev-parse --abbrev-ref HEAD', 'feature/my-feature-branch');

    const downloadTranslations = require('../../lib/commands/download-translations');
    expect(consoleData.indexOf('Working on git branch: feature/my-feature-branch') !== -1).toBeTruthy();
    consoleData = '';

    mockedFetch.mockResponses(
      [
        // https://api.crowdin.com/api/project/my-project-name/add-directory
        JSON.stringify({ success: true }),
        { status: 200 }
      ],
      [
        // https://api.crowdin.com/api/project/my-project-name/add-directory
        JSON.stringify({ success: true }),
        { status: 200 }
      ],
      [
        // https://api.crowdin.com/api/project/my-project-name/add-file
        JSON.stringify({ success: true }),
        { status: 200 }
      ],
      [
github yuriykuzin / crowdin-helper / test / commands / trigger-auto-translation.spec.js View on Github external
test('should say "no new phrases" if phrases === 0', async () => {
    mockedChildProcess.__setResponse('git rev-parse --abbrev-ref HEAD', 'feature/my-feature-branch');

    const triggerAutoTranslation = require('../../lib/commands/trigger-auto-translation');
    expect(consoleData.indexOf('Working on git branch: feature/my-feature-branch') !== -1).toBeTruthy();
    consoleData = '';

    mockedFetch.mockResponses(
      [
        // https://api.crowdin.com/api/project/pre-translate
        JSON.stringify({
          success: true
        }),
        { status: 200 }
      ]
    );

    await triggerAutoTranslation();

    expect(mockedFetch.mock.calls[0][0]).toEqual('https://api.crowdin.com/api/project/my-project-name/pre-translate');
  });
});
github yuriykuzin / crowdin-helper / test / commands / check-progress-on-branch.spec.js View on Github external
test('should trigger auto translate and then exit on master branch', async () => {
    mockedChildProcess.__setResponse('git rev-parse --abbrev-ref HEAD', 'master');

    const checkProgressOnBranch = require('../../lib/commands/check-progress-on-branch');
    expect(consoleData.indexOf('Working on git branch: master') !== -1).toBeTruthy();
    consoleData = '';

    mockedFetch.mockResponses(
      [
        // https://api.crowdin.com/api/project/pre-translate
        JSON.stringify({
          success: true
        }),
        { status: 200 }
      ]
    );

    await checkProgressOnBranch();

    expect(consoleData.indexOf('No validation performed since it is a master branch') !== -1).toBeTruthy();
    expect(mockedFetch.mock.calls[0][0]).toEqual('https://api.crowdin.com/api/project/my-project-name/pre-translate');
  });
github yuriykuzin / crowdin-helper / test / commands / check-progress-on-branch.spec.js View on Github external
consoleData = '';

    mockedFetch.mockResponses(
      [
        // https://api.crowdin.com/api/project/pre-translate
        JSON.stringify({
          success: true
        }),
        { status: 200 }
      ]
    );

    await checkProgressOnBranch();

    expect(consoleData.indexOf('No validation performed since it is a master branch') !== -1).toBeTruthy();
    expect(mockedFetch.mock.calls[0][0]).toEqual('https://api.crowdin.com/api/project/my-project-name/pre-translate');
  });
github yuriykuzin / crowdin-helper / test / commands / trigger-auto-translation.spec.js View on Github external
expect(consoleData.indexOf('Working on git branch: feature/my-feature-branch') !== -1).toBeTruthy();
    consoleData = '';

    mockedFetch.mockResponses(
      [
        // https://api.crowdin.com/api/project/pre-translate
        JSON.stringify({
          success: true
        }),
        { status: 200 }
      ]
    );

    await triggerAutoTranslation();

    expect(mockedFetch.mock.calls[0][0]).toEqual('https://api.crowdin.com/api/project/my-project-name/pre-translate');
  });
});
github yuriykuzin / crowdin-helper / test / commands / delete-old-branches.spec.js View on Github external
}),
        { status: 200 }
      ],
      [
        // https://api.crowdin.com/api/project/delete-directory
        JSON.stringify({
          success: true
        }),
        { status: 200 }
      ]
    );

    await deleteOldBranches();

    expect(consoleData.indexOf('Branch "sample-old-branch" is removed from crowdin') !== -1).toBeTruthy();
    expect(mockedFetch.mock.calls[1][0])
      .toEqual('https://api.crowdin.com/api/project/my-project-name/delete-directory');
  });
});
github yuriykuzin / crowdin-helper / test / commands / upload-sources.spec.js View on Github external
],
      [
        // https://api.crowdin.com/api/project/my-project-name/add-file
        JSON.stringify({ success: true }),
        { status: 200 }
      ],
      [
        // https://api.crowdin.com/api/project/my-project-name/pre-translate
        JSON.stringify({ success: true }),
        { status: 200 }
      ]
    );

    await uploadSources();

    expect(mockedFetch.mock.calls[2][1].body._streams[7].source._readableState.buffer)
      .toEqual(fs.createReadStream('test/sample-source-file/en.json')._readableState.buffer);

    expect(consoleData.indexOf('Uploading to branch: feature--my-feature-branch') !== -1).toBeTruthy();
    expect(consoleData.indexOf('test/sample-source-file/en.json is uploaded') !== -1).toBeTruthy();
    expect(consoleData.indexOf('Triggering auto-translation of a branch: feature--my-feature-branch') !== -1).toBeTruthy();

    const properApiCallsOrder = [
      'https://api.crowdin.com/api/project/my-project-name/add-directory',
      'https://api.crowdin.com/api/project/my-project-name/add-directory',
      'https://api.crowdin.com/api/project/my-project-name/add-file',
      'https://api.crowdin.com/api/project/my-project-name/pre-translate'
    ];

    properApiCallsOrder.forEach((apiCall, index) => {
      expect(mockedFetch.mock.calls[index][0]).toEqual(apiCall);
    })
github geostyler / geostyler-wfs-parser / src / WfsDataParser.spec.ts View on Github external
}, {
          'type': 'Feature',
          'id': 'osm-busstops.fid-4dec2e09_163976bf70d_-73ab',
          'geometry': {
            'type': 'Point',
            'coordinates': [981048.0170072, 7001258.45667367]
          },
          'geometry_name': 'geometry',
          'properties': {
            'osm_id': 29077474
          }
        }]
      };

      // mock responses of WFS DescribeFeatureType and WFS GetFeature
      fetch.mockResponses([describeFeatureTypeResponse], [JSON.stringify(getFeatureResponse)]);

      const wfsParser = new WfsDataParser();
      const resultPromise = wfsParser.readData({
        url: '//ows.terrestris.de/geoserver/osm/wfs',
        version: '2.0.0',
        typeName: 'osm:osm-country-borders',
        maxFeatures: 1
      });

      const onFullFilled = (result: any) => {
        expect(result).toBeDefined();
        expect(result.schema.type).toEqual('object');
        expect(typeof result.schema.properties).toBe('object');
        expect(result.schema.properties.osm_id).toEqual({
          type: 'number'
        });