Skip to content

Commit

Permalink
Add support for passing callback to stubRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rky-pl committed Apr 14, 2017
1 parent 62101e4 commit e75ce6b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ let mockAdapter = (config) => {
if (stub.timeout) {
throwTimeout(config)
}
request.respondWith(stub.response)
if(typeof stub.response === 'function') {
request.respondWith(stub.response(request))
} else {
request.respondWith(stub.response)
}
stub.resolve()
break
}
Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ describe('moxios', function () {
})
})

it('should stub requests with callback', function (done) {
moxios.stubRequest('/whoami', (request) => {
return {
status: 200,
response: request.url
}
})

axios.get('/whoami').then(onFulfilled)

moxios.wait(function () {
let response = onFulfilled.getCall(0).args[0]
deepEqual(response.data, '/whoami')
done()
})
})

it('should stub timeout', function (done) {
moxios.stubTimeout('/users/12345')

Expand Down

0 comments on commit e75ce6b

Please sign in to comment.