Skip to content

Commit 92ca8e3

Browse files
davidlewallenctimmerm
authored andcommitted
Existing mock should be overwritten when new mock is defined (#109)
* Added test to check if we can overwrite existing mocks * Added functionality to allow for overwriting of handlers instead of just adding to end of array * removed arrow function and code cleanup
1 parent 32fb9cb commit 92ca8e3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,16 @@ function addHandler(method, handlers, handler) {
102102
handlers[verb].push(handler);
103103
});
104104
} else {
105-
handlers[method].push(handler);
105+
if (handlers[method].length) {
106+
handlers[method].forEach(function(item, index) {
107+
if (item[0] === handler[0]) {
108+
handlers[method].splice(index, 1, handler);
109+
}
110+
handlers[method].push(handler);
111+
});
112+
} else {
113+
handlers[method].push(handler);
114+
}
106115
}
107116
}
108117

test/basics.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -532,4 +532,27 @@ describe('MockAdapter basics', function() {
532532
expect(data[0].bar).to.equal(123);
533533
});
534534
});
535+
536+
it('should overwrite existing mock', function() {
537+
var data = [
538+
{
539+
bar: 123
540+
}
541+
];
542+
543+
var data2 = [
544+
{
545+
bar: 321
546+
}
547+
];
548+
549+
mock.onGet('/').reply(200, data);
550+
mock.onGet('/').reply(200, data2);
551+
552+
return instance
553+
.get('/')
554+
.then(function(response) {
555+
expect(response.data).to.deep.equal(data2);
556+
});
557+
});
535558
});

0 commit comments

Comments
 (0)