Skip to content

Commit

Permalink
Add support for chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
ctimmerm committed Apr 14, 2016
1 parent 3905e46 commit 8cb6864
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ mock.onGet(/\/users\/\d+/).reply(function(config) {
});
```

Chaining is also supported

```js
mock
.onGet('/users').reply(200, users)
.onGet('/posts').reply(200, posts);
```

Mocking any request to a given url

```js
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ MockAdapter.prototype.onAny = function(matcher) {
verbs.forEach(function(verb) {
_this.matchers[verb].push(handler);
});
return _this;
}
};
};
Expand All @@ -71,6 +72,7 @@ verbs.forEach(function(method) {
return {
reply: function reply(code, response, headers) {
_this.matchers[method].push([matcher, code, response, headers]);
return _this;
}
};
};
Expand Down
10 changes: 10 additions & 0 deletions test/mock_adapter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ describe('MockAdapter', function() {
expect(newInstance.defaults.adapter).to.equal(adapter);
});

it('can chain calls to add mock handlers', function() {
mock
.onGet('/foo').reply(200)
.onAny('/bar').reply(404)
.onPost('/baz').reply(500);

expect(mock.matchers['get']).to.not.be.empty;
expect(mock.matchers['post']).to.not.be.empty;
});

context('on the default instance', function() {
afterEach(function() {
axios.defaults.adapter = undefined;
Expand Down

0 comments on commit 8cb6864

Please sign in to comment.