I just started using nock to intercept http calls. Below is the code I am using in a unit test case after importing the required modules.
it('testing superagent using nock..', function () {
let mockData = {
scope: this,
data: {"title":"test"},
callback: function (data){
//expecting {id:10} here.but nothing is coming
console.log(data)
},
errback: function(err) {console.log(err)}
};
nock('http://localhost').post('/api/1234').reply(200, {id: 10});
callFunction(actionFunction, mockData, this);
}
callFunction sends the parameters 'actionFunction', 'mockData' to another class where actionFucntion takes mockData and makes ajax request using superagent module.
import xhr from './xhr';
static actionFunction(mockData){
let xhr = mockData.xhr || xhr;
xhr.reqXhr({
method: :"POST",
url: mockData.url,
data: mockData.data,
callback: function(data) {mockData.callback.call(mockData.scope, data);},
errback: function(response) {mockData.errback.call(mockData.scope, response);}
})
}
//in xhr class I am triggering the actual ajax call using super agent
super-agent(method, url).send(reqdata).end((errror, response) => {
//sending response.body to call backs similar to below
request.callback.call(this, response.body);
}
Now the issue is though nock was able to intercept the http call, nock mock response ({id:10}) is not reaching callback function. I would like to display this response in call back function
any possible way to do this?