Skip to content

Commit

Permalink
FIX: Ensure all arguments are passed to base fetch function
Browse files Browse the repository at this point in the history
This is not a great fix; it ignores that method is only inspected when it's
in the first fetch() parameter, and it glosses over the fact that the
navigator object, for historical reasons, is very constrained in the
responses it gives (which is why the code snagged jsdom in the first
place).

It does, however, solve my immediate need of getting fetch-mock to
interoperate with jsdom.

Fixes wheresrhys#608
  • Loading branch information
Brian Crowell committed May 15, 2021
1 parent 4b54e6d commit 6154fe6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib/fetch-handler.js
Expand Up @@ -31,11 +31,11 @@ const patchNativeFetchForSafari = (nativeFetch) => {
return nativeFetch;
}
// It seems the code is working on Safari thus patch native fetch to avoid the error.
return async (request) => {
return async (request, ...args) => {
const { method } = request;
if (!['POST', 'PUT', 'PATCH'].includes(method)) {
// No patch is required in this case
return nativeFetch(request);
return nativeFetch(request, ...args);
}
const body = await request.clone().text();
const {
Expand Down
41 changes: 41 additions & 0 deletions test/specs/config/safari.test.js
@@ -0,0 +1,41 @@
const chai = require('chai');
const expect = chai.expect;

const { fetchMock, theGlobal } = testGlobals;

describe('Safari override', () => {
beforeEach(() => {
fetchMock.createInstance();
});

it('passes all GET arguments to next function when not under Safari', async () => {
theGlobal.fetch = async (...args) => {
expect(args[0]).to.equal('http://mocked.com/');
expect(args[1]).to.deep.equal({ method: 'GET' });
return { status: 202 };
};
fetchMock.spy('http://mocked.com');
const res = await fetchMock.fetchHandler('http://mocked.com', {
method: 'GET',
});
expect(res.status).to.equal(202);
fetchMock.restore();
delete theGlobal.fetch;
});

it('passes all GET arguments to next function under Safari', async () => {
theGlobal.navigator = { vendor: 'Apple Computer, Inc.' };
theGlobal.fetch = async (...args) => {
expect(args[0]).to.equal('http://mocked.com/');
expect(args[1]).to.deep.equal({ method: 'GET' });
return { status: 202 };
};
fetchMock.spy('http://mocked.com');
const res = await fetchMock.fetchHandler('http://mocked.com', {
method: 'GET',
});
expect(res.status).to.equal(202);
fetchMock.restore();
delete theGlobal.fetch;
});
});

0 comments on commit 6154fe6

Please sign in to comment.