Skip to content

Commit 1cc8672

Browse files
committed
return handler in reply(...) mocks in order to allow removing handler, and monitoring calls
1 parent d742b23 commit 1cc8672

File tree

6 files changed

+49
-43
lines changed

6 files changed

+49
-43
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var mock = new MockAdapter(axios);
3131

3232
// Mock any GET request to /users
3333
// arguments for reply are (status, data, headers)
34-
mock.onGet('/users').reply(200, {
34+
var handler = mock.onGet('/users').reply(200, {
3535
users: [
3636
{ id: 1, name: 'John Smith' }
3737
]
@@ -41,6 +41,9 @@ axios.get('/users')
4141
.then(function(response) {
4242
console.log(response.data);
4343
});
44+
45+
// Assert mock was called
46+
expect(handler.called).to.equal(1);
4447
```
4548

4649
Mocking a `GET` request with specific parameters

src/handle_request.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function handleRequest(mockAdapter, resolve, reject, config) {
3131
);
3232

3333
if (handler) {
34+
handler.called++;
3435
if (handler.length === 7) {
3536
utils.purgeIfReplyOnce(mockAdapter, handler);
3637
}

src/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

5+
var utils = require('./utils');
56
var handleRequest = require('./handle_request');
67

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

5556
MockAdapter.prototype.adapter = adapter;
5657

58+
MockAdapter.prototype.removeHandler = function removeHanlder(handler) {
59+
utils.purgeIfReplyOnce(this, handler);
60+
};
61+
5762
MockAdapter.prototype.restore = function restore() {
5863
if (this.axiosInstance) {
5964
this.axiosInstance.defaults.adapter = this.originalAdapter;
@@ -73,13 +78,13 @@ VERBS.concat('any').forEach(function(method) {
7378
function reply(code, response, headers) {
7479
var handler = [matcher, body, requestHeaders, code, response, headers];
7580
addHandler(method, _this.handlers, handler);
76-
return _this;
81+
return handler;
7782
}
7883

7984
function replyOnce(code, response, headers) {
8085
var handler = [matcher, body, requestHeaders, code, response, headers, true];
8186
addHandler(method, _this.handlers, handler);
82-
return _this;
87+
return handler;
8388
}
8489

8590
return {
@@ -90,7 +95,7 @@ VERBS.concat('any').forEach(function(method) {
9095
passThrough: function passThrough() {
9196
var handler = [matcher, body];
9297
addHandler(method, _this.handlers, handler);
93-
return _this;
98+
return handler;
9499
},
95100

96101
networkError: function() {
@@ -151,6 +156,8 @@ function findInHandlers(method, handlers, handler) {
151156
}
152157

153158
function addHandler(method, handlers, handler) {
159+
handler.called = 0;
160+
// const handler = originalHandler
154161
if (method === 'any') {
155162
VERBS.forEach(function(verb) {
156163
handlers[verb].push(handler);

test/basics.spec.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ describe('MockAdapter basics', function() {
101101
it('accepts a callback that returns an axios request', function() {
102102
mock
103103
.onGet('/bar')
104-
.reply(200, { foo: 'bar' })
105-
.onGet('/foo')
104+
.reply(200, { foo: 'bar' });
105+
mock.onGet('/foo')
106106
.reply(function() {
107107
return instance.get('/bar');
108108
});
@@ -216,6 +216,18 @@ describe('MockAdapter basics', function() {
216216
});
217217
});
218218

219+
it('allow removing mock handler', function() {
220+
const handler = mock.onGet('/').reply(200);
221+
return instance.get('/').then(function(response) {
222+
expect(response.status).to.equal(200);
223+
mock.removeHandler(handler);
224+
return instance.get('/');
225+
}).catch(function(error) {
226+
expect(error.response.status).to.equal(404);
227+
expect(handler.called).to.equal(1);
228+
});
229+
});
230+
219231
it('matches when parameters were not expected', function() {
220232
mock.onGet('/withParams').reply(200);
221233
return instance
@@ -466,10 +478,10 @@ describe('MockAdapter basics', function() {
466478
it('can chain calls to add mock handlers', function() {
467479
mock
468480
.onGet('/foo')
469-
.reply(200)
470-
.onAny('/bar')
481+
.reply(200);
482+
mock.onAny('/bar')
471483
.reply(404)
472-
.onPost('/baz')
484+
mock.onPost('/baz')
473485
.reply(500);
474486

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

512524
it('maps empty GET path to any path', function() {
513-
mock
514-
.onGet('/foo')
515-
.reply(200, 'foo')
516-
.onGet()
525+
mock.onGet('/foo')
526+
.reply(200, 'foo');
527+
mock.onGet()
517528
.reply(200, 'bar');
518529

519530
return Promise.all([
@@ -636,14 +647,13 @@ describe('MockAdapter basics', function() {
636647
});
637648

638649
it('supports chaining on same path with different params', function() {
639-
mock
640-
.onGet('/users', { params: { searchText: 'John' } })
641-
.reply(200, { id: 1 })
642-
.onGet('/users', { params: { searchText: 'James' } })
643-
.reply(200, { id: 2 })
644-
.onGet('/users', { params: { searchText: 'Jake' } })
645-
.reply(200, { id: 3 })
646-
.onGet('/users', { params: { searchText: 'Jackie' } })
650+
mock.onGet('/users', { params: { searchText: 'John' } })
651+
.reply(200, { id: 1 });
652+
mock.onGet('/users', { params: { searchText: 'James' } })
653+
.reply(200, { id: 2 });
654+
mock.onGet('/users', { params: { searchText: 'Jake' } })
655+
.reply(200, { id: 3 });
656+
mock.onGet('/users', { params: { searchText: 'Jackie' } })
647657
.reply(200, { id: 4 });
648658

649659
return instance
@@ -739,10 +749,9 @@ describe('MockAdapter basics', function() {
739749
});
740750

741751
it('allows overwriting mocks with parameters', function() {
742-
mock
743-
.onGet('/users', { params: { searchText: 'John' } })
744-
.reply(500)
745-
.onGet('/users', { params: { searchText: 'John' } })
752+
mock.onGet('/users', { params: { searchText: 'John' } })
753+
.reply(500);
754+
mock.onGet('/users', { params: { searchText: 'John' } })
746755
.reply(200, { id: 1 });
747756

748757
return instance

test/pass_through.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ describe('passThrough tests (requires Node)', function() {
8585
});
8686

8787
it('allows setting default passThrough handler', function() {
88-
mock
89-
.onGet('/foo').reply(200, 'bar')
90-
.onAny().passThrough();
88+
mock.onGet('/foo').reply(200, 'bar');
89+
mock.onAny().passThrough();
9190

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

test/reply_once.spec.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@ describe('MockAdapter replyOnce', function() {
1212
mock = new MockAdapter(instance);
1313
});
1414

15-
it('supports chaining', function() {
16-
mock
17-
.onGet('/foo')
18-
.replyOnce(200)
19-
.onAny('/foo')
20-
.replyOnce(500)
21-
.onPost('/foo')
22-
.replyOnce(201);
23-
24-
expect(mock.handlers['get'].length).to.equal(2);
25-
expect(mock.handlers['post'].length).to.equal(2);
26-
});
27-
2815
it('replies as normally on the first call', function() {
2916
mock.onGet('/foo').replyOnce(200, {
3017
foo: 'bar'
@@ -90,8 +77,8 @@ describe('MockAdapter replyOnce', function() {
9077
.onGet('/foo')
9178
.replyOnce(function() {
9279
return [200];
93-
})
94-
.onGet('/foo')
80+
});
81+
mock.onGet('/foo')
9582
.replyOnce(function() {
9683
return [202];
9784
});

0 commit comments

Comments
 (0)