How to use the fetch-mock.patch 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
'/test/succeeds/json-one',
  () =>
    new Promise(resolve => {
      resolve(jsonResponse());
    })
);

fetchMock.get(
  '/test/succeeds/json-two',
  () =>
    new Promise(resolve => {
      resolve(jsonResponse2());
    })
);

fetchMock.patch(
  '/test/succeeds/patch',
  () =>
    new Promise(resolve => {
      resolve(jsonResponse3());
    })
);

// We do this at the start of each test, just in case a test
// replaces the global fetch and does not reset it
beforeEach(() => {
  fetchMock.reset();
});
github Palindrom / Palindrom / test / specs / client / reconnect.js View on Github external
remoteUrl: getTestURL('testURL'),
                    ot: true,
                    localVersionPath,
                    remoteVersionPath,
                    onReconnectionCountdown,
                    onReconnectionEnd,
                    useWebSocket: true,

                    pingIntervalS
                });
                fetchMock.patch(getTestURL('testURL'), {
                    status: 200,
                    headers: { contentType: 'application/json-patch+json' },
                    body: '[]'
                }, {name: 'ping'});
                fetchMock.patch(getTestURL('testURL')+'/reconnect', {
                    status: 200,
                    headers: { contentType: 'application/json' },
                    body: `{"witaj": "swiecie", "${remoteVersion}": 777}`
                }, {name: 'reconnect'});
                // wait for HTTP responses and WebSocket server to be established
                await sleep();
                // issue a change that will not get acknoledged by server
                palindrom.obj.hello = 'OT';
                // close the WS
                mockSocketServer.close({
                    code,
                    wasClean,
                    reason
                });
                // setup another web socket so reconnection would not throw an error
                anotherSocket = new MockSocketServer(getTestURL('testURL', false, true));
github Palindrom / Palindrom / test / specs / client / reconnect.js View on Github external
}, {name: 'establish'});
                
                onReconnectionCountdown = sinon.spy();
                onReconnectionEnd = sinon.spy();
                palindrom = new Palindrom({
                    remoteUrl: getTestURL('testURL'),
                    ot: true,
                    localVersionPath,
                    remoteVersionPath,
                    onReconnectionCountdown,
                    onReconnectionEnd,
                    useWebSocket: true,

                    pingIntervalS
                });
                fetchMock.patch(getTestURL('testURL'), {
                    status: 200,
                    headers: { contentType: 'application/json-patch+json' },
                    body: '[]'
                }, {name: 'ping'});
                fetchMock.patch(getTestURL('testURL')+'/reconnect', {
                    status: 200,
                    headers: { contentType: 'application/json' },
                    body: `{"witaj": "swiecie", "${remoteVersion}": 777}`
                }, {name: 'reconnect'});
                // wait for HTTP responses and WebSocket server to be established
                await sleep();
                // issue a change that will not get acknoledged by server
                palindrom.obj.hello = 'OT';
                // close the WS
                mockSocketServer.close({
                    code,
github werwolfby / monitorrent / test / unit / specs / api / api.settings.spec.js View on Github external
it(`'updateNewVersionChecker' should update value on backend`, async () => {
            const mock = fetchMock.patch(`/api/settings/new-version-checker`, {status: 204})

            await api.settings.updateNewVersionChecker({enabled: false, include_prerelease: true, interval: 60})

            expect(mock.called())
            expect(JSON.parse(mock.lastCall()[1].body)).to.be.eql({enabled: false, include_prerelease: true, interval: 3600})
        })
github Rsullivan00 / apollo-link-json-api / src / __tests__ / jsonApiLink.ts View on Github external
it('supports PATCH requests', async () => {
      expect.assertions(2);

      const link = new JsonApiLink({ uri: '/api' });

      const post = {
        data: {
          type: 'posts',
          id: '1',
          attributes: { title: 'Love apollo', categoryId: 6 },
        },
      };
      fetchMock.patch('/api/posts/1', post);
      const resultPost = {
        __typename: 'posts',
        id: '1',
        title: 'Love apollo',
        categoryId: 6,
      };

      const editPostMutation = gql`
        fragment PartialPostInput on REST {
          id: ID
          title: String
          categoryId: Number
        }

        mutation editPost($id: ID!, $input: PartialPostInput!) {
          editedPost(id: $id, input: $input)
github Palindrom / Palindrom / test / specs / client / heartbeat.js View on Github external
body: '{"hello": "world"}'
                }, {name: 'establish'});
                
                onReconnectionCountdown = sinon.spy();
                onReconnectionEnd = sinon.spy();
                palindrom = new Palindrom({
                    remoteUrl: getTestURL('testURL'),
                    ot: true,
                    localVersionPath,
                    remoteVersionPath,
                    onReconnectionCountdown,
                    onReconnectionEnd,

                    pingIntervalS
                });
                fetchMock.patch(getTestURL('testURL'), {
                    status: 200,
                    headers: { contentType: 'application/json-patch+json' },
                    body: '[]'
                }, {
                    name: 'ping',
                    functionMatcher: function(url, options){
                        return options.body === '[]';
                    }
                });
                fetchMock.patch(getTestURL('testURL'), {
                    status: 200,
                    headers: { contentType: 'application/json-patch+json' },
                    body: `[
                        {"op": "replace", "path": "${remoteVersionPath}", "value": 100}, 
                        {"op": "test", "path": "${localVersionPath}", "value": 1}
                    ]`
github jpodwys / preact-journal / client / js / actions / index.spec.js View on Github external
it('should handle a failure to update an entry from client to server', (done) => {
        fetchMock.get('/api/entries/sync/1234', {
          status: 200,
          body: {
            entries: [],
            timestamp: 4321
          }
        });
        fetchMock.patch('/api/entry/0', 500);

        el.state.loggedIn = true;
        el.state.timestamp = 1234;
        el.state.entries = [ { id: 0, date: '2017-01-01', text: 'what', needsSync: true } ];
        Entry.getEntries(el);
        setTimeout(() => {
          expect(console.log.calledWith('updateEntryFailure')).to.be.true;
          done();
        });
      });
github jpodwys / preact-journal / client / js / actions / index.spec.js View on Github external
it('should sync entries from the client to the server when one or more entries has the needsSync flag', (done) => {
        fetchMock.get('/api/entries/sync/1234', {
          status: 200,
          body: {
            entries: [],
            timestamp: 4321
          }
        });
        fetchMock.post('/api/entry', {
          status: 200,
          body: { id: 3 }
        });
        fetchMock.patch('/api/entry/1', 204);
        fetchMock.delete('/api/entry/2', 204);

        el.state.loggedIn = true;
        el.state.timestamp = 1234;
        el.state.entries = [
          { id: 0, date: '2018-01-01', text: 'bogus', newEntry: true, needsSync: true  },
          { id: 1, date: '2017-01-01', text: 'what', needsSync: true },
          { id: 2, deleted: true, needsSync: true },
        ];
        Entry.getEntries(el);
        setTimeout(() => {
          const entries = el.state.entries;
          expect(entries.length).to.equal(2);
          expect(entries[0].id).to.equal(3);
          expect(entries[0].newEntry).to.be.undefined;
          expect(entries[0].needsSync).to.be.undefined;
github jpodwys / preact-journal / client / js / actions / index.spec.js View on Github external
it('should set state.entry.text, mark state.entries[entryIndex] for update, and remove the needsSync flag when the network call succeedes', (done) => {
        fetchMock.patch('/api/entry/0', 204);
        const updateObj = {
          entry: { text: 'b' },
          property: 'text',
          entryId: 0
        };
        Entry.updateEntry(el, updateObj);

        const firstCallArgs = el.set.args[0];
        expect(firstCallArgs[0].entry.text).to.equal(updateObj.entry.text);
        expect(firstCallArgs[0].entry.needsSync).to.be.true;
        expect(firstCallArgs[0].entries[0].text).to.equal(updateObj.entry.text);
        expect(firstCallArgs[0].entries[0].needsSync).to.be.true;

        setTimeout(() => {
          const secondCallArgs = el.set.args[1];
          expect(secondCallArgs[0].entry.text).to.equal(updateObj.entry.text);
github jpodwys / preact-journal / client / js / services / index.spec.js View on Github external
it('should call the update endpoint with an entry object', (done) => {
      fetchMock.patch('/api/entry/1234', 204);
      Entry.update(1234, fakeEntry).then(() => {
        const options = fetchMock.lastOptions();
        expect(typeof options.body).to.equal('string');
        expect(JSON.parse(options.body).date).to.equal('1234');
        done();
      }).catch(done);
    });