Created
December 20, 2014 14:34
-
-
Save shovon/5d34311ad62428e84b24 to your computer and use it in GitHub Desktop.
superagent mock for Jest tests. Stolen from http://bl.ocks.org/simenbrekken/b6282f713605b619834f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global jest */ | |
var superagent = jest.genMockFunction().mockReturnThis(); | |
var Response = jest.genMockFunction().mockImplementation(function() { | |
this.status = 200; | |
this.ok = true; | |
}); | |
Response.prototype.get = jest.genMockFunction(); | |
Response.prototype.toError = jest.genMockFunction(); | |
var Request = jest.genMockFunction().mockImplementation(function(method, url) { | |
this.method = method; | |
this.url = url; | |
}); | |
Request.prototype.accept = jest.genMockFunction().mockReturnThis(); | |
Request.prototype.set = jest.genMockFunction().mockReturnThis(); | |
Request.prototype.send = jest.genMockFunction().mockReturnThis(); | |
Request.prototype.field = jest.genMockFunction().mockReturnThis(); | |
Request.prototype.query = jest.genMockFunction().mockReturnThis(); | |
Request.prototype.end = jest.genMockFunction().mockImplementation(function(callback) { | |
if (superagent.mockDelay) { | |
this.delayTimer = setTimeout(callback, 0, superagent.mockError, superagent.mockResponse); | |
return; | |
} | |
callback(superagent.mockError, superagent.mockResponse); | |
}); | |
Request.prototype.abort = jest.genMockFunction().mockImplementation(function() { | |
this.aborted = true; | |
if (this.delayTimer) { | |
clearTimeout(this.delayTimer); | |
} | |
}); | |
superagent.Request = Request; | |
superagent.Response = Response; | |
superagent.mockResponse = new Response(); | |
superagent.mockError = null; | |
superagent.mockDelay = false; | |
module.exports = superagent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment