Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/wheresrhys/fetch-mock int…
Browse files Browse the repository at this point in the history
…o v8

* 'master' of https://github.com/wheresrhys/fetch-mock:
  document new behaviour
  implement behaviour
  added tests for calling mock() with no params
  clarify docs on overwriteRoutes
  • Loading branch information
wheresrhys committed Oct 27, 2019
2 parents d3d4f85 + 8040ff6 commit c044450
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
9 changes: 5 additions & 4 deletions docs/_api-mocking/mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
title: ".mock(matcher, response, options)"
navTitle: .mock()
position: 1.0
description: Initialises or extends a stub implementation of fetch, applying a `route` that matches `matcher`, delivers a `Response` configured using `response`, and that respects the additional `options`. The stub will record its calls so they can be inspected later. If `.mock()` is called on the top level `fetch-mock` instance, this stub function will also replace `fetch` globally.
description: |
Initialises or extends a stub implementation of fetch, applying a `route` that matches `matcher`, delivers a `Response` configured using `response`, and that respects the additional `options`. The stub will record its calls so they can be inspected later. If `.mock` is called on the top level `fetch-mock` instance, this stub function will also replace `fetch` globally. Calling `.mock()` with no arguments will carry out this stubbing without defining any mock responses.
*In the documentation **route** is often used to refer to the combination of matching and responding behaviour set up using a single call to `mock()`*
parameters:
- name: matcher
types:
Expand All @@ -23,9 +26,7 @@ parameters:
- Object
content: More options to configure matching and responding behaviour
content_markdown: |-
Alternatively a single parameter, `options`, an Object with `matcher`, `response` and other options defined, can be passed
In the documentation "route" is often used to refer to the combination of matching and responding behaviour set up using a single call to `mock()`
Alternatively a single parameter, `options`, an Object with `matcher`, `response` and other options defined, can be passed in
left_code_blocks:
Expand Down
4 changes: 3 additions & 1 deletion docs/_api-mocking/mock_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
title: 'options'
position: 1.3
description: |-
An object containing further options for configuring mocking behaviour
An object containing further options for configuring mocking behaviour.
**IMPORTANT NOTE:** *To set multiple mocks which use the same `matcher` but use some of the options below to match on different criteria e.g. different headers, you will also need to set `overwriteRoutes: false` in this options object*
types:
- Object
type: parameter
Expand Down
39 changes: 21 additions & 18 deletions src/lib/set-up-and-tear-down.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
const compileRoute = require('./compile-route');
const FetchMock = {};

FetchMock.mock = function(matcher, response, options = {}) {
let route;

// Handle the variety of parameters accepted by mock (see README)
if (matcher && response) {
route = Object.assign(
{
matcher,
response
},
options
);
} else if (matcher && matcher.matcher) {
route = matcher;
} else {
throw new Error('fetch-mock: Invalid parameters passed to fetch-mock');
FetchMock.mock = function(...args) {
if (args.length) {
const [matcher, response, options = {}] = args;
let route;

// Handle the variety of parameters accepted by mock (see README)
if (matcher && response) {
route = Object.assign(
{
matcher,
response
},
options
);
} else if (matcher && matcher.matcher) {
route = matcher;
} else {
throw new Error('fetch-mock: Invalid parameters passed to fetch-mock');
}

this.addRoute(route);
}

this.addRoute(route);

return this._mock();
};

Expand Down
11 changes: 11 additions & 0 deletions test/specs/set-up-and-tear-down.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ module.exports = fetchMock => {
describe('parameters', () => {
beforeEach(() => {
sinon.stub(fm, 'compileRoute').returns({});
sinon.stub(fm, '_mock').returns(fm);
});

afterEach(() => {
fm.compileRoute.restore();
fm._mock.restore();
});

it('accepts single config object', () => {
Expand All @@ -58,6 +60,7 @@ module.exports = fetchMock => {
};
expect(() => fm.mock(config)).not.to.throw();
expect(fm.compileRoute).calledWith(config);
expect(fm._mock).called;
});

it('accepts matcher, route pairs', () => {
Expand All @@ -66,6 +69,7 @@ module.exports = fetchMock => {
matcher: 'http://it.at.there/',
response: 200
});
expect(fm._mock).called;
});

it('accepts matcher, response, config triples', () => {
Expand All @@ -81,6 +85,7 @@ module.exports = fetchMock => {
method: 'PUT',
some: 'prop'
});
expect(fm._mock).called;
});

it('expects a matcher', () => {
Expand All @@ -91,6 +96,12 @@ module.exports = fetchMock => {
expect(() => fm.mock('http://it.at.there/')).to.throw();
});

it('can be called with no parameters', () => {
expect(() => fm.mock()).not.to.throw();
expect(fm.compileRoute).not.called;
expect(fm._mock).called;
});

it('REGRESSION: should accept object respones when passing options', () => {
expect(() =>
fm.mock('http://foo.com', { foo: 'bar' }, { method: 'GET' })
Expand Down

0 comments on commit c044450

Please sign in to comment.