From 1573fc495a3b7d938b52e4a8a1e2232f4af4c300 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 29 Nov 2024 15:58:02 +0100 Subject: [PATCH] pj attachment input described_by add champ.error_id target --- app/components/attachment/edit_component.rb | 2 ++ .../attachment/edit_component_spec.rb | 25 ++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index e52d3379df9..c4404a6dc80 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -88,6 +88,8 @@ def file_field_options describedby = [] describedby << champ.describedby_id if champ&.description.present? describedby << describedby_hint_id if show_hint? + describedby << champ.error_id if champ&.errors&.has_key?(:value) + options[:aria] = { describedby: describedby.join(' ') } options.merge!(has_content_type_validator? ? { accept: accept_content_type } : {}) diff --git a/spec/components/attachment/edit_component_spec.rb b/spec/components/attachment/edit_component_spec.rb index ffbebc9b83a..73f940c64ac 100644 --- a/spec/components/attachment/edit_component_spec.rb +++ b/spec/components/attachment/edit_component_spec.rb @@ -36,14 +36,27 @@ expect(subject).to have_content(/Formats supportés : jpeg, png/) end - it 'sets up its aria describedby' do - subject + describe 'aria describedby' do + let(:describedby_attribute) { page.find('input')['aria-describedby'].split } - hint_element = page.find('.fr-hint-text') - expect(hint_element['id']).to eq("#{champ.input_id}-pj-hint") + it 'targets describedby_id and pj-hint' do + subject - input_describedby = page.find('input')['aria-describedby'].split - expect(input_describedby).to eq([champ.describedby_id, "#{champ.input_id}-pj-hint"]) + hint_element = page.find('.fr-hint-text') + expect(hint_element['id']).to eq("#{champ.input_id}-pj-hint") + + expect(describedby_attribute).to eq([champ.describedby_id, "#{champ.input_id}-pj-hint"]) + end + + context 'when there is an error' do + before { champ.errors.add(:value, 'is invalid') } + + it 'targets error_id' do + subject + + expect(describedby_attribute).to eq([champ.describedby_id, "#{champ.input_id}-pj-hint", champ.error_id]) + end + end end end