File tree Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,15 @@ You can restore the original adapter (which will remove the mocking behavior)
45
45
mock .restore ();
46
46
```
47
47
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
+
48
57
Passing a function to ` reply `
49
58
50
59
``` js
Original file line number Diff line number Diff line change @@ -31,11 +31,15 @@ function adapter() {
31
31
} . bind ( this ) ;
32
32
}
33
33
34
- function MockAdapter ( axiosInstance ) {
34
+ function reset ( ) {
35
35
this . matchers = verbs . reduce ( function ( previousValue , currentValue ) {
36
36
previousValue [ currentValue ] = [ ] ;
37
37
return previousValue ;
38
38
} , { } ) ;
39
+ }
40
+
41
+ function MockAdapter ( axiosInstance ) {
42
+ reset . call ( this ) ;
39
43
40
44
if ( axiosInstance ) {
41
45
this . axiosInstance = axiosInstance ;
@@ -65,6 +69,8 @@ MockAdapter.prototype.restore = function() {
65
69
}
66
70
} ;
67
71
72
+ MockAdapter . prototype . reset = reset ;
73
+
68
74
verbs . forEach ( function ( method ) {
69
75
var methodName = 'on' + method . charAt ( 0 ) . toUpperCase ( ) + method . slice ( 1 ) ;
70
76
MockAdapter . prototype [ methodName ] = function ( matcher ) {
Original file line number Diff line number Diff line change @@ -163,6 +163,18 @@ describe('MockAdapter', function() {
163
163
expect ( newInstance . defaults . adapter ) . to . equal ( adapter ) ;
164
164
} ) ;
165
165
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
+
166
178
it ( 'can chain calls to add mock handlers' , function ( ) {
167
179
mock
168
180
. onGet ( '/foo' ) . reply ( 200 )
You can’t perform that action at this time.
0 commit comments