demarches-normaliennes/spec/models/concern/treeable_concern_spec.rb
simon lehericey e64ac79f05 tech(refactor): much nicer code, thx LeSim
root -> rooted_tree

depth_cach -> walk

smal refactor

comment

remove form for header_section

remove seen_at from header section

header_section: champ -> header_section

champ_subree: remove if not

remove root_depth use header_section_level_value instead

remove unused include

remove ChampTreeComponent

rename ChampsSubtreeComponent to SectionComponent

use TreeableConcern only in section component

remove fields_for_champ_component

champs -> tail

add split_section_champ helper

refactor(editable_champ::header_section): keep same interface everywhere

fix(repetition): add spec for SectionComponent on repetitions
2023-04-20 08:22:02 +02:00

163 lines
4 KiB
Ruby

describe TreeableConcern do
class ChampsToTree
include TreeableConcern
attr_reader :root
def initialize(champs:)
@root = to_tree(champs:)
end
end
subject { ChampsToTree.new(champs: champs).root }
describe "to_tree" do
let(:header_1) { build(:champ_header_section_level_1) }
let(:header_1_2) { build(:champ_header_section_level_2) }
let(:header_2) { build(:champ_header_section_level_1) }
let(:champ_text) { build(:champ_text) }
let(:champ_textarea) { build(:champ_textarea) }
let(:champ_explication) { build(:champ_explication) }
let(:champ_communes) { build(:champ_communes) }
context 'without section' do
let(:champs) do
[
champ_text, champ_textarea
]
end
it 'inlines champs at root level' do
expect(subject.size).to eq(champs.size)
expect(subject).to eq(champs)
end
end
context 'with header_section and champs' do
let(:champs) 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
let(:champs) 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
let(:champs) 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
let(:header_1) { build(:champ_header_section_level_1) }
let(:header_1_2_1) { build(:champ_header_section_level_2) }
let(:header_1_2_2) { build(:champ_header_section_level_2) }
let(:header_1_2_3) { build(:champ_header_section_level_2) }
let(:champs) 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
let(:header_1_2_3) { build(:champ_header_section_level_3) }
let(:champs) 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