fetch-mock
Advanced tools
Comparing version 4.0.1 to 4.1.0
@@ -61,2 +61,3 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.fetchMock = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
opts.url = url; | ||
opts.sendAsJson = config.sendAsJson === undefined ? true : config.sendAsJson; | ||
opts.status = config.status || 200; | ||
@@ -69,3 +70,3 @@ // The ternary operator is to cope with new Headers(undefined) throwing in Chrome | ||
/*eslint-disable*/ | ||
if (config.body != null && (typeof body === 'undefined' ? 'undefined' : _typeof(body)) === 'object') { | ||
if (opts.sendAsJson && config.body != null && (typeof body === 'undefined' ? 'undefined' : _typeof(body)) === 'object') { | ||
/*eslint-enable*/ | ||
@@ -72,0 +73,0 @@ body = JSON.stringify(body); |
@@ -47,2 +47,3 @@ 'use strict'; | ||
opts.url = url; | ||
opts.sendAsJson = config.sendAsJson === undefined ? true : config.sendAsJson; | ||
opts.status = config.status || 200; | ||
@@ -55,3 +56,3 @@ // The ternary operator is to cope with new Headers(undefined) throwing in Chrome | ||
/*eslint-disable*/ | ||
if (config.body != null && (typeof body === 'undefined' ? 'undefined' : _typeof(body)) === 'object') { | ||
if (opts.sendAsJson && config.body != null && (typeof body === 'undefined' ? 'undefined' : _typeof(body)) === 'object') { | ||
/*eslint-enable*/ | ||
@@ -58,0 +59,0 @@ body = JSON.stringify(body); |
{ | ||
"name": "fetch-mock", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Mock http requests made using fetch (or isomorphic-fetch)", | ||
@@ -5,0 +5,0 @@ "main": "src/server.js", |
@@ -32,2 +32,3 @@ # fetch-mock [![Build Status](https://travis-ci.org/wheresrhys/fetch-mock.svg?branch=master)](https://travis-ci.org/wheresrhys/fetch-mock) | ||
* `throws`: If this property is present then a `Promise` rejected with the value of `throws` is returned | ||
* `sendAsJson`: This property determines whether or not the request body should be JSON.stringified before being sent (defaults to true). | ||
* `Function(url, opts)`: A function that is passed the url and opts `fetch()` is called with and that returns any of the responses listed above | ||
@@ -63,3 +64,3 @@ | ||
``` | ||
```js | ||
fetchMock | ||
@@ -111,3 +112,3 @@ .mock('http://domain1', 200) | ||
##### Mockery example | ||
``` | ||
```js | ||
var fetch = require('node-fetch'); | ||
@@ -142,20 +143,21 @@ var fetchMock = require('fetch-mock'); | ||
* `registerRoute()` and `unregisterRoute()` have been removed to simplify the API. Since V3, calls to `.mock()` can be chained, so persisting routes over a series of tests can easily be achieved by means of a beforeEach or helper e.g. | ||
``` | ||
beforeEach(() => { | ||
fetchMock | ||
.mock('http://auth.service.com/user', 200) | ||
.mock('http://mvt.service.com/session', {test1: true, test2: false}) | ||
}); | ||
afterEach(() => { | ||
fetchMock.restore(); | ||
}); | ||
```js | ||
beforeEach(() => { | ||
fetchMock | ||
.mock('http://auth.service.com/user', 200) | ||
.mock('http://mvt.service.com/session', {test1: true, test2: false}) | ||
}); | ||
it('should be possible to augment persistent set of routes', () => { | ||
fetchMock.mock('http://content.service.com/contentid', {content: 'blah blah'}) | ||
page.init(); | ||
expect(fetchMock.called('http://content.service.com/contentid')).to.be.true; | ||
}); | ||
``` | ||
afterEach(() => { | ||
fetchMock.restore(); | ||
}); | ||
it('should be possible to augment persistent set of routes', () => { | ||
fetchMock.mock('http://content.service.com/contentid', {content: 'blah blah'}) | ||
page.init(); | ||
expect(fetchMock.called('http://content.service.com/contentid')).to.be.true; | ||
}); | ||
``` | ||
* Defining two routes with the same name will no longer throw an error (previous implementation was buggy anyway) | ||
* Added `lastCall()`, `lastUrl()` and `lastOptions()` utilities | ||
* Added `lastCall()`, `lastUrl()` and `lastOptions()` utilities |
@@ -41,2 +41,3 @@ 'use strict'; | ||
opts.url = url; | ||
opts.sendAsJson = config.sendAsJson === undefined ? true : config.sendAsJson; | ||
opts.status = config.status || 200; | ||
@@ -49,3 +50,3 @@ // The ternary operator is to cope with new Headers(undefined) throwing in Chrome | ||
/*eslint-disable*/ | ||
if (config.body != null && typeof body === 'object') { | ||
if (opts.sendAsJson && config.body != null && typeof body === 'object') { | ||
/*eslint-enable*/ | ||
@@ -52,0 +53,0 @@ body = JSON.stringify(body); |
@@ -17,5 +17,27 @@ 'use strict'; | ||
}); | ||
}) | ||
}); | ||
describe('request types that only work in the browser', function () { | ||
it('respond with blob', function (done) { | ||
const blob = new Blob(); | ||
fetchMock.mock({ | ||
routes: { | ||
name: 'route', | ||
matcher: 'http://it.at.there/', | ||
response: {body: blob, sendAsJson: false} | ||
} | ||
}); | ||
fetch('http://it.at.there/') | ||
.then(function (res) { | ||
expect(res.status).to.equal(200); | ||
res.blob().then(function (blobData) { | ||
expect(blobData).to.eql(blob); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
require('./spec')(fetchMock, window, window.Request); | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73572
2010
160
61