Skip to content

Commit

Permalink
Add reset method
Browse files Browse the repository at this point in the history
  • Loading branch information
ctimmerm committed Apr 15, 2016
1 parent c38679f commit fa478df
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ You can restore the original adapter (which will remove the mocking behavior)
mock.restore();
```

You can also reset the registered mock handlers with `reset`

```js
mock.reset();
```

`reset` is different from `restore` in that `restore` removes the mocking from the axios instance completely,
whereas `reset` only removes all mock handlers that were added with onGet, onPost, etc. but leaves the mocking in place.

Passing a function to `reply`

```js
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ function adapter() {
}.bind(this);
}

function MockAdapter(axiosInstance) {
function reset() {
this.matchers = verbs.reduce(function(previousValue, currentValue) {
previousValue[currentValue] = [];
return previousValue;
}, {});
}

function MockAdapter(axiosInstance) {
reset.call(this);

if (axiosInstance) {
this.axiosInstance = axiosInstance;
Expand Down Expand Up @@ -65,6 +69,8 @@ MockAdapter.prototype.restore = function() {
}
};

MockAdapter.prototype.reset = reset;

verbs.forEach(function(method) {
var methodName = 'on' + method.charAt(0).toUpperCase() + method.slice(1);
MockAdapter.prototype[methodName] = function(matcher) {
Expand Down
12 changes: 12 additions & 0 deletions test/mock_adapter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ describe('MockAdapter', function() {
expect(newInstance.defaults.adapter).to.equal(adapter);
});

it('resets the registered mock handlers', function(done) {
mock.onGet('/foo').reply(500);
mock.reset();
mock.onGet('/foo').reply(200);

instance.get('/foo')
.then(function(response) {
expect(response.status).to.equal(200);
done();
});
});

it('can chain calls to add mock handlers', function() {
mock
.onGet('/foo').reply(200)
Expand Down

0 comments on commit fa478df

Please # to comment.