From 6398f0a6988a6c5b0ed554ddc00cf4668aa052dc Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Wed, 21 Oct 2015 23:41:17 +0100 Subject: [PATCH] rethink of API ot handle method --- README.md | 5 +++-- src/fetch-mock.js | 21 +++++++++++---------- test/spec.js | 16 ++++++++++++---- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c724dd71..29c67949 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Replaces `fetch()` with a stub which records it's calls, grouped by route, and o > ##### *Shorthand notation for simplest use cases* The following are also accepted by mock() and translated into `config` objects with the `routes` property defined using the values passed in to mock as follows: -* `mock(name, matcher, response)` - configuration for a single named route to be mocked +* `mock(matcher, method, response)` - configuration for a single unnamed route to be mocked. To access details of its calls `fetchMock.calls()` should be called without passing a parameter * `mock(matcher, response)` - configuration for a single unnamed route to be mocked. To access details of its calls `fetchMock.calls()` should be called without passing a parameter * `mock(route)` - configuration object for a single route * `mock(routes)` - array of route configuration objects @@ -47,10 +47,11 @@ The following are also accepted by mock() and translated into `config` objects w * `routes`: Either a single object or an array of similar objects each defining how the mock handles a given request. If multiple routes are specified the first matching route will be used to define the response. Each route object must have the following properties. * `name`: A unique string naming the route + * `method`: If specified will only match requests using the given http method * `matcher`: The rule for matching calls to `fetch()`. Accepts any of the following * `string`: Either an exact url to match e.g. 'http://www.site.com/page.html' or, if the string begins with a `^`, the string following the `^` must begin the url e.g. '^http://www.site.com' would match 'http://www.site.com' or 'http://www.site.com/page.html' * `RegExp`: A regular expression to test the url against - * `Function(url, opts)`: A function that is passed the url and opts `fetch()` is called with and that returns a Boolean + * `Function(url, opts)`: A function that is passed the url and opts `fetch()` is called with and that returns a Boolean. * `response`: Configures the response object returned by the mock. Can take any of the following values * `number`: creates a response with the number as the response status * `string`: creates a 200 response with the string as the response body diff --git a/src/fetch-mock.js b/src/fetch-mock.js index a5698ad5..00016772 100644 --- a/src/fetch-mock.js +++ b/src/fetch-mock.js @@ -244,38 +244,39 @@ class FetchMock { this._calls[name].push(call); } - mock (name, matcher, response) { + mock (matcher, method, response) { let config; if (response) { config = { routes: [{ - name, + name: '_mock', matcher, + method, response }] } - } else if (matcher) { + } else if (method) { config = { routes: [{ name: '_mock', - matcher: name, - response: matcher + matcher, + response: method }] } - } else if (name instanceof Array) { + } else if (matcher instanceof Array) { config = { - routes: name + routes: matcher } - } else if (name && name.matcher) { + } else if (matcher && matcher.matcher) { config = { - routes: [name] + routes: [matcher] } } else { - config = name; + config = matcher; } debug('mocking fetch'); diff --git a/test/spec.js b/test/spec.js index 271a6a9b..eb1426ee 100644 --- a/test/spec.js +++ b/test/spec.js @@ -119,12 +119,12 @@ module.exports = function (fetchMock, theGlobal) { }); describe('shorthand notation', function () { - it('accepts name, matcher, route triples', function () { + it('accepts matcher, method, route triples', function () { expect(function () { - fetchMock.mock('route', 'http://it.at.there', 'ok'); + fetchMock.mock('http://it.at.there', 'PUT', 'ok'); }).not.to.throw(); - fetch('http://it.at.there'); - expect(fetchMock.calls('route').length).to.equal(1); + fetch('http://it.at.there', {method: 'PUT'}); + expect(fetchMock.calls().length).to.equal(1); }); it('accepts matcher, route pairs', function () { @@ -135,6 +135,14 @@ module.exports = function (fetchMock, theGlobal) { expect(fetchMock.calls().length).to.equal(1); }); + it('accepts single route', function () { + expect(function () { + fetchMock.mock({name: 'route', matcher: 'http://it.at.there', response: 'ok'}); + }).not.to.throw(); + fetch('http://it.at.there'); + expect(fetchMock.calls('route').length).to.equal(1); + }); + it('accepts array of routes', function () { expect(function () { fetchMock.mock([