demarches-normaliennes/spec/models/concerns/treeable_concern_spec.rb

164 lines
4.2 KiB
Ruby
Raw Normal View History

describe TreeableConcern do
class ChampsToTree
include TreeableConcern
attr_reader :root
2023-11-28 17:33:43 +01:00
def initialize(types_de_champ:)
@root = to_tree(types_de_champ:)
end
end
2023-11-28 17:33:43 +01:00
subject { ChampsToTree.new(types_de_champ:).root }
describe "to_tree" do
2023-11-28 17:33:43 +01:00
let(:header_1) { build(:champ_header_section_level_1).type_de_champ }
let(:header_1_2) { build(:champ_header_section_level_2).type_de_champ }
let(:header_2) { build(:champ_header_section_level_1).type_de_champ }
let(:champ_text) { build(:champ_text).type_de_champ }
let(:champ_textarea) { build(:champ_textarea).type_de_champ }
let(:champ_explication) { build(:champ_explication).type_de_champ }
let(:champ_communes) { build(:champ_communes).type_de_champ }
context 'without section' do
2023-11-28 17:33:43 +01:00
let(:types_de_champ) do
[
champ_text, champ_textarea
]
end
it 'inlines champs at root level' do
2023-11-28 17:33:43 +01:00
expect(subject.size).to eq(types_de_champ.size)
expect(subject).to eq(types_de_champ)
end
end
context 'with header_section and champs' do
2023-11-28 17:33:43 +01:00
let(:types_de_champ) do
[
header_1,
champ_explication,
champ_text,
header_2,
champ_textarea
]
end
it 'wraps champs within preview header section' do
expect(subject.size).to eq(2)
expect(subject).to eq([
[header_1, champ_explication, champ_text],
[header_2, champ_textarea]
])
end
end
context 'leading champs, and in between sections only' do
2023-11-28 17:33:43 +01:00
let(:types_de_champ) do
[
champ_text,
champ_textarea,
header_1,
champ_explication,
champ_communes,
header_2,
champ_textarea
]
end
it 'chunk by uniq champs' do
expect(subject.size).to eq(4)
expect(subject).to eq([
champ_text,
champ_textarea,
[header_1, champ_explication, champ_communes],
[header_2, champ_textarea]
])
end
end
context 'with one sub sections' do
2023-11-28 17:33:43 +01:00
let(:types_de_champ) do
[
header_1,
champ_explication,
header_1_2,
champ_communes,
header_2,
champ_textarea
]
end
it 'chunk by uniq champs' do
expect(subject.size).to eq(2)
expect(subject).to eq([
[header_1, champ_explication, [header_1_2, champ_communes]],
[header_2, champ_textarea]
])
end
end
context 'with consecutive subsection' do
2023-11-28 17:33:43 +01:00
let(:header_1) { build(:champ_header_section_level_1).type_de_champ }
let(:header_1_2_1) { build(:champ_header_section_level_2).type_de_champ }
let(:header_1_2_2) { build(:champ_header_section_level_2).type_de_champ }
let(:header_1_2_3) { build(:champ_header_section_level_2).type_de_champ }
let(:types_de_champ) do
[
header_1,
header_1_2_1,
champ_text,
header_1_2_2,
champ_textarea,
header_1_2_3,
champ_communes
]
end
it 'chunk by uniq champs' do
expect(subject.size).to eq(1)
expect(subject).to eq([
[
header_1,
[header_1_2_1, champ_text],
[header_1_2_2, champ_textarea],
[header_1_2_3, champ_communes]
]
])
end
end
context 'with one sub sections and one subsub section' do
2023-11-28 17:33:43 +01:00
let(:header_1_2_3) { build(:champ_header_section_level_3).type_de_champ }
2023-11-28 17:33:43 +01:00
let(:types_de_champ) do
[
header_1,
champ_explication,
header_1_2,
champ_communes,
header_1_2_3,
champ_text,
header_2,
champ_textarea
]
end
it 'chunk by uniq champs' do
expect(subject.size).to eq(2)
expect(subject).to eq([
[
header_1,
champ_explication,
[
header_1_2,
champ_communes,
[
header_1_2_3, champ_text
]
]
],
[
header_2,
champ_textarea
]
])
end
end
end
end