Skip to content

Commit fa478df

Browse files
committed
Add reset method
1 parent c38679f commit fa478df

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ You can restore the original adapter (which will remove the mocking behavior)
4545
mock.restore();
4646
```
4747

48+
You can also reset the registered mock handlers with `reset`
49+
50+
```js
51+
mock.reset();
52+
```
53+
54+
`reset` is different from `restore` in that `restore` removes the mocking from the axios instance completely,
55+
whereas `reset` only removes all mock handlers that were added with onGet, onPost, etc. but leaves the mocking in place.
56+
4857
Passing a function to `reply`
4958

5059
```js

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ function adapter() {
3131
}.bind(this);
3232
}
3333

34-
function MockAdapter(axiosInstance) {
34+
function reset() {
3535
this.matchers = verbs.reduce(function(previousValue, currentValue) {
3636
previousValue[currentValue] = [];
3737
return previousValue;
3838
}, {});
39+
}
40+
41+
function MockAdapter(axiosInstance) {
42+
reset.call(this);
3943

4044
if (axiosInstance) {
4145
this.axiosInstance = axiosInstance;
@@ -65,6 +69,8 @@ MockAdapter.prototype.restore = function() {
6569
}
6670
};
6771

72+
MockAdapter.prototype.reset = reset;
73+
6874
verbs.forEach(function(method) {
6975
var methodName = 'on' + method.charAt(0).toUpperCase() + method.slice(1);
7076
MockAdapter.prototype[methodName] = function(matcher) {

test/mock_adapter_test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ describe('MockAdapter', function() {
163163
expect(newInstance.defaults.adapter).to.equal(adapter);
164164
});
165165

166+
it('resets the registered mock handlers', function(done) {
167+
mock.onGet('/foo').reply(500);
168+
mock.reset();
169+
mock.onGet('/foo').reply(200);
170+
171+
instance.get('/foo')
172+
.then(function(response) {
173+
expect(response.status).to.equal(200);
174+
done();
175+
});
176+
});
177+
166178
it('can chain calls to add mock handlers', function() {
167179
mock
168180
.onGet('/foo').reply(200)

0 commit comments

Comments
 (0)