2023-04-11 13:53:59 +02:00
|
|
|
describe TreeableConcern do
|
|
|
|
class ChampsToTree
|
|
|
|
include TreeableConcern
|
2023-04-12 17:55:08 +02:00
|
|
|
|
2023-04-11 13:53:59 +02:00
|
|
|
attr_reader :root
|
2023-11-28 17:33:43 +01:00
|
|
|
def initialize(types_de_champ:)
|
|
|
|
@root = to_tree(types_de_champ:)
|
2023-04-11 13:53:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-28 17:33:43 +01:00
|
|
|
subject { ChampsToTree.new(types_de_champ:).root }
|
2023-04-11 13:53:59 +02:00
|
|
|
describe "to_tree" do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public:) }
|
|
|
|
let(:types_de_champ_public) { [] }
|
|
|
|
let(:types_de_champ) { procedure.active_revision.types_de_champ_public }
|
|
|
|
|
|
|
|
let(:header_1) { { type: :header_section, level: 1, stable_id: 99 } }
|
|
|
|
let(:header_1_2) { { type: :header_section, level: 2, stable_id: 199 } }
|
|
|
|
let(:header_2) { { type: :header_section, level: 1, stable_id: 299 } }
|
|
|
|
let(:champ_text) { { stable_id: 399 } }
|
|
|
|
let(:champ_textarea) { { type: :textarea, stable_id: 499 } }
|
|
|
|
let(:champ_explication) { { type: :explication, stable_id: 599 } }
|
|
|
|
let(:champ_communes) { { type: :communes, stable_id: 699 } }
|
|
|
|
|
|
|
|
let(:header_1_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 99 } }
|
|
|
|
let(:header_1_2_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 199 } }
|
|
|
|
let(:header_2_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 299 } }
|
|
|
|
let(:champ_text_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 399 } }
|
|
|
|
let(:champ_textarea_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 499 } }
|
|
|
|
let(:champ_explication_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 599 } }
|
|
|
|
let(:champ_communes_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 699 } }
|
2023-04-11 13:53:59 +02:00
|
|
|
|
|
|
|
context 'without section' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:types_de_champ_public) do
|
2023-04-11 13:53:59 +02:00
|
|
|
[
|
|
|
|
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)
|
2024-07-01 15:31:32 +02:00
|
|
|
expect(subject).to eq([champ_text_tdc, champ_textarea_tdc])
|
2023-04-11 13:53:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with header_section and champs' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:types_de_champ_public) do
|
2023-04-11 13:53:59 +02:00
|
|
|
[
|
|
|
|
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([
|
2024-07-01 15:31:32 +02:00
|
|
|
[header_1_tdc, champ_explication_tdc, champ_text_tdc],
|
|
|
|
[header_2_tdc, champ_textarea_tdc]
|
2023-04-11 13:53:59 +02:00
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'leading champs, and in between sections only' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:champ_textarea_bis) { { type: :textarea, stable_id: 799 } }
|
|
|
|
let(:champ_textarea_bis_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 799 } }
|
|
|
|
let(:types_de_champ_public) do
|
2023-04-11 13:53:59 +02:00
|
|
|
[
|
|
|
|
champ_text,
|
|
|
|
champ_textarea,
|
|
|
|
header_1,
|
|
|
|
champ_explication,
|
|
|
|
champ_communes,
|
|
|
|
header_2,
|
2024-07-01 15:31:32 +02:00
|
|
|
champ_textarea_bis
|
2023-04-11 13:53:59 +02:00
|
|
|
]
|
|
|
|
end
|
|
|
|
it 'chunk by uniq champs' do
|
|
|
|
expect(subject.size).to eq(4)
|
|
|
|
expect(subject).to eq([
|
2024-07-01 15:31:32 +02:00
|
|
|
champ_text_tdc,
|
|
|
|
champ_textarea_tdc,
|
|
|
|
[header_1_tdc, champ_explication_tdc, champ_communes_tdc],
|
|
|
|
[header_2_tdc, champ_textarea_bis_tdc]
|
2023-04-11 13:53:59 +02:00
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with one sub sections' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:types_de_champ_public) do
|
2023-04-11 13:53:59 +02:00
|
|
|
[
|
|
|
|
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([
|
2024-07-01 15:31:32 +02:00
|
|
|
[header_1_tdc, champ_explication_tdc, [header_1_2_tdc, champ_communes_tdc]],
|
|
|
|
[header_2_tdc, champ_textarea_tdc]
|
2023-04-11 13:53:59 +02:00
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with consecutive subsection' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:header_1_2_1) { { type: :header_section, level: 2, stable_id: 799 } }
|
|
|
|
let(:header_1_2_2) { { type: :header_section, level: 2, stable_id: 899 } }
|
|
|
|
let(:header_1_2_3) { { type: :header_section, level: 2, stable_id: 999 } }
|
|
|
|
|
|
|
|
let(:header_1_2_1_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 799 } }
|
|
|
|
let(:header_1_2_2_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 899 } }
|
|
|
|
let(:header_1_2_3_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 999 } }
|
|
|
|
|
|
|
|
let(:types_de_champ_public) do
|
2023-04-11 13:53:59 +02:00
|
|
|
[
|
|
|
|
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([
|
|
|
|
[
|
2024-07-01 15:31:32 +02:00
|
|
|
header_1_tdc,
|
|
|
|
[header_1_2_1_tdc, champ_text_tdc],
|
|
|
|
[header_1_2_2_tdc, champ_textarea_tdc],
|
|
|
|
[header_1_2_3_tdc, champ_communes_tdc]
|
2023-04-11 13:53:59 +02:00
|
|
|
]
|
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with one sub sections and one subsub section' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:header_1_2_3) { { type: :header_section, level: 3, stable_id: 799 } }
|
|
|
|
let(:header_1_2_3_tdc) { procedure.active_revision.types_de_champ_public.find { _1.stable_id == 799 } }
|
2023-04-11 13:53:59 +02:00
|
|
|
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:types_de_champ_public) do
|
2023-04-11 13:53:59 +02:00
|
|
|
[
|
|
|
|
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([
|
|
|
|
[
|
2024-07-01 15:31:32 +02:00
|
|
|
header_1_tdc,
|
|
|
|
champ_explication_tdc,
|
2023-04-11 13:53:59 +02:00
|
|
|
[
|
2024-07-01 15:31:32 +02:00
|
|
|
header_1_2_tdc,
|
|
|
|
champ_communes_tdc,
|
2023-04-11 13:53:59 +02:00
|
|
|
[
|
2024-07-01 15:31:32 +02:00
|
|
|
header_1_2_3_tdc, champ_text_tdc
|
2023-04-11 13:53:59 +02:00
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
2024-07-01 15:31:32 +02:00
|
|
|
header_2_tdc,
|
|
|
|
champ_textarea_tdc
|
2023-04-11 13:53:59 +02:00
|
|
|
]
|
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|