a11y(select): cleanup select helpers in specs

This commit is contained in:
Paul Chavard 2022-01-05 11:46:49 +01:00
parent 968384952a
commit cb66348916
7 changed files with 51 additions and 68 deletions

View file

@ -103,41 +103,27 @@ module SystemHelpers
end end
end end
def select_combobox(champ, fill_with, value) def select_combobox(libelle, fill_with, value, check: true)
fill_in champ, with: fill_with fill_in libelle, with: fill_with
selector = "li[data-option-value=\"#{value}\"]" selector = "li[data-option-value=\"#{value}\"]"
find(selector).click find(selector).click
expect(page).to have_css(selector) if check
expect(page).to have_css("[type=\"hidden\"][value=\"#{value}\"]") check_selected_value(libelle, with: value)
end
end end
def select_multi_combobox(champ, fill_with, value) def check_selected_value(libelle, with:)
input = find("input[aria-label=\"#{champ}\"") field = find_hidden_field_for(libelle)
input.click value = field.value.starts_with?('[') ? JSON.parse(field.value) : field.value
input.fill_in with: fill_with if value.is_a?(Array)
selector = "li[data-option-value=\"#{value}\"]" if with.is_a?(Array)
find(selector).click expect(value.sort).to eq(with.sort)
check_selected_value(champ, value) else
expect(value).to include(with)
end end
else
def check_selected_values(champ, values) expect(value).to eq(with)
combobox = find(:xpath, "//input[@aria-label=\"#{champ}\"]/ancestor::div[@data-react-class='ComboMultipleDropdownList']")
hidden_field_id = JSON.parse(combobox["data-react-props"])["hiddenFieldId"]
hidden_field = find("input[data-uuid=\"#{hidden_field_id}\"]")
hidden_field_values = JSON.parse(hidden_field.value)
expect(values.sort).to eq(hidden_field_values.sort)
end end
def check_selected_value(champ, value)
combobox = find(:xpath, "//input[@aria-label=\"#{champ}\"]/ancestor::div[@data-react-class='ComboMultipleDropdownList']")
hidden_field_id = JSON.parse(combobox["data-react-props"])["hiddenFieldId"]
hidden_field = find("input[data-uuid=\"#{hidden_field_id}\"]")
hidden_field_values = JSON.parse(hidden_field.value)
expect(hidden_field_values).to include(value)
end
def have_hidden_field(libelle, with:)
have_css("##{form_id_for(libelle)}[value=\"#{with}\"]")
end end
def log_out def log_out
@ -172,6 +158,18 @@ module SystemHelpers
end end
end end
end end
def find_hidden_field_for(libelle, name: 'value')
find("#{form_group_id_for(libelle)} input[type=\"hidden\"][name$=\"[#{name}]\"]")
end
def form_group_id_for(libelle)
"#champ-#{form_id_for(libelle).gsub('-input', '')}"
end
def form_id_for(libelle)
find(:xpath, ".//label[contains(text()[normalize-space()], '#{libelle}')]")[:for]
end
end end
RSpec.configure do |config| RSpec.configure do |config|

View file

@ -9,7 +9,7 @@ describe 'As an instructeur', js: true do
visit admin_procedure_path(procedure) visit admin_procedure_path(procedure)
find('#groupe-instructeurs').click find('#groupe-instructeurs').click
find("input[aria-label='email instructeur'").send_keys(instructeur_email, :enter) fill_in 'Emails', with: instructeur_email
perform_enqueued_jobs { click_on 'Affecter' } perform_enqueued_jobs { click_on 'Affecter' }
expect(page).to have_text("Les instructeurs ont bien été affectés à la démarche") expect(page).to have_text("Les instructeurs ont bien été affectés à la démarche")

View file

@ -165,8 +165,8 @@ describe 'Instructing a dossier:', js: true do
click_on 'Personnes impliquées' click_on 'Personnes impliquées'
select_multi_combobox('email instructeur', instructeur_2.email, instructeur_2.id) select_combobox('Emails', instructeur_2.email, instructeur_2.id, check: false)
select_multi_combobox('email instructeur', instructeur_3.email, instructeur_3.id) select_combobox('Emails', instructeur_3.email, instructeur_3.id, check: false)
click_on 'Envoyer' click_on 'Envoyer'

View file

@ -125,13 +125,13 @@ describe "procedure filters" do
def add_column(column_name, column_path) def add_column(column_name, column_path)
click_on 'Personnaliser' click_on 'Personnaliser'
select_multi_combobox('colonne', column_name, column_path) select_combobox('Colonne à afficher', column_name, column_path, check: false)
click_button "Enregistrer" click_button "Enregistrer"
end end
def remove_column(column_name) def remove_column(column_name)
click_on 'Personnaliser' click_on 'Personnaliser'
find(:xpath, ".//li[contains(text(), \"#{column_name}\")]/button", text: 'Désélectionner').click click_button column_name
find("body").native.send_key("Escape") find("body").native.send_key("Escape")
click_button "Enregistrer" click_button "Enregistrer"
end end

View file

@ -30,14 +30,14 @@ describe 'The routing', js: true do
expect(page).to have_field('Nom du groupe', with: 'littéraire') expect(page).to have_field('Nom du groupe', with: 'littéraire')
# add victor to littéraire groupe # add victor to littéraire groupe
find("input[aria-label='email instructeur'").send_keys('victor@inst.com', :enter) fill_in 'Emails', with: 'victor@inst.com'
perform_enqueued_jobs { click_on 'Affecter' } perform_enqueued_jobs { click_on 'Affecter' }
expect(page).to have_text("Linstructeur victor@inst.com a été affecté au groupe « littéraire »") expect(page).to have_text("Linstructeur victor@inst.com a été affecté au groupe « littéraire »")
victor = User.find_by(email: 'victor@inst.com').instructeur victor = User.find_by(email: 'victor@inst.com').instructeur
# add superwoman to littéraire groupe # add superwoman to littéraire groupe
find("input[aria-label='email instructeur'").send_keys('superwoman@inst.com', :enter) fill_in 'Emails', with: 'superwoman@inst.com'
perform_enqueued_jobs { click_on 'Affecter' } perform_enqueued_jobs { click_on 'Affecter' }
expect(page).to have_text("Linstructeur superwoman@inst.com a été affecté au groupe « littéraire »") expect(page).to have_text("Linstructeur superwoman@inst.com a été affecté au groupe « littéraire »")
@ -50,14 +50,14 @@ describe 'The routing', js: true do
expect(page).to have_text('Le groupe dinstructeurs « scientifique » a été créé.') expect(page).to have_text('Le groupe dinstructeurs « scientifique » a été créé.')
# add marie to scientifique groupe # add marie to scientifique groupe
find("input[aria-label='email instructeur'").send_keys('marie@inst.com', :enter) fill_in 'Emails', with: 'marie@inst.com'
perform_enqueued_jobs { click_on 'Affecter' } perform_enqueued_jobs { click_on 'Affecter' }
expect(page).to have_text("Linstructeur marie@inst.com a été affecté") expect(page).to have_text("Linstructeur marie@inst.com a été affecté")
marie = User.find_by(email: 'marie@inst.com').instructeur marie = User.find_by(email: 'marie@inst.com').instructeur
# add superwoman to scientifique groupe # add superwoman to scientifique groupe
find("input[aria-label='email instructeur'").send_keys('superwoman@inst.com', :enter) fill_in 'Emails', with: 'superwoman@inst.com'
perform_enqueued_jobs { click_on 'Affecter' } perform_enqueued_jobs { click_on 'Affecter' }
expect(page).to have_text("Linstructeur superwoman@inst.com a été affecté") expect(page).to have_text("Linstructeur superwoman@inst.com a été affecté")
@ -112,7 +112,7 @@ describe 'The routing', js: true do
click_on litteraire_user.dossiers.first.id.to_s click_on litteraire_user.dossiers.first.id.to_s
click_on 'Modifier mon dossier' click_on 'Modifier mon dossier'
fill_in 'dossier_champs_attributes_0_value', with: 'some value' fill_in litteraire_user.dossiers.first.champs.first.libelle, with: 'some value'
click_on 'Enregistrer les modifications du dossier' click_on 'Enregistrer les modifications du dossier'
log_out log_out

View file

@ -28,13 +28,13 @@ describe 'The user' do
check('val1') check('val1')
check('val3') check('val3')
select('bravo', from: form_id_for('simple_choice_drop_down_list_long')) select('bravo', from: form_id_for('simple_choice_drop_down_list_long'))
select_multi_combobox('multiple_choice_drop_down_list_long', 'alp', 'alpha') select_combobox('multiple_choice_drop_down_list_long', 'alp', 'alpha')
select_multi_combobox('multiple_choice_drop_down_list_long', 'cha', 'charly') select_combobox('multiple_choice_drop_down_list_long', 'cha', 'charly')
select_combobox('pays', 'aust', 'Australie') select_combobox('pays', 'aust', 'Australie')
select_combobox('regions', 'Ma', 'Martinique') select_combobox('regions', 'Ma', 'Martinique')
select_combobox('departements', 'Ai', '02 - Aisne') select_combobox('departements', 'Ai', '02 - Aisne')
select_combobox('communes', 'Ai', '02 - Aisne') select_combobox('communes', 'Ai', '02 - Aisne', check: false)
select_combobox('communes', 'Ambl', 'Ambléon (01300)') select_combobox('communes', 'Ambl', 'Ambléon (01300)')
check('engagement') check('engagement')
@ -87,11 +87,11 @@ describe 'The user' do
expect(page).to have_checked_field('val1') expect(page).to have_checked_field('val1')
expect(page).to have_checked_field('val3') expect(page).to have_checked_field('val3')
expect(page).to have_selected_value('simple_choice_drop_down_list_long', selected: 'bravo') expect(page).to have_selected_value('simple_choice_drop_down_list_long', selected: 'bravo')
check_selected_values('multiple_choice_drop_down_list_long', ['alpha', 'charly']) check_selected_value('multiple_choice_drop_down_list_long', with: ['alpha', 'charly'])
expect(page).to have_hidden_field('pays', with: 'Australie') check_selected_value('pays', with: 'Australie')
expect(page).to have_hidden_field('regions', with: 'Martinique') check_selected_value('regions', with: 'Martinique')
expect(page).to have_hidden_field('departements', with: '02 - Aisne') check_selected_value('departements', with: '02 - Aisne')
expect(page).to have_hidden_field('communes', with: 'Ambléon (01300)') check_selected_value('communes', with: 'Ambléon (01300)')
expect(page).to have_checked_field('engagement') expect(page).to have_checked_field('engagement')
expect(page).to have_field('dossier_link', with: '123') expect(page).to have_field('dossier_link', with: '123')
expect(page).to have_text('file.pdf') expect(page).to have_text('file.pdf')
@ -337,10 +337,6 @@ describe 'The user' do
expect(page).to have_current_path(identite_dossier_path(user_dossier)) expect(page).to have_current_path(identite_dossier_path(user_dossier))
end end
def form_id_for(libelle)
find(:xpath, ".//label[contains(text()[normalize-space()], '#{libelle}')]")[:for]
end
def form_id_for_datetime(libelle) def form_id_for_datetime(libelle)
# The HTML for datetime is a bit specific since it has 5 selects, below here is a sample HTML # The HTML for datetime is a bit specific since it has 5 selects, below here is a sample HTML
# So, we want to find the partial id of a datetime (partial because there are 5 ids: # So, we want to find the partial id of a datetime (partial because there are 5 ids:

View file

@ -27,16 +27,16 @@ describe 'linked dropdown lists' do
fill_individual fill_individual
# Select a primary value # Select a primary value
select('Primary 2', from: primary_id_for('linked dropdown')) select('Primary 2', from: 'linked dropdown')
# Secondary menu reflects chosen primary value # Secondary menu reflects chosen primary value
expect(page).to have_select(secondary_id_for('linked dropdown'), options: ['', 'Secondary 2.1', 'Secondary 2.2', 'Secondary 2.3']) expect(page).to have_select("Valeur secondaire dépendant de la première", options: ['', 'Secondary 2.1', 'Secondary 2.2', 'Secondary 2.3'])
# Select another primary value # Select another primary value
select('Primary 1', from: primary_id_for('linked dropdown')) select('Primary 1', from: 'linked dropdown')
# Secondary menu gets updated # Secondary menu gets updated
expect(page).to have_select(secondary_id_for('linked dropdown'), options: ['', 'Secondary 1.1', 'Secondary 1.2']) expect(page).to have_select("Valeur secondaire dépendant de la première", options: ['', 'Secondary 1.1', 'Secondary 1.2'])
end end
private private
@ -63,15 +63,4 @@ describe 'linked dropdown lists' do
click_on 'Continuer' click_on 'Continuer'
expect(page).to have_current_path(brouillon_dossier_path(user_dossier)) expect(page).to have_current_path(brouillon_dossier_path(user_dossier))
end end
def primary_id_for(libelle)
find(:xpath, ".//label[contains(text()[normalize-space()], '#{libelle}')]")[:for]
end
def secondary_id_for(libelle)
primary_id = primary_id_for(libelle)
find("\##{primary_id}")
.ancestor('.editable-champ')
.find("[data-secondary]")['id']
end
end end