Skip to content

Commit

Permalink
added note to upgrade docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 19, 2018
1 parent e5236b7 commit 43cc6ee
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion docs/v6-v7-upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const fetchMock = require('fetch-mock').fetchMock;
const { fetchMock } = require('fetch-mock');
```

The reason for this shoudl become clear in the next point
The reason for this should become clear in the next point

## Adds MATCHED and UNMATCHED constants
The inspection APIs e.g. `calls()` can be passed `true` or `false` to return matched/unmatched calls respectively. To aid with more comprehensible code, fetchMock now exports `MATCHED` and `UNMATCHED` constants, equal to `true` and `false`. Using `true` or `false` still works, but I'd recommend using the constants. Compare the readbility of the following:
Expand All @@ -39,3 +39,31 @@ const { fetchMock, MATCHED, UNMATCHED } = require('fetch-mock');
fetchMock.called(true);
fetchMock.called(MATCHED);
```

## Able to match requests based on the path of the url
`fetchMock.mock('path:/apples/pears')` Will match any url whose `path` part is `apples/pears`

## More powerful inspection filtering
Previously, any filter passed in to `calls(filter)` etc... would always be converted to a string and then be used to lookup whether any fetch calls had been handled by a route matcher that also had the same `toString()` value. This is still the case, but if no calls match, then the filter will be converted into an on the fly route matcher, which is then used to filter the list of calls that were handled by fetch. This measn e.g. you can use any regex or glob to filter the calls.

### Example
```js
fetchMock.mock('*', 200)
await fetch('/main-course/lasagne', {headers: {discount: true}});
await fetch('/main-course/bolognaise');

fetchMock.called(/l.+gne/) // true
fetchMock.called('glob:/*/lasagne') // true
fetchMock.called((url, options) => options.headers.discount) // true
```
And, copied directly from the API docs:

Most of the methods below accept two parameters, `(filter, method)`
- `filter` Enables filtering fetch calls for the most commonly use cases. The behaviour can be counterintuitive. The following rules, applied in the order they are described, are used to try to retrieve calls. If any rule retrieves no calls the next rule will be tried.
- If `filter` is `true` (or `fetchMock.MATCHED`) all calls that matched some route are returned
- If `filter` is `false` (or `fetchMock.UNMATCHED`) all calls that did not match any route are returned (when `.spy()`, `catch()` or `config.fallThroughToNetwork` were used to prevent errors being thrown)
- If `filter` is `undefined` all calls, matched _and_ unmatched, are returned
- If `filter` is the name of a named route, all calls handled by that route are returned
- If `filter` is equal to `matcher` or `matcher.toString()` for a route, all calls handled by that route are returned
- `filter` is executed using the same execution plan as matchers used in `.mock()`. Any calls matched by it will be returned.
- `method` A http method to filter by

0 comments on commit 43cc6ee

Please sign in to comment.