Skip to content

Commit

Permalink
added setImplementations method to use custom fetch utility classes (#…
Browse files Browse the repository at this point in the history
…161)

* added setImplementations method to use custom fetch utility classes

* typo
  • Loading branch information
wheresrhys authored Jan 28, 2017
1 parent 428e3c3 commit fc3741d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ Returns the options for the last matched call to fetch
Set some global config options, which include
* `sendAsJson` [default `true`] - by default fetchMock will convert objects to JSON before sending. This is overrideable fro each call but for some scenarios e.g. when dealing with a lot of array buffers, it can be useful to default to `false`
##### `setImplementations(opts)`
When using non global fetch (e.g. a ponyfill) or an alternative Promise implementation, this will configure fetch-mock to use your chosen implementations. `opts` is an object with one or more of the following properties: `Headers`,`Request`,`Response`,`Promise`. Note that `setImplementations(require('fetch-ponyfill'))` will configure fetch-mock to use all of fetch-ponyfill's classes.
## Troubleshooting and alternative installation
### `fetch` is assigned to a local variable, not a global
Expand Down
10 changes: 6 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const FetchMock = require('./fetch-mock');
const statusTextMap = require('./status-text');
const theGlobal = typeof window !== 'undefined' ? window : self;

FetchMock.setGlobals({
global: theGlobal,
FetchMock.global = theGlobal;
FetchMock.statusTextMap = statusTextMap;

FetchMock.setImplementations({
Promise: Promise,
Request: theGlobal.Request,
Response: theGlobal.Response,
Headers: theGlobal.Headers,
statusTextMap: statusTextMap
Headers: theGlobal.Headers
});

module.exports = new FetchMock()
15 changes: 10 additions & 5 deletions src/fetch-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ FetchMock.prototype.spy = function () {
}

FetchMock.prototype.fetchMock = function (url, opts) {
const Promise = this.Promise || FetchMock.global.Promise;
const Promise = this.Promise || FetchMock.Promise;
let response = this.router(url, opts);

if (!response) {
Expand Down Expand Up @@ -130,7 +130,7 @@ FetchMock.prototype.addRoute = function (route) {


FetchMock.prototype.mockResponse = function (url, responseConfig, fetchOpts) {
const Promise = this.Promise || FetchMock.global.Promise;
const Promise = this.Promise || FetchMock.Promise;

// It seems odd to call this in here even though it's already called within fetchMock
// It's to handle the fact that because we want to support making it very easy to add a
Expand Down Expand Up @@ -269,8 +269,11 @@ FetchMock.prototype.configure = function (opts) {
Object.assign(FetchMock.config, opts);
}

FetchMock.setGlobals = function (globals) {
Object.assign(FetchMock, globals)
FetchMock.setImplementations = FetchMock.prototype.setImplementations = function (implementations) {
FetchMock.Headers = implementations.Headers || FetchMock.Headers;
FetchMock.Request = implementations.Request || FetchMock.Request;
FetchMock.Response = implementations.Response || FetchMock.Response;
FetchMock.Promise = implementations.Promise || FetchMock.Promise;
}

FetchMock.prototype.sandbox = function (Promise) {
Expand All @@ -295,7 +298,9 @@ FetchMock.prototype.sandbox = function (Promise) {
functionInstance.bindMethods();
boundMock = functionInstance.fetchMock;
functionInstance.isSandbox = true;
functionInstance.Promise = Promise;
if (Promise) {
functionInstance.Promise = Promise;
}

return functionInstance;
};
Expand Down
12 changes: 7 additions & 5 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ const stream = require('stream');
const FetchMock = require('./fetch-mock');
const http = require('http');

FetchMock.setGlobals({
global: global,
FetchMock.global = global;
FetchMock.statusTextMap = http.STATUS_CODES;
FetchMock.stream = stream;

FetchMock.setImplementations({
Promise: Promise,
Request: Request,
Response: Response,
Headers: Headers,
stream: stream,
statusTextMap: http.STATUS_CODES
Headers: Headers
});

module.exports = new FetchMock()
55 changes: 55 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,61 @@ module.exports = (fetchMock, theGlobal, Request, Response) => {
}
fetchMock.restore();
});

describe('fetch utility class implementations', () => {
const FetchMock = require('../src/fetch-mock');
['Headers', 'Request', 'Response']
.forEach(className => {
it(`should use configured ${className}`, () => {
const original = FetchMock[className];
sinon.spy(FetchMock, className);
const spy = FetchMock[className];
let callCount = 0;
const custom = function () {
callCount++;
return spy.apply(this, arguments)
};
custom.prototype = original.prototype;
const confObject = {};
confObject[className] = custom
fetchMock.setImplementations(confObject);
expect(FetchMock[className]).to.equal(custom);

fetchMock.mock('http://test.url.com/', {
status: 200,
headers: {
id: 1
}
});
return Promise.all([
fetch('http://test.url.com/'),
fetch(new FetchMock.Request('http://test.url.com/'))
])
.then(() => {
expect(callCount).to.equal(spy.args.length);
confObject[className] = original;
fetchMock.restore();
fetchMock.setImplementations(confObject);
})
})

})
});

it('can be configured to use alternate Promise implementations', () => {
fetchMock.setImplementations({
Promise: BluebirdPromise
});
fetchMock
.mock('http://example.com', 200)
const fetchCall = fetch('http://example.com');
expect(fetchCall).to.be.instanceof(BluebirdPromise);
return fetchCall.then(() => {
fetchMock.restore();
fetchMock.setImplementations({Promise});
})

});
})

describe('sandbox', () => {
Expand Down

0 comments on commit fc3741d

Please sign in to comment.