Skip to content

Commit

Permalink
Implemented history to record all calls to the mock (#124)
Browse files Browse the repository at this point in the history
* implemented history to record all calls to the mock

* modified mock reset function to resets handlers and history, implemented resetHandlers to reset only handlers
  • Loading branch information
JoshMcguigan authored and ctimmerm committed Mar 29, 2018
1 parent 6a187dc commit 2eb55cc
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 17 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
43 changes: 43 additions & 0 deletions test/history.spec.js
Original file line number Diff line number Diff line change
@@ -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([]);
});
});
});

0 comments on commit 2eb55cc

Please # to comment.