Skip to content

Commit

Permalink
rethink of API ot handle method
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Oct 21, 2015
1 parent 2ebd5c4 commit 6398f0a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 11 additions & 10 deletions src/fetch-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
16 changes: 12 additions & 4 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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([
Expand Down

0 comments on commit 6398f0a

Please sign in to comment.