Skip to content

Instantly share code, notes, and snippets.

@nathanpalmer
Forked from AlphaGit/queueHttpResponder.js
Created April 24, 2014 15:25
Show Gist options
  • Save nathanpalmer/11258730 to your computer and use it in GitHub Desktop.
Save nathanpalmer/11258730 to your computer and use it in GitHub Desktop.
var httpResponseQueue = Ember.A();
var checkQueueInterval = null;
var checkQueue = function() {
for (var i = 0; i < httpResponseQueue.length; i++) {
var queuedItem = httpResponseQueue[i];
var found = fakehr.match(queuedItem.verb.toUpperCase(), queuedItem.url);
if (found) {
found.respond(queuedItem.status || 200, {'content-type': 'application/json'}, queuedItem.body);
}
}
};
var queueHttpResponse = function(app, verb, url, body, status) {
if(typeof body !== 'string') { body = JSON.stringify(body); }
var newItem = { verb: verb, url: url, body: body, status: status };
var existingQueuedItem = httpResponseQueue.find(function(item) {
return item.verb === newItem.verb && item.url === newItem.url;
});
if (existingQueuedItem) {
httpResponseQueue.removeObject(existingQueuedItem);
}
httpResponseQueue.push(newItem);
if (!checkQueueInterval) {
// we need to do this outside of the ember run loop or tests will get stuck anyway
checkQueueInterval = window.setInterval(checkQueue, 10);
}
};
var clearHttpResponseQueue = function() {
httpResponseQueue = Ember.A();
window.clearInterval(checkQueueInterval);
checkQueueInterval = null;
};
Ember.Test.registerHelper('queueHttpResponse', queueHttpResponse);
Ember.Test.registerHelper('clearHttpResponseQueue', clearHttpResponseQueue);
module('my tests', function() {
setup: function() {
fakehr.start();
queueHttpResponse('GET', '/1', { something: 1 });
queueHttpResponse('GET', '/2', { something: 2 });
},
teardown: function() {
fakehr.reset();
clearHttpResponseQueue();
}
});
test('Test1', function() {
// overriding one response for this test only
queueHttpResponse('GET', '/1', { something: "1.1" });
visit('/home');
andThen(function() {
// your asserts here
});
});
@nathanpalmer
Copy link
Author

Changed

  • Added "add" as the first parameter to queueHttpResponse
  • Enabled the helpers (versus exporting)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment