diff --git a/src/handle_request.js b/src/handle_request.js index 84ea5a9..ab405ac 100644 --- a/src/handle_request.js +++ b/src/handle_request.js @@ -16,6 +16,7 @@ function handleRequest(mockAdapter, resolve, reject, config) { config.url = config.url.slice(config.baseURL ? config.baseURL.length : 0); } config.adapter = null; + mockAdapter.history[config.method].push(config); var handler = utils.findHandler( mockAdapter.handlers, diff --git a/src/index.js b/src/index.js index 33bd327..ebd6a32 100644 --- a/src/index.js +++ b/src/index.js @@ -21,13 +21,26 @@ function adapter() { }.bind(this); } -function reset() { - this.handlers = VERBS.reduce(function(accumulator, verb) { +function getVerbObject() { + return VERBS.reduce(function(accumulator, verb) { accumulator[verb] = []; return accumulator; }, {}); } +function reset() { + resetHandlers.call(this); + resetHistory.call(this); +} + +function resetHandlers() { + this.handlers = getVerbObject(); +} + +function resetHistory() { + this.history = getVerbObject(); +} + function MockAdapter(axiosInstance, options) { reset.call(this); @@ -48,6 +61,8 @@ MockAdapter.prototype.restore = function restore() { }; MockAdapter.prototype.reset = reset; +MockAdapter.prototype.resetHandlers = resetHandlers; +MockAdapter.prototype.resetHistory = resetHistory; VERBS.concat('any').forEach(function(method) { var methodName = 'on' + method.charAt(0).toUpperCase() + method.slice(1); diff --git a/test/basics.spec.js b/test/basics.spec.js index 9066f39..680e9ae 100644 --- a/test/basics.spec.js +++ b/test/basics.spec.js @@ -384,6 +384,31 @@ describe('MockAdapter basics', function() { expect(mock.handlers['get']).to.be.empty; }); + it('resets the history', function() { + mock.onAny('/foo').reply(200); + + return instance + .get('/foo') + .then(function(response) { + mock.reset(); + expect(mock.history['get']).to.eql([]); + }); + }); + + it('resets only the registered mock handlers, not the history', function() { + mock.onAny('/foo').reply(200); + expect(mock.handlers['get']).not.to.be.empty; + expect(mock.history['get']).to.eql([]); + + return instance + .get('/foo') + .then(function(response) { + mock.resetHandlers(); + expect(mock.history.get.length).to.equal(1); + expect(mock.handlers['get']).to.be.empty; + }); + }); + it('can chain calls to add mock handlers', function() { mock .onGet('/foo') diff --git a/test/history.spec.js b/test/history.spec.js new file mode 100644 index 0000000..444cd48 --- /dev/null +++ b/test/history.spec.js @@ -0,0 +1,43 @@ +var axios = require('axios'); +var expect = require('chai').expect; + +var MockAdapter = require('../src'); + +describe('MockAdapter history', function() { + var instance; + var mock; + + beforeEach(function() { + instance = axios.create(); + mock = new MockAdapter(instance); + }); + + it('initializes empty history for each http method', function() { + expect(mock.history['get']).to.eql([]); + expect(mock.history['post']).to.eql([]); + expect(mock.history['put']).to.eql([]); + }); + + it('records the axios config each time the handler is invoked', function() { + mock.onAny('/foo').reply(200); + + return instance + .get('/foo') + .then(function(response) { + expect(mock.history.get.length).to.equal(1); + expect(mock.history.get[0].method).to.equal('get'); + expect(mock.history.get[0].url).to.equal('/foo'); + }); + }); + + it('reset history should reset all history', function() { + mock.onAny('/foo').reply(200); + + return instance + .get('/foo') + .then(function(response) { + mock.resetHistory(); + expect(mock.history['get']).to.eql([]); + }); + }); +});