Skip to content

Commit ce0aaf7

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

File tree

6 files changed

+50
-44
lines changed

6 files changed

+50
-44
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: 31 additions & 22 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
@@ -510,10 +522,10 @@ describe('MockAdapter basics', function() {
510522
it('can chain calls to add mock handlers', function() {
511523
mock
512524
.onGet('/foo')
513-
.reply(200)
514-
.onAny('/bar')
515-
.reply(404)
516-
.onPost('/baz')
525+
.reply(200);
526+
mock.onAny('/bar')
527+
.reply(404);
528+
mock.onPost('/baz')
517529
.reply(500);
518530

519531
expect(mock.handlers['get'].length).to.equal(2);
@@ -554,10 +566,9 @@ describe('MockAdapter basics', function() {
554566
});
555567

556568
it('maps empty GET path to any path', function() {
557-
mock
558-
.onGet('/foo')
559-
.reply(200, 'foo')
560-
.onGet()
569+
mock.onGet('/foo')
570+
.reply(200, 'foo');
571+
mock.onGet()
561572
.reply(200, 'bar');
562573

563574
return Promise.all([
@@ -680,14 +691,13 @@ describe('MockAdapter basics', function() {
680691
});
681692

682693
it('supports chaining on same path with different params', function() {
683-
mock
684-
.onGet('/users', { params: { searchText: 'John' } })
685-
.reply(200, { id: 1 })
686-
.onGet('/users', { params: { searchText: 'James' } })
687-
.reply(200, { id: 2 })
688-
.onGet('/users', { params: { searchText: 'Jake' } })
689-
.reply(200, { id: 3 })
690-
.onGet('/users', { params: { searchText: 'Jackie' } })
694+
mock.onGet('/users', { params: { searchText: 'John' } })
695+
.reply(200, { id: 1 });
696+
mock.onGet('/users', { params: { searchText: 'James' } })
697+
.reply(200, { id: 2 });
698+
mock.onGet('/users', { params: { searchText: 'Jake' } })
699+
.reply(200, { id: 3 });
700+
mock.onGet('/users', { params: { searchText: 'Jackie' } })
691701
.reply(200, { id: 4 });
692702

693703
return instance
@@ -783,10 +793,9 @@ describe('MockAdapter basics', function() {
783793
});
784794

785795
it('allows overwriting mocks with parameters', function() {
786-
mock
787-
.onGet('/users', { params: { searchText: 'John' } })
788-
.reply(500)
789-
.onGet('/users', { params: { searchText: 'John' } })
796+
mock.onGet('/users', { params: { searchText: 'John' } })
797+
.reply(500);
798+
mock.onGet('/users', { params: { searchText: 'John' } })
790799
.reply(200, { id: 1 });
791800

792801
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)