2017-04-13 12:31:33 -04:00
|
|
|
require_relative './wait_for_ajax'
|
2017-05-09 00:15:34 -04:00
|
|
|
require_relative './wait_for_vue_resource'
|
2017-04-13 12:31:33 -04:00
|
|
|
|
2017-03-09 21:28:20 -05:00
|
|
|
module WaitForRequests
|
|
|
|
extend self
|
2017-04-13 12:31:33 -04:00
|
|
|
include WaitForAjax
|
2017-05-09 00:15:34 -04:00
|
|
|
include WaitForVueResource
|
2017-03-09 21:28:20 -05:00
|
|
|
|
|
|
|
# This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests
|
|
|
|
def wait_for_requests_complete
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.block_requests!
|
|
|
|
wait_for('pending AJAX requests complete') do
|
2017-05-12 18:10:12 -04:00
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero?
|
2017-03-09 21:28:20 -05:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
|
|
|
|
end
|
|
|
|
|
|
|
|
# Waits until the passed block returns true
|
|
|
|
def wait_for(condition_name, max_wait_time: Capybara.default_max_wait_time, polling_interval: 0.01)
|
|
|
|
wait_until = Time.now + max_wait_time.seconds
|
|
|
|
loop do
|
|
|
|
break if yield
|
|
|
|
if Time.now > wait_until
|
|
|
|
raise "Condition not met: #{condition_name}"
|
|
|
|
else
|
|
|
|
sleep(polling_interval)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.configure do |config|
|
|
|
|
config.after(:each, :js) do
|
|
|
|
wait_for_requests_complete
|
|
|
|
end
|
|
|
|
end
|