Wait for ajax (#3920)
Corrige certains tests automatisés qui échouaient aléatoirement
This commit is contained in:
commit
3ae6b64994
5 changed files with 41 additions and 95 deletions
|
@ -26,14 +26,17 @@ export function delegate(eventNames, selector, callback) {
|
|||
}
|
||||
|
||||
export function getJSON(url, data, method = 'get') {
|
||||
incrementActiveRequestsCount();
|
||||
data = method !== 'get' ? JSON.stringify(data) : data;
|
||||
return $.ajax({
|
||||
method,
|
||||
url,
|
||||
data,
|
||||
contentType: 'application/json',
|
||||
dataType: 'json'
|
||||
});
|
||||
return Promise.resolve(
|
||||
$.ajax({
|
||||
method,
|
||||
url,
|
||||
data,
|
||||
contentType: 'application/json',
|
||||
dataType: 'json'
|
||||
})
|
||||
).finally(decrementActiveRequestsCount);
|
||||
}
|
||||
|
||||
export function scrollTo(container, scrollTo) {
|
||||
|
@ -62,3 +65,15 @@ function offset(element) {
|
|||
left: rect.left + document.body.scrollLeft
|
||||
};
|
||||
}
|
||||
|
||||
const DATA_ACTIVE_REQUESTS_COUNT = 'data-active-requests-count';
|
||||
|
||||
function incrementActiveRequestsCount() {
|
||||
const count = document.body.getAttribute(DATA_ACTIVE_REQUESTS_COUNT) || '0';
|
||||
document.body.setAttribute(DATA_ACTIVE_REQUESTS_COUNT, parseInt(count) + 1);
|
||||
}
|
||||
|
||||
function decrementActiveRequestsCount() {
|
||||
const count = document.body.getAttribute(DATA_ACTIVE_REQUESTS_COUNT) || '0';
|
||||
document.body.setAttribute(DATA_ACTIVE_REQUESTS_COUNT, parseInt(count) - 1);
|
||||
}
|
||||
|
|
|
@ -1,77 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
feature 'add a new type de piece justificative', js: true do
|
||||
let(:administrateur) { create(:administrateur) }
|
||||
|
||||
before do
|
||||
login_as administrateur, scope: :administrateur
|
||||
end
|
||||
context 'when there is an existing piece justificative' do
|
||||
let(:procedure) { create(:procedure, administrateur: administrateur) }
|
||||
before do
|
||||
# Create a dummy PJ, because adding PJs is no longer allowed on procedures that
|
||||
# do not already have one
|
||||
procedure.types_de_piece_justificative.create(libelle: "dummy PJ")
|
||||
visit admin_procedure_pieces_justificatives_path(procedure)
|
||||
end
|
||||
scenario 'displays a form to add new type de piece justificative' do
|
||||
within '#new_type_de_piece_justificative' do
|
||||
expect(page).to have_css('#procedure_types_de_piece_justificative_attributes_1_libelle')
|
||||
end
|
||||
end
|
||||
context 'when user fills field and submit' do
|
||||
let(:libelle) { 'ma piece' }
|
||||
let(:description) { 'ma description' }
|
||||
before do
|
||||
page.find_by_id('procedure_types_de_piece_justificative_attributes_1_libelle').set(libelle)
|
||||
page.find_by_id('procedure_types_de_piece_justificative_attributes_1_description').set(description)
|
||||
page.click_on 'Ajouter la pièce'
|
||||
wait_for_ajax
|
||||
end
|
||||
subject do
|
||||
procedure.reload
|
||||
procedure.types_de_piece_justificative.second
|
||||
end
|
||||
scenario 'creates new type de piece' do
|
||||
expect(subject.libelle).to eq(libelle)
|
||||
expect(subject.description).to eq(description)
|
||||
end
|
||||
scenario 'displays new created pj' do
|
||||
within '#liste_piece_justificative' do
|
||||
expect(page).to have_css('#procedure_types_de_piece_justificative_attributes_1_libelle')
|
||||
expect(page.body).to match(libelle)
|
||||
expect(page.body).to match(description)
|
||||
end
|
||||
within '#new_type_de_piece_justificative' do
|
||||
expect(page).to have_css('#procedure_types_de_piece_justificative_attributes_2_libelle')
|
||||
end
|
||||
end
|
||||
context 'when user delete pj' do
|
||||
before do
|
||||
pj = procedure.types_de_piece_justificative.second
|
||||
page.find_by_id("delete_type_de_piece_justificative_#{pj.id}").click
|
||||
wait_for_ajax
|
||||
end
|
||||
scenario 'removes pj from page' do
|
||||
within '#liste_piece_justificative' do
|
||||
expect(page).not_to have_css('#procedure_types_de_piece_justificative_attributes_1_libelle')
|
||||
expect(page.body).not_to match(libelle)
|
||||
expect(page.body).not_to match(description)
|
||||
end
|
||||
end
|
||||
end
|
||||
context 'when user change existing type de pj' do
|
||||
let(:new_libelle) { 'mon nouveau libelle' }
|
||||
before do
|
||||
page.find_by_id('procedure_types_de_piece_justificative_attributes_1_libelle').set(new_libelle)
|
||||
page.find_by_id('save').click
|
||||
wait_for_ajax
|
||||
end
|
||||
scenario 'saves change in database' do
|
||||
pj = procedure.types_de_piece_justificative.second
|
||||
expect(pj.libelle).to eq(new_libelle)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -122,8 +122,9 @@ feature 'As an administrateur I can edit types de champ', js: true do
|
|||
it "Add carte champ" do
|
||||
select('Carte', from: 'champ-0-type_champ')
|
||||
fill_in 'champ-0-libelle', with: 'Libellé de champ carte', fill_options: { clear: :backspace }
|
||||
blur
|
||||
check 'Quartiers prioritaires'
|
||||
|
||||
wait_until { procedure.types_de_champ.first.quartiers_prioritaires == true }
|
||||
expect(page).to have_content('Formulaire enregistré')
|
||||
|
||||
preview_window = window_opened_by { click_on 'Prévisualiser le formulaire' }
|
||||
|
@ -136,9 +137,10 @@ feature 'As an administrateur I can edit types de champ', js: true do
|
|||
|
||||
it "Add dropdown champ" do
|
||||
select('Menu déroulant', from: 'champ-0-type_champ')
|
||||
fill_in 'champ-0-libelle', with: 'libellé de champ menu déroulant'
|
||||
blur
|
||||
fill_in 'champ-0-drop_down_list_value', with: 'Un menu'
|
||||
fill_in 'champ-0-libelle', with: 'Libellé de champ menu déroulant', fill_options: { clear: :backspace }
|
||||
fill_in 'champ-0-drop_down_list_value', with: 'Un menu', fill_options: { clear: :backspace }
|
||||
|
||||
wait_until { procedure.types_de_champ.first.drop_down_list&.value == 'Un menu' }
|
||||
expect(page).to have_content('Formulaire enregistré')
|
||||
|
||||
page.refresh
|
||||
|
|
|
@ -62,6 +62,18 @@ module FeatureHelpers
|
|||
def blur
|
||||
page.find('body').click
|
||||
end
|
||||
|
||||
def pause
|
||||
$stderr.write 'Spec paused. Press enter to continue:'
|
||||
$stdin.gets
|
||||
end
|
||||
|
||||
def wait_until
|
||||
Timeout.timeout(Capybara.default_max_wait_time) do
|
||||
sleep(0.1) until (value = yield)
|
||||
value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
RSpec.configure do |config|
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
module WaitForAjax
|
||||
def wait_for_ajax
|
||||
Timeout.timeout(Capybara.default_max_wait_time) do
|
||||
loop until finished_all_ajax_requests?
|
||||
end
|
||||
end
|
||||
|
||||
def finished_all_ajax_requests?
|
||||
page.evaluate_script('jQuery.active').zero?
|
||||
expect(page).to have_selector('body[data-active-requests-count="0"]')
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue