Skip to content

Instantly share code, notes, and snippets.

@AlphaGit
Last active August 29, 2015 14:00
Show Gist options
  • Save AlphaGit/11225226 to your computer and use it in GitHub Desktop.
Save AlphaGit/11225226 to your computer and use it in GitHub Desktop.
Approach for Ember 1.4+ and emberHttpRespond
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(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;
};
//should we do this or should we just use these as global functions? (Using EAK some of them are required anyway, so these would not be global.)
//Ember.Test.registerHelper('queueHttpResponse', queueHttpResponse);
//Ember.Test.registerHelper('clearHttpResponseQueue', clearHttpResponseQueue);
export default {
clearHttpResponseQueue: clearHttpResponseQueue,
queueHttpResponse: queueHttpResponse
};
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
});
});
@AlphaGit
Copy link
Author

This gist gaps the inconsistency between ember-testing-httpRespond and ember 1.4+. Details here.

While this does not allow for the tests to assert the system state while the ajax is not yet completed, it does solve the halting problem that the tests have after the upgrade to Ember 1.4.

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