Commit ed3b23fe authored by Mike Greiling's avatar Mike Greiling

refactor wait_for_requests, add slow_requests block helper

parent 187a4f6c
...@@ -43,11 +43,11 @@ describe 'Dropdown assignee', :js do ...@@ -43,11 +43,11 @@ describe 'Dropdown assignee', :js do
end end
it 'should show loading indicator when opened' do it 'should show loading indicator when opened' do
Gitlab::Testing::RequestBlockerMiddleware.slow_requests! slow_requests do
filtered_search.set('assignee:') filtered_search.set('assignee:')
expect(page).to have_css('#js-dropdown-assignee .filter-dropdown-loading', visible: true) expect(page).to have_css('#js-dropdown-assignee .filter-dropdown-loading', visible: true)
Gitlab::Testing::RequestBlockerMiddleware.allow_requests! end
end end
it 'should hide loading indicator when loaded' do it 'should hide loading indicator when loaded' do
......
...@@ -51,11 +51,11 @@ describe 'Dropdown author', :js do ...@@ -51,11 +51,11 @@ describe 'Dropdown author', :js do
end end
it 'should show loading indicator when opened' do it 'should show loading indicator when opened' do
Gitlab::Testing::RequestBlockerMiddleware.slow_requests! slow_requests do
filtered_search.set('author:') filtered_search.set('author:')
expect(page).to have_css('#js-dropdown-author .filter-dropdown-loading', visible: true) expect(page).to have_css('#js-dropdown-author .filter-dropdown-loading', visible: true)
Gitlab::Testing::RequestBlockerMiddleware.allow_requests! end
end end
it 'should hide loading indicator when loaded' do it 'should hide loading indicator when loaded' do
......
...@@ -70,11 +70,11 @@ describe 'Dropdown emoji', :js do ...@@ -70,11 +70,11 @@ describe 'Dropdown emoji', :js do
end end
it 'should show loading indicator when opened' do it 'should show loading indicator when opened' do
Gitlab::Testing::RequestBlockerMiddleware.slow_requests! slow_requests do
filtered_search.set('my-reaction:') filtered_search.set('my-reaction:')
expect(page).to have_css('#js-dropdown-my-reaction .filter-dropdown-loading', visible: true) expect(page).to have_css('#js-dropdown-my-reaction .filter-dropdown-loading', visible: true)
Gitlab::Testing::RequestBlockerMiddleware.allow_requests! end
end end
it 'should hide loading indicator when loaded' do it 'should hide loading indicator when loaded' do
......
...@@ -66,12 +66,12 @@ describe 'Dropdown label', :js do ...@@ -66,12 +66,12 @@ describe 'Dropdown label', :js do
end end
it 'shows loading indicator when opened and hides it when loaded' do it 'shows loading indicator when opened and hides it when loaded' do
Gitlab::Testing::RequestBlockerMiddleware.slow_requests! slow_requests do
filtered_search.set('label:') filtered_search.set('label:')
expect(page).to have_css("#{js_dropdown_label} .filter-dropdown-loading", visible: true) expect(page).to have_css("#{js_dropdown_label} .filter-dropdown-loading", visible: true)
end
expect(find(js_dropdown_label)).not_to have_css('.filter-dropdown-loading') expect(find(js_dropdown_label)).not_to have_css('.filter-dropdown-loading')
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
end end
it 'loads all the labels when opened' do it 'loads all the labels when opened' do
......
...@@ -50,11 +50,11 @@ describe 'Dropdown milestone', :js do ...@@ -50,11 +50,11 @@ describe 'Dropdown milestone', :js do
end end
it 'should show loading indicator when opened' do it 'should show loading indicator when opened' do
Gitlab::Testing::RequestBlockerMiddleware.slow_requests! slow_requests do
filtered_search.set('milestone:') filtered_search.set('milestone:')
expect(page).to have_css('#js-dropdown-milestone .filter-dropdown-loading', visible: true) expect(page).to have_css('#js-dropdown-milestone .filter-dropdown-loading', visible: true)
Gitlab::Testing::RequestBlockerMiddleware.allow_requests! end
end end
it 'should hide loading indicator when loaded' do it 'should hide loading indicator when loaded' do
......
...@@ -179,7 +179,7 @@ describe 'User browses files' do ...@@ -179,7 +179,7 @@ describe 'User browses files' do
click_button('Upload file') click_button('Upload file')
end end
block_and_wait_for_requests_complete wait_for_all_requests
visit(project_blob_path(project, 'new_branch_name/logo_sample.svg')) visit(project_blob_path(project, 'new_branch_name/logo_sample.svg'))
......
...@@ -21,24 +21,12 @@ feature 'User uploads file to note' do ...@@ -21,24 +21,12 @@ feature 'User uploads file to note' do
end end
context 'uploading is in progress' do context 'uploading is in progress' do
before do
Gitlab::Testing::RequestBlockerMiddleware.slow_requests!
end
after do
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
end
it 'shows "Cancel" button on uploading', :js do
dropzone_file([Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false)
expect(page).to have_button('Cancel')
end
it 'cancels uploading on clicking to "Cancel" button', :js do it 'cancels uploading on clicking to "Cancel" button', :js do
slow_requests do
dropzone_file([Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false) dropzone_file([Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false)
click_button 'Cancel' click_button 'Cancel'
end
expect(page).to have_button('Attach a file') expect(page).to have_button('Attach a file')
expect(page).not_to have_button('Cancel') expect(page).not_to have_button('Cancel')
...@@ -46,17 +34,21 @@ feature 'User uploads file to note' do ...@@ -46,17 +34,21 @@ feature 'User uploads file to note' do
end end
it 'shows "Attaching a file" message on uploading 1 file', :js do it 'shows "Attaching a file" message on uploading 1 file', :js do
slow_requests do
dropzone_file([Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false) dropzone_file([Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false)
expect(page).to have_selector('.attaching-file-message', visible: true, text: 'Attaching a file -') expect(page).to have_selector('.attaching-file-message', visible: true, text: 'Attaching a file -')
end end
end
it 'shows "Attaching 2 files" message on uploading 2 file', :js do it 'shows "Attaching 2 files" message on uploading 2 file', :js do
slow_requests do
dropzone_file([Rails.root.join('spec', 'fixtures', 'video_sample.mp4'), dropzone_file([Rails.root.join('spec', 'fixtures', 'video_sample.mp4'),
Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false) Rails.root.join('spec', 'fixtures', 'dk.png')], 0, false)
expect(page).to have_selector('.attaching-file-message', visible: true, text: 'Attaching 2 files -') expect(page).to have_selector('.attaching-file-message', visible: true, text: 'Attaching 2 files -')
end end
end
it 'shows error message, "retry" and "attach a new file" link a if file is too big', :js do it 'shows error message, "retry" and "attach a new file" link a if file is too big', :js do
dropzone_file([Rails.root.join('spec', 'fixtures', 'video_sample.mp4')], 0.01) dropzone_file([Rails.root.join('spec', 'fixtures', 'video_sample.mp4')], 0.01)
......
...@@ -9,7 +9,7 @@ module InspectRequests ...@@ -9,7 +9,7 @@ module InspectRequests
yield yield
block_and_wait_for_requests_complete wait_for_all_requests
Gitlab::Testing::RequestInspectorMiddleware.requests Gitlab::Testing::RequestInspectorMiddleware.requests
ensure ensure
Gitlab::Testing::RequestInspectorMiddleware.stop_logging! Gitlab::Testing::RequestInspectorMiddleware.stop_logging!
......
require_relative './wait_for_requests'
module WaitForRequests module WaitForRequests
extend self extend self
# This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests # This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests
def block_and_wait_for_requests_complete def block_and_wait_for_requests_complete
block_requests { wait_for_all_requests }
end
# Block all requests inside block with 503 response
def block_requests
Gitlab::Testing::RequestBlockerMiddleware.block_requests! Gitlab::Testing::RequestBlockerMiddleware.block_requests!
wait_for('pending requests complete') do yield
Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero? && finished_all_requests? ensure
Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
end end
# Slow down requests inside block by injecting `sleep 0.2` before each response
def slow_requests
Gitlab::Testing::RequestBlockerMiddleware.slow_requests!
yield
ensure ensure
Gitlab::Testing::RequestBlockerMiddleware.allow_requests! Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
end end
# Wait for client-side AJAX requests
def wait_for_requests def wait_for_requests
wait_for('JS requests') { finished_all_requests? } wait_for('JS requests complete') { finished_all_js_requests? }
end
# Wait for active Rack requests and client-side AJAX requests
def wait_for_all_requests
wait_for('pending requests complete') do
finished_all_rack_reqiests? &&
finished_all_js_requests?
end
end end
private private
def finished_all_requests? def finished_all_rack_reqiests?
Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero?
end
def finished_all_js_requests?
return true unless javascript_test? return true unless javascript_test?
finished_all_ajax_requests? && finished_all_ajax_requests? &&
......
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