Skip to content

Commit 96e116e

Browse files
authored
Merge pull request #2788 from govuk-forms/order-selection-options
Order selection options
2 parents 8dbda43 + 17cdb7f commit 96e116e

2 files changed

Lines changed: 66 additions & 13 deletions

File tree

app/components/page_list_component/view.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ def skip_condition_route_page_text(condition)
101101
end
102102

103103
def answer_value_groups(page)
104-
page.routing_conditions.group_by(&:goto_page_id).sort_by { |goto_page_id, _| goto_page_id || Float::INFINITY }
104+
answer_order = page.answer_settings&.selection_options&.map(&:value) || []
105+
106+
page.routing_conditions.to_a
107+
.in_order_of(:answer_value, answer_order, filter: false)
108+
.group_by(&:goto_page_id)
109+
.sort_by { |goto_page_id, _| goto_page_id || Float::INFINITY }
105110
end
106111
end
107112
end

spec/components/page_list_component/view_spec.rb

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -520,21 +520,69 @@
520520
end
521521

522522
describe "#answer_value_groups" do
523-
let(:form) { create :form, :ready_for_routing }
524-
let(:pages) { form.reload.pages }
525-
let!(:first_condition) { create :condition, routing_page_id: pages.first.id, answer_value: "Option 1", goto_page_id: pages.third.id }
526-
let!(:second_condition) { create :condition, routing_page_id: pages.first.id, answer_value: "Option 2", goto_page_id: pages.third.id }
527-
let!(:third_condition) { create :condition, routing_page_id: pages.first.id, answer_value: "Option 3", goto_page_id: pages.fourth.id }
528-
let!(:fourth_condition) { create :condition, routing_page_id: pages.first.id, answer_value: "Option 3", goto_page_id: nil, skip_to_end: true }
523+
let(:form) { build_stubbed :form, :ready_for_routing }
524+
525+
let(:pages) do
526+
[
527+
build_stubbed(
528+
:page,
529+
:with_selection_settings,
530+
id: 101,
531+
selection_options:,
532+
routing_conditions: conditions,
533+
),
534+
build_stubbed(:page, id: 102),
535+
build_stubbed(:page, id: 103),
536+
build_stubbed(:page, id: 104),
537+
]
538+
end
539+
540+
let(:selection_options) do
541+
[
542+
{ value: "Option 1" },
543+
{ value: "Option 2" },
544+
{ value: "Option 3" },
545+
]
546+
end
547+
548+
let(:conditions) do
549+
[
550+
build_stubbed(:condition, routing_page_id: 101, answer_value: "Option 1", goto_page_id: 103),
551+
build_stubbed(:condition, routing_page_id: 101, answer_value: "Option 2", goto_page_id: 103),
552+
build_stubbed(:condition, routing_page_id: 101, answer_value: "Option 3", goto_page_id: 104),
553+
build_stubbed(:condition, routing_page_id: 101, answer_value: nil, goto_page_id: nil, skip_to_end: true),
554+
]
555+
end
529556

530557
it "groups conditions by goto_page_id" do
531-
expect(page_list_component.answer_value_groups(pages.first.reload)).to eq(
558+
expected = [
559+
[103, [conditions.first, conditions.second]],
560+
[104, [conditions.third]],
561+
[nil, [conditions.fourth]],
562+
]
563+
result = page_list_component.answer_value_groups(pages.first)
564+
expect(result).to eq expected
565+
end
566+
567+
context "when given a different order of selection options" do
568+
let(:selection_options) do
532569
[
533-
[pages.third.id, [first_condition, second_condition]],
534-
[pages.fourth.id, [third_condition]],
535-
[nil, [fourth_condition]],
536-
],
537-
)
570+
{ value: "Option 3" },
571+
{ value: "Option 2" },
572+
{ value: "Option 1" },
573+
]
574+
end
575+
576+
it "returns the conditions in the same order" do
577+
expected = [
578+
[103, [conditions.second, conditions.first]],
579+
[104, [conditions.third]],
580+
[nil, [conditions.fourth]],
581+
]
582+
583+
result = page_list_component.answer_value_groups(pages.first)
584+
expect(result).to eq expected
585+
end
538586
end
539587
end
540588
end

0 commit comments

Comments
 (0)