diff --git a/README.md b/README.md index 1b94fb1..da6d803 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index edf2a9f..7b0e08d 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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) { diff --git a/test/mock_adapter_test.js b/test/mock_adapter_test.js index 3cef6a0..05cb2dc 100644 --- a/test/mock_adapter_test.js +++ b/test/mock_adapter_test.js @@ -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)