Skip to content

Instantly share code, notes, and snippets.

@Maxim-Filimonov
Forked from pherris/superagent.js
Created May 25, 2015 05:48
Show Gist options
  • Save Maxim-Filimonov/8e74144f3028855de26c to your computer and use it in GitHub Desktop.
Save Maxim-Filimonov/8e74144f3028855de26c to your computer and use it in GitHub Desktop.
//mock for superagent - __mocks__/superagent.js
var mockDelay;
var mockError;
var mockResponse = {
status: function() {
return 200;
},
ok: function() {
return true;
},
get: jest.genMockFunction(),
toError: jest.genMockFunction()
};
var Request = {
post: function() {
return this;
},
get: function() {
return this;
},
send: function() {
return this;
},
query: function() {
return this;
},
field: function() {
return this;
},
set: function() {
return this;
},
accept: function() {
return this;
},
timeout: function() {
return this;
},
end: jest.genMockFunction().mockImplementation(function(callback) {
if (mockDelay) {
this.delayTimer = setTimeout(callback, 0, mockError, mockResponse);
return;
}
callback(mockError, mockResponse);
}),
//expose helper methods for tests to set
__setMockDelay: function(boolValue) {
mockDelay = boolValue;
},
__setMockResponse: function(mockRes) {
mockResponse = mockRes;
},
__setMockError: function(mockErr) {
mockErr = mockErr;
}
};
module.exports = Request;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment