Created
August 9, 2015 22:11
-
-
Save JoelOtter/b97d207acbbca414baba to your computer and use it in GitHub Desktop.
Jest superagent mock in CoffeeScript
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
mockDelay = {} | |
mockError = {} | |
mockResponse = { | |
status: -> | |
return 200 | |
ok: -> | |
return true | |
body: { | |
walla: true | |
} | |
get: jest.genMockFunction() | |
toError: jest.genMockFunction() | |
} | |
Request = { | |
post: -> | |
this | |
get: -> | |
this | |
send: -> | |
this | |
query: -> | |
this | |
field: -> | |
this | |
set: -> | |
this | |
accept: -> | |
this | |
timeout: -> | |
this | |
end: jest.genMockFunction().mockImplementation((callback) -> | |
if mockDelay | |
this.delayTimer = setTimeout(callback, 0, mockError, mockResponse) | |
return | |
callback(mockError, mockResponse) | |
) | |
# expose helper methods for tests to set | |
__setMockDelay: (boolValue) -> | |
mockDelay = boolValue | |
__setMockResponse: (mockRes) -> | |
mockResponse = mockRes | |
__setMockError: (mockErr) -> | |
mockErr = mockErr | |
} | |
module.exports = Request |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment