Skip to content

Instantly share code, notes, and snippets.

@bnorton
Last active August 29, 2015 14:21
Show Gist options
  • Save bnorton/9c7644deb97f53b789a4 to your computer and use it in GitHub Desktop.
Save bnorton/9c7644deb97f53b789a4 to your computer and use it in GitHub Desktop.
Ember.js meet Capybara
<!-- ... -->
<%= javascript_include_tag 'test' if Rails.env.test? %>
<!-- ... -->
module RequestHelper
def self.included(base)
base.send(:include, InstanceMethods)
end
module InstanceMethods
##
# Wait up to 3 seconds for asyncronous action processing to complete
def wait_for_async
start_time = Time.now
loop do
break if page.evaluate_script('Object.keys(test.ajaxRequests).length == 0 && !Ember.run.hasScheduledTimers() && !Ember.run.currentRunLoop')
raise "Waited too long for ajax requests to finish #{page.evaluate_script('JSON.stringify(test.ajaxRequests)')} - #{page.evaluate_script('!Ember.run.hasScheduledTimers()')} - #{page.evaluate_script('!Ember.run.currentRunLoop')}" if (Time.now - start_time) > 3
sleep 0.1
end
end
end
end
# ...
RSpec.configure do |config|
config.include RequestHelper, :type => :feature
end
# ...
test = {
ajaxRequests: {}
};
$(document).on('ajaxSend', function(e, xhr, options) {
test.ajaxRequests[options.url] = true;
}).on('ajaxComplete', function(e, xhr, options) {
delete test.ajaxRequests[options.url];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment