Skip to content

Commit

Permalink
return handler in reply(...) mocks in order to allow removing handler…
Browse files Browse the repository at this point in the history
…, and monitoring calls
  • Loading branch information
burgalon committed Apr 28, 2019
1 parent d742b23 commit 13c43f9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 44 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var mock = new MockAdapter(axios);

// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet('/users').reply(200, {
var handler = mock.onGet('/users').reply(200, {
users: [
{ id: 1, name: 'John Smith' }
]
Expand All @@ -41,6 +41,9 @@ axios.get('/users')
.then(function(response) {
console.log(response.data);
});

// Assert mock was called
expect(handler.called).to.equal(1);
```

Mocking a `GET` request with specific parameters
Expand Down
1 change: 1 addition & 0 deletions src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function handleRequest(mockAdapter, resolve, reject, config) {
);

if (handler) {
handler.called++;
if (handler.length === 7) {
utils.purgeIfReplyOnce(mockAdapter, handler);
}
Expand Down
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var deepEqual = require('deep-equal');

var utils = require('./utils');
var handleRequest = require('./handle_request');

var VERBS = ['get', 'post', 'head', 'delete', 'patch', 'put', 'options', 'list'];
Expand Down Expand Up @@ -54,6 +55,10 @@ function MockAdapter(axiosInstance, options) {

MockAdapter.prototype.adapter = adapter;

MockAdapter.prototype.removeHandler = function removeHanlder(handler) {
utils.purgeIfReplyOnce(this, handler);
};

MockAdapter.prototype.restore = function restore() {
if (this.axiosInstance) {
this.axiosInstance.defaults.adapter = this.originalAdapter;
Expand All @@ -73,13 +78,13 @@ VERBS.concat('any').forEach(function(method) {
function reply(code, response, headers) {
var handler = [matcher, body, requestHeaders, code, response, headers];
addHandler(method, _this.handlers, handler);
return _this;
return handler;
}

function replyOnce(code, response, headers) {
var handler = [matcher, body, requestHeaders, code, response, headers, true];
addHandler(method, _this.handlers, handler);
return _this;
return handler;
}

return {
Expand All @@ -90,7 +95,7 @@ VERBS.concat('any').forEach(function(method) {
passThrough: function passThrough() {
var handler = [matcher, body];
addHandler(method, _this.handlers, handler);
return _this;
return handler;
},

networkError: function() {
Expand Down Expand Up @@ -151,6 +156,8 @@ function findInHandlers(method, handlers, handler) {
}

function addHandler(method, handlers, handler) {
handler.called = 0;
// const handler = originalHandler
if (method === 'any') {
VERBS.forEach(function(verb) {
handlers[verb].push(handler);
Expand Down
53 changes: 31 additions & 22 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ describe('MockAdapter basics', function() {
it('accepts a callback that returns an axios request', function() {
mock
.onGet('/bar')
.reply(200, { foo: 'bar' })
.onGet('/foo')
.reply(200, { foo: 'bar' });
mock.onGet('/foo')
.reply(function() {
return instance.get('/bar');
});
Expand Down Expand Up @@ -216,6 +216,18 @@ describe('MockAdapter basics', function() {
});
});

it('allow removing mock handler', function() {
const handler = mock.onGet('/').reply(200);
return instance.get('/').then(function(response) {
expect(response.status).to.equal(200);
mock.removeHandler(handler);
return instance.get('/');
}).catch(function(error) {
expect(error.response.status).to.equal(404);
expect(handler.called).to.equal(1);
});
});

it('matches when parameters were not expected', function() {
mock.onGet('/withParams').reply(200);
return instance
Expand Down Expand Up @@ -466,10 +478,10 @@ describe('MockAdapter basics', function() {
it('can chain calls to add mock handlers', function() {
mock
.onGet('/foo')
.reply(200)
.onAny('/bar')
.reply(404)
.onPost('/baz')
.reply(200);
mock.onAny('/bar')
.reply(404);
mock.onPost('/baz')
.reply(500);

expect(mock.handlers['get'].length).to.equal(2);
Expand Down Expand Up @@ -510,10 +522,9 @@ describe('MockAdapter basics', function() {
});

it('maps empty GET path to any path', function() {
mock
.onGet('/foo')
.reply(200, 'foo')
.onGet()
mock.onGet('/foo')
.reply(200, 'foo');
mock.onGet()
.reply(200, 'bar');

return Promise.all([
Expand Down Expand Up @@ -636,14 +647,13 @@ describe('MockAdapter basics', function() {
});

it('supports chaining on same path with different params', function() {
mock
.onGet('/users', { params: { searchText: 'John' } })
.reply(200, { id: 1 })
.onGet('/users', { params: { searchText: 'James' } })
.reply(200, { id: 2 })
.onGet('/users', { params: { searchText: 'Jake' } })
.reply(200, { id: 3 })
.onGet('/users', { params: { searchText: 'Jackie' } })
mock.onGet('/users', { params: { searchText: 'John' } })
.reply(200, { id: 1 });
mock.onGet('/users', { params: { searchText: 'James' } })
.reply(200, { id: 2 });
mock.onGet('/users', { params: { searchText: 'Jake' } })
.reply(200, { id: 3 });
mock.onGet('/users', { params: { searchText: 'Jackie' } })
.reply(200, { id: 4 });

return instance
Expand Down Expand Up @@ -739,10 +749,9 @@ describe('MockAdapter basics', function() {
});

it('allows overwriting mocks with parameters', function() {
mock
.onGet('/users', { params: { searchText: 'John' } })
.reply(500)
.onGet('/users', { params: { searchText: 'John' } })
mock.onGet('/users', { params: { searchText: 'John' } })
.reply(500);
mock.onGet('/users', { params: { searchText: 'John' } })
.reply(200, { id: 1 });

return instance
Expand Down
5 changes: 2 additions & 3 deletions test/pass_through.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ describe('passThrough tests (requires Node)', function() {
});

it('allows setting default passThrough handler', function() {
mock
.onGet('/foo').reply(200, 'bar')
.onAny().passThrough();
mock.onGet('/foo').reply(200, 'bar');
mock.onAny().passThrough();

var randomPath = 'xyz' + Math.round(10000 * Math.random());

Expand Down
17 changes: 2 additions & 15 deletions test/reply_once.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ describe('MockAdapter replyOnce', function() {
mock = new MockAdapter(instance);
});

it('supports chaining', function() {
mock
.onGet('/foo')
.replyOnce(200)
.onAny('/foo')
.replyOnce(500)
.onPost('/foo')
.replyOnce(201);

expect(mock.handlers['get'].length).to.equal(2);
expect(mock.handlers['post'].length).to.equal(2);
});

it('replies as normally on the first call', function() {
mock.onGet('/foo').replyOnce(200, {
foo: 'bar'
Expand Down Expand Up @@ -90,8 +77,8 @@ describe('MockAdapter replyOnce', function() {
.onGet('/foo')
.replyOnce(function() {
return [200];
})
.onGet('/foo')
});
mock.onGet('/foo')
.replyOnce(function() {
return [202];
});
Expand Down

0 comments on commit 13c43f9

Please sign in to comment.