Skip to content

Instantly share code, notes, and snippets.

@patrickberkeley
Last active August 29, 2015 13:57
Show Gist options
  • Save patrickberkeley/9471631 to your computer and use it in GitHub Desktop.
Save patrickberkeley/9471631 to your computer and use it in GitHub Desktop.
Wrapper for mocking AJAX requests with Mockjax - https://github.com/appendto/jquery-mockjax
# Wrapper for mocking AJAX requests with Mockjax - https://github.com/appendto/jquery-mockjax
#
# Arguments:
#
# url - String, endpoint for request you want to capture
# response - Object, response you expect to be returned from the endpoint
# opts - Object (optional)
# opts.type - String, the HTTP request type. Default 'GET'
# opts.status - Integer, the HTTP response status code. Default 200
# opts.data - Object, data object passed with request. Default null
#
# Examples:
#
# mockRequest('/campaigns/1', {campaign: {id: 1, status: 'Active'}})
# mockRequest('/campaigns/1', {campaign: {id: 1, status: 'Inactive'}}, {type: 'PUT'})
# mockRequest('/campaigns', {[campaigns: {id: 1, status: 'Inactive'}, {id: 2, status: 'Active'}]}, {data: {page: 2}})
#
window.mockRequest = (url, response, opts) ->
options = $.extend {}, opts
namespace = $.mockjaxSettings.namespace || ''
url = namespace + url
type = options.type || 'GET'
status = options.status || 200
data = options.data || null
$.mockjax
url: url
dataType: 'json'
data: data
responseText: response
type: type
status: status
return
$.mockjaxSettings.namespace = '/adminv2/api'
$.mockjaxSettings.status = 200
$.mockjaxSettings.logging = true
$.mockjaxSettings.responseTime = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment