Skip to content

Instantly share code, notes, and snippets.

@kimjoar
Created May 10, 2013 08:14
Show Gist options
  • Save kimjoar/5553104 to your computer and use it in GitHub Desktop.
Save kimjoar/5553104 to your computer and use it in GitHub Desktop.
High-level helpers for Sinon.js
define(function(require) {
var sinon = require('sinon');
var _ = require('underscore');
// fakeResponse
// ------------
//
// High-level helper for responding equally to all Ajax requests.
//
// **Parameters:**
//
// `response` specifies the response to all Ajax requests. It can be
// specified either as a string or as an object. In the latter case the
// object is transformed into JSON using `JSON.stringify`.
//
// `options` is an optional hash with two keys:
//
// - `statusCode` which specify the status code for all requests. Defaults
// to 200 OK.
// - `headers` which specify the headers for all requests. Defaults to
// specifying JSON as the content type.
//
// `callback` is the callback which should trigger an Ajax request, i.e. this
// callback should include those Ajax requests that should have the specified
// response.
//
//
// **Example:**
//
// var response = { error: "Everything failed" };
// fakeResponse(response, { statusCode: 403 }, function() {
// // perform ajax request
// });
var fakeResponse = function(responseBody, options, callback) {
if (typeof options === "undefined" && typeof callback === "undefined" && typeof responseBody === "function") {
callback = responseBody;
responseBody = {};
options = {};
}
if (typeof callback === "undefined" && typeof options === "function") {
callback = options;
options = {};
}
_.defaults(options, {
statusCode: 200,
headers: { "Content-Type": "application/json" }
});
var response = [options.statusCode, options.headers, stringify(responseBody)];
respondWith(options.url, response, callback);
};
var respondWith = function(url, response, callback) {
var server = sinon.fakeServer.create();
if (url) {
server.respondWith(url, response);
} else {
server.respondWith(response);
}
callback();
server.respond();
server.restore();
};
var stringify = function(data) {
if (typeof data !== "string") {
return JSON.stringify(data);
}
return data;
};
return fakeResponse;
});
define(function(require) {
var sinon = require('sinon');
var _ = require('underscore');
// fakeResponses
// -------------
//
// High-level helper for responding to sevaral Ajax requests.
//
// **Parameters:**
//
// `responses` is an array of responses. Each response must specify:
//
// - URL, which can either be a string or a regular expression.
//
// Each response can optionally specify:
//
// - method, the HTTP verb for the request. Defaults to GET.
// - status, the HTTP status code for the response. Defaults to 200 OK.
// - headers, the HTTP response headers. Defaults to setting content type to "application/json"
// - body, the HTTP response body. Defaults to empty object, i.e. the JSON string "{}"
//
// `callback` is the callback which should trigger the Ajax requests, i.e. this
// callback should include those Ajax requests that should have the specified
// responses.
//
//
// **Example:**
//
// var responses = [{
// url: /\/cart\/items/i,
// method: "DELETE",
// status: 404
// }, {
// url: /\/cart/i,
// body: newCosts
// }];
//
// fakeResponses(responses, function() {
// // perform ajax requests
// });
var fakeResponses = function(responses, callback) {
var server = sinon.fakeServer.create();
var defaultHeaders = { "Content-Type": "application/json" };
_.each(responses, function(response) {
var url = response.url;
var method = response.method || 'GET';
var status = response.status || 200;
var headers = response.headers || defaultHeaders;
var body = stringify(response.body || {});
server.respondWith(method, url, [status, headers, body]);
});
callback();
server.respond();
server.restore();
};
var stringify = function(data) {
if (typeof data !== "string") {
return JSON.stringify(data);
}
return data;
};
return fakeResponses;
});
define(function(require) {
var sinon = require('sinon');
// allRequests
// ------------
//
// High-level helper for collecting Ajax requests when using Sinon.JS.
// Returns an array of all requests performed during the callback.
//
// **Parameters:**
//
// `callback` is the callback which should trigger Ajax requests
//
// The interface for the responses can be found at
// http://sinonjs.org/docs/#FakeXMLHttpRequest
//
//
// **Example:**
//
// var requests = allRequests(function() {
// // perform ajax requests
// });
var allRequests = function(callback) {
var xhr = sinon.useFakeXMLHttpRequest();
var requests = [];
xhr.onCreate = function (xhr) {
requests.push(xhr);
};
callback();
xhr.restore();
return requests;
};
return allRequests;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment