2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-04-14 10:55:36 +02:00
|
|
|
describe EditableChamp::SectionComponent, type: :component do
|
2023-04-12 17:55:08 +02:00
|
|
|
include TreeableConcern
|
2024-03-14 15:13:13 +01:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public:) }
|
|
|
|
let(:types_de_champ_public) { [] }
|
|
|
|
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
|
|
|
|
let(:types_de_champ) { dossier.revision.types_de_champ_public }
|
|
|
|
let(:component) { described_class.new(types_de_champ:, dossier:) }
|
2023-04-12 17:55:08 +02:00
|
|
|
before { render_inline(component).to_html }
|
|
|
|
|
|
|
|
context 'list of champs without an header_section' do
|
2024-03-14 15:13:13 +01:00
|
|
|
let(:types_de_champ_public) { [{ type: :text }, { type: :textarea }] }
|
2023-04-12 17:55:08 +02:00
|
|
|
|
2024-04-11 11:56:42 +02:00
|
|
|
it 'does not renders within a fieldset' do
|
|
|
|
expect(page).to have_selector("fieldset", count: 0)
|
2023-04-12 17:55:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders champs' do
|
|
|
|
expect(page).to have_selector("input[type=text]", count: 1)
|
|
|
|
expect(page).to have_selector("textarea", count: 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'list of champs with an header_section' do
|
2024-03-14 15:13:13 +01:00
|
|
|
let(:types_de_champ_public) { [{ type: :header_section, level: 1 }, { type: :text }, { type: :textarea }] }
|
2023-04-12 17:55:08 +02:00
|
|
|
|
|
|
|
it 'renders fieldset' do
|
|
|
|
expect(page).to have_selector("fieldset")
|
|
|
|
expect(page).to have_selector("legend h2")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders champs within fieldset' do
|
|
|
|
expect(page).to have_selector("fieldset input[type=text]")
|
|
|
|
expect(page).to have_selector("fieldset textarea")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'list of champs without section and an header_section having champs' do
|
2024-03-14 15:13:13 +01:00
|
|
|
let(:types_de_champ_public) { [{ type: :text }, { type: :header_section, level: 1 }, { type: :text }] }
|
2023-04-12 17:55:08 +02:00
|
|
|
|
2024-04-11 11:56:42 +02:00
|
|
|
it 'renders nested champs (after an header section) within a fieldset' do
|
|
|
|
expect(page).to have_selector("fieldset", count: 1)
|
|
|
|
expect(page).to have_selector("fieldset legend h2")
|
|
|
|
expect(page).to have_selector("input[type=text]", count: 2)
|
|
|
|
expect(page).to have_selector("fieldset input[type=text]", count: 1)
|
2023-04-12 17:55:08 +02:00
|
|
|
end
|
|
|
|
|
2024-04-11 11:56:42 +02:00
|
|
|
it 'renders nested within its fieldset' do
|
2023-04-12 17:55:08 +02:00
|
|
|
expect(page).to have_selector("input[type=text]", count: 2)
|
2024-04-11 11:56:42 +02:00
|
|
|
expect(page).to have_selector("fieldset > .fr-fieldset__element input[type=text]", count: 1)
|
2023-04-12 17:55:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'list of header_section without champs' do
|
2024-03-14 15:13:13 +01:00
|
|
|
let(:types_de_champ_public) { [{ type: :header_section, level: 1 }, { type: :header_section, level: 2 }, { type: :header_section, level: 3 }] }
|
2023-04-12 17:55:08 +02:00
|
|
|
|
2023-09-27 18:43:45 +02:00
|
|
|
it 'render header within fieldset' do
|
|
|
|
expect(page).to have_selector("fieldset > legend", count: 3)
|
2023-04-12 17:55:08 +02:00
|
|
|
expect(page).to have_selector("h2")
|
|
|
|
expect(page).to have_selector("h3")
|
|
|
|
expect(page).to have_selector("h4")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'header_section followed by explication and another fieldset' do
|
2024-03-14 15:13:13 +01:00
|
|
|
let(:types_de_champ_public) { [{ type: :header_section, level: 1 }, { type: :explication }, { type: :header_section, level: 1 }, { type: :text }] }
|
2023-04-12 17:55:08 +02:00
|
|
|
|
2023-09-27 18:43:45 +02:00
|
|
|
it 'render fieldset, header_section, also render explication' do
|
2023-04-12 17:55:08 +02:00
|
|
|
expect(page).to have_selector("h2", count: 2)
|
|
|
|
expect(page).to have_selector("h3") # explication
|
2023-09-27 18:43:45 +02:00
|
|
|
expect(page).to have_selector("fieldset > legend > h2", count: 2)
|
2023-04-12 17:55:08 +02:00
|
|
|
expect(page).to have_selector("fieldset input[type=text]", count: 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'nested fieldsset' do
|
2024-03-14 15:13:13 +01:00
|
|
|
let(:types_de_champ_public) { [{ type: :header_section, level: 1 }, { type: :text }, { type: :header_section, level: 2 }, { type: :textarea }] }
|
2023-04-12 17:55:08 +02:00
|
|
|
|
|
|
|
it 'render nested fieldsets' do
|
|
|
|
expect(page).to have_selector("fieldset")
|
|
|
|
expect(page).to have_selector("legend h2")
|
|
|
|
expect(page).to have_selector("fieldset fieldset")
|
|
|
|
expect(page).to have_selector("fieldset fieldset legend h3")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains all champs' do
|
|
|
|
expect(page).to have_selector("fieldset input[type=text]", count: 1)
|
|
|
|
expect(page).to have_selector("fieldset fieldset textarea", count: 1)
|
|
|
|
end
|
|
|
|
end
|
2023-04-14 10:55:36 +02:00
|
|
|
|
|
|
|
context 'with repetition' do
|
2024-03-14 15:13:13 +01:00
|
|
|
let(:types_de_champ_public) do
|
|
|
|
[
|
|
|
|
{ type: :header_section, level: 1 },
|
2023-04-14 10:55:36 +02:00
|
|
|
{
|
|
|
|
type: :repetition,
|
|
|
|
libelle: 'repetition',
|
|
|
|
children: [
|
2024-03-14 15:13:13 +01:00
|
|
|
{ type: :header_section, level: 1, libelle: 'child_1' },
|
2023-04-14 10:55:36 +02:00
|
|
|
{ type: :text, libelle: 'child_2' }
|
|
|
|
]
|
|
|
|
}
|
2024-03-14 15:13:13 +01:00
|
|
|
]
|
2023-04-14 10:55:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'render nested fieldsets, increase heading level for repetition header_section' do
|
|
|
|
expect(page).to have_selector("fieldset")
|
|
|
|
expect(page).to have_selector("legend h2")
|
|
|
|
expect(page).to have_selector("fieldset fieldset")
|
|
|
|
expect(page).to have_selector("fieldset fieldset legend h3")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains as many text champ as repetition.rows' do
|
2024-09-27 15:37:11 +02:00
|
|
|
expect(page).to have_selector("fieldset fieldset input[type=text]", count: dossier.project_champs_public.find(&:repetition?).rows.size)
|
2023-04-14 10:55:36 +02:00
|
|
|
end
|
|
|
|
end
|
2023-09-27 18:43:45 +02:00
|
|
|
|
|
|
|
context 'with complex markup structure' do
|
|
|
|
def check_fieldset_structure(fieldset)
|
|
|
|
expect(fieldset[:class]).to include('fr-fieldset')
|
|
|
|
|
|
|
|
# Vérifie que chaque fr-fieldset a un enfant fr-fieldset__element ou une légende
|
|
|
|
fieldset.all('> *').each do |child|
|
|
|
|
expect(child.tag_name).to be_in(['div', 'legend', 'input'])
|
|
|
|
|
|
|
|
case child.tag_name
|
|
|
|
when 'legend'
|
|
|
|
expect(child[:class]).to include('fr-fieldset__legend')
|
|
|
|
when 'input'
|
|
|
|
expect(child[:type]).to eq("hidden")
|
|
|
|
else
|
|
|
|
expect(child[:class]).to include('fr-fieldset__element')
|
|
|
|
# Vérifie récursivement les fieldsets imbriqués
|
|
|
|
child.all('> fieldset').each do |nested_fieldset|
|
|
|
|
check_fieldset_structure(nested_fieldset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-14 15:13:13 +01:00
|
|
|
let(:types_de_champ_public) {
|
2023-09-27 18:43:45 +02:00
|
|
|
[
|
2024-03-14 15:13:13 +01:00
|
|
|
{ type: :header_section, level: 1 },
|
|
|
|
{ type: :header_section, level: 2 },
|
|
|
|
{ type: :header_section, level: 3 },
|
|
|
|
{ type: :integer_number },
|
|
|
|
|
|
|
|
{ type: :header_section, level: 3 },
|
|
|
|
{ type: :yes_no },
|
|
|
|
|
|
|
|
{ type: :header_section, level: 2 },
|
|
|
|
{ type: :header_section, level: 3 },
|
|
|
|
{ type: :integer_number },
|
|
|
|
|
|
|
|
{ type: :header_section, level: 1 },
|
|
|
|
{ type: :text },
|
|
|
|
{ type: :header_section, level: 2 },
|
|
|
|
{ type: :text }
|
2023-09-27 18:43:45 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
it 'respect dsfr fieldset hierarchy' do
|
|
|
|
within('.dossier-edit .form') do
|
|
|
|
all('fieldset').each do |fieldset|
|
|
|
|
check_fieldset_structure(fieldset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-04-12 17:55:08 +02:00
|
|
|
end
|