Skip to content

Commit 61122fb

Browse files
committed
Better API docs
1 parent e303132 commit 61122fb

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

README.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ Mock http requests made using fetch (or isomorphic-fetch)
1111
`require('fetch-mock')` exports a singleton with the following methods
1212

1313
### `mock(config)`
14-
Replaces `fetch` with a sinon stub which, in addition to the default sinon behaviour, records each of its calls and optionally returns a stub response or passes the call through to `fetch`. `config` is an optional* object with the following properties.
15-
16-
* `routes` Either a single object or an array of similar objects each defining how the mock handles a given request. Each route object must have the following properties. If multiple routes are specified the first matching route will be used to define the response
17-
* `name` A unique string naming the route
18-
* `matcher` The rule for matching calls to `fetch`. Accepts any of the following
19-
* `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'
20-
* `RegExp` A regular expression to test the url against
21-
* `Function(url, opts)` A function that is passed the url and opts `fetch` is called with and that returns a Boolean
22-
* `response` Configures the response object returned by the mock. Can have one of the following values
23-
* `string` creates a 200 response with the string as the response body
24-
* `config object` If the object contains any of the properties body, status, headers, throws; then these properties - all of them optional - are used to construct a response as follows
25-
* `body` Retunred in the response body
26-
* `status` Returned in the response status
27-
* `headers` Returned in the response headers. They should be defined as an object literal (property names case-insensitive) which will be converted to a `Headers` instance
28-
* `throws` If this property is present then a `Promise` rejected with the value of `throws` is returned
29-
* `object literal` As long as the object does not contain any of the above properties it is converted into a json string and this is returned as the body of a 200 response
30-
* `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
31-
* `responses` When `registerRoute` has already been used to register some routes then `responses` can be used to override the default response. It's value should be an object mapping route names to responses, which should be similar to those listed immediately above e.g.
14+
Replaces `fetch()` with a sinon stub which, in addition to the default sinon behaviour, records each of its calls and optionally returns a stub response or passes the call through to `fetch()`. `config` is an optional* object with the following properties.
15+
16+
* `routes`: Either a single object or an array of similar objects each defining how the mock handles a given request. Each route object must have the following properties. If multiple routes are specified the first matching route will be used to define the response
17+
* `name`: A unique string naming the route
18+
* `matcher`: The rule for matching calls to `fetch()`. Accepts any of the following
19+
* `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'
20+
* `RegExp`: A regular expression to test the url against
21+
* `Function(url, opts)`: A function that is passed the url and opts `fetch` is called with and that returns a Boolean
22+
* `response`: Configures the response object returned by the mock. Can have one of the following values
23+
* `string`: creates a 200 response with the string as the response body
24+
* `object`: If the object contains any of the properties body, status, headers, throws; then these properties - all of them optional - are used to construct a response as follows
25+
* `body`: Retunred in the response body
26+
* `status`: Returned in the response status
27+
* `headers`: Returned in the response headers. They should be defined as an object literal (property names case-insensitive) which will be converted to a `Headers` instance
28+
* `throws`: If this property is present then a `Promise` rejected with the value of `throws` is returned
29+
As long as the object does not contain any of the above properties it is converted into a json string and this is returned as the body of a 200 response
30+
* `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
31+
* `responses`: When `registerRoute()` has already been used to register some routes then `responses` can be used to override the default response. Its value should be an object mapping route names to responses, which should be similar to those listed immediately above e.g.
3232

3333
```javascript
3434
responses: {
@@ -42,8 +42,8 @@ Replaces `fetch` with a sinon stub which, in addition to the default sinon behav
4242
}
4343
```
4444

45-
* `greed` Determines how the mock handles unmatched requests
46-
* 'none': all unmatched calls get passed through to `fetch`
45+
* `greed`: Determines how the mock handles unmatched requests
46+
* 'none': all unmatched calls get passed through to `fetch()`
4747
* 'bad': all unmatched calls result in a rejected promise
4848
* 'good': all unmatched calls result in a resolved promise with a 200 status
4949

@@ -52,13 +52,13 @@ Replaces `fetch` with a sinon stub which, in addition to the default sinon behav
5252

5353

5454
### `restore()`
55-
Restores `fetch` to its unstubbed state and clears all data recorded for its calls
55+
Restores `fetch()` to its unstubbed state and clears all data recorded for its calls
5656

5757
### `reset()`
58-
Clears all data recorded for `fetch`'s calls
58+
Clears all data recorded for `fetch()`'s calls
5959

6060
### `calls(routeName)`
61-
Returns an array of arrays of the arguments passed to `fetch` that matched the given route
61+
Returns an array of arrays of the arguments passed to `fetch()` that matched the given route
6262

6363
### `called(routeName)`
6464
Returns a Boolean denoting whether any calls matched the given route
@@ -67,12 +67,12 @@ Returns a Boolean denoting whether any calls matched the given route
6767
Normally calling `mock()` twice without restoring inbetween will throw an error. `reMock()` calls `restore()` internally before calling `mock()` again. This allows you to put a generic call to `mock()` in a `beforeEach()` while retaining the flexibility to vary the responses for some tests
6868

6969
### `registerRoute(name, matcher, response)`
70-
Often your application/module will always need responses for some calls in order to initialise properly, even if the content of those calls are not the subject of a given test e.g. a mock response from an authentication service and a lti-variant testing service might be necessary in order to test the UI for a version of a log in form. It's helpful to be able to define some default responses for these services which will exist throughout all or a large subset of your tests. `registerRoute` aims to fulfil this need. All these predefined routes can be overridden when `mock(config)` is called.
70+
Often your application/module will always need responses for some calls in order to initialise properly, even if the content of those calls are not the subject of a given test e.g. a mock response from an authentication service and a lti-variant testing service might be necessary in order to test the UI for a version of a log in form. It's helpful to be able to define some default responses for these services which will exist throughout all or a large subset of your tests. `registerRoute()` aims to fulfil this need. All these predefined routes can be overridden when `mock(config)` is called.
7171

72-
`registerRoute` takes either of the following parameters
73-
* `object` An object similar to the route objects accepted by `mock()`
74-
* `array` An array of the above objects
75-
* `name`, `matcher`, `response` The 3 properties of the route object spread across 3 parameters
72+
`registerRoute()` takes either of the following parameters
73+
* `object`: An object similar to the route objects accepted by `mock()`
74+
* `array`: An array of the above objects
75+
* `name`, `matcher`, `response`: The 3 properties of the route object spread across 3 parameters
7676

7777
### `unregisterRoute(name)`
7878
Unregisters one or more previously registered routes. Accepts either a string or an array of strings

0 commit comments

Comments
 (0)