Commit e605e6f0 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu Committed by Lukas Eipert

Fix counting of pending AJAX requests

@rails/ujs uses native XHR rather than jQuery AJAX. Thus we add event
listeners to count pending Rails UJS requests. This uses the same
methodology as our request counting for axios.
parent 7f4ca6bd
...@@ -5,6 +5,16 @@ export const initRails = () => { ...@@ -5,6 +5,16 @@ export const initRails = () => {
// eslint-disable-next-line no-underscore-dangle // eslint-disable-next-line no-underscore-dangle
if (!window._rails_loaded) { if (!window._rails_loaded) {
Rails.start(); Rails.start();
// Count XHR requests for tests. See spec/support/helpers/wait_for_requests.rb
window.pendingRailsUJSRequests = 0;
document.body.addEventListener('ajax:complete', () => {
window.pendingRailsUJSRequests -= 1;
});
document.body.addEventListener('ajax:beforeSend', () => {
window.pendingRailsUJSRequests += 1;
});
} }
}; };
......
...@@ -9,20 +9,14 @@ module QA ...@@ -9,20 +9,14 @@ module QA
def wait_for_requests(skip_finished_loading_check: false) def wait_for_requests(skip_finished_loading_check: false)
Waiter.wait_until(log: false) do Waiter.wait_until(log: false) do
finished_all_ajax_requests? && finished_all_axios_requests? && (!skip_finished_loading_check ? finished_loading?(wait: 1) : true) finished_all_ajax_requests? && (!skip_finished_loading_check ? finished_loading?(wait: 1) : true)
end end
rescue Repeater::WaitExceededError rescue Repeater::WaitExceededError
raise $!, 'Page did not fully load. This could be due to an unending async request or loading icon.' raise $!, 'Page did not fully load. This could be due to an unending async request or loading icon.'
end end
def finished_all_axios_requests?
Capybara.page.evaluate_script('window.pendingRequests || 0').zero? # rubocop:disable Style/NumericPredicate
end
def finished_all_ajax_requests? def finished_all_ajax_requests?
return true if Capybara.page.evaluate_script('typeof jQuery === "undefined"') Capybara.page.evaluate_script('window.pendingRequests || window.pendingRailsUJSRequests || 0').zero? # rubocop:disable Style/NumericPredicate
Capybara.page.evaluate_script('jQuery.active').zero? # rubocop:disable Style/NumericPredicate
end end
def finished_loading?(wait: DEFAULT_MAX_WAIT_TIME) def finished_loading?(wait: DEFAULT_MAX_WAIT_TIME)
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
describe QA::Support::WaitForRequests do describe QA::Support::WaitForRequests do
describe '.wait_for_requests' do describe '.wait_for_requests' do
before do before do
allow(subject).to receive(:finished_all_axios_requests?).and_return(true)
allow(subject).to receive(:finished_all_ajax_requests?).and_return(true) allow(subject).to receive(:finished_all_ajax_requests?).and_return(true)
allow(subject).to receive(:finished_loading?).and_return(true) allow(subject).to receive(:finished_loading?).and_return(true)
end end
......
...@@ -48,17 +48,10 @@ module WaitForRequests ...@@ -48,17 +48,10 @@ module WaitForRequests
def finished_all_js_requests? def finished_all_js_requests?
return true unless javascript_test? return true unless javascript_test?
finished_all_ajax_requests? && finished_all_ajax_requests?
finished_all_axios_requests?
end
def finished_all_axios_requests?
Capybara.page.evaluate_script('window.pendingRequests || 0').zero? # rubocop:disable Style/NumericPredicate
end end
def finished_all_ajax_requests? def finished_all_ajax_requests?
return true if Capybara.page.evaluate_script('typeof jQuery === "undefined"') Capybara.page.evaluate_script('window.pendingRequests || window.pendingRailsUJSRequests || 0').zero? # rubocop:disable Style/NumericPredicate
Capybara.page.evaluate_script('jQuery.active').zero? # rubocop:disable Style/NumericPredicate
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment