Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions app/models/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def next_step_slug_after_routing
return goto_condition_step_slug(routing_conditions.first)
end

if first_condition_matches?
return goto_condition_step_slug(routing_conditions.first)
if (matching_condition = find_matching_condition)
return goto_condition_step_slug(matching_condition)
end

next_step_slug
Expand Down Expand Up @@ -141,19 +141,25 @@ def goto_condition_step_slug(condition)
end
end

def first_condition_matches?
def find_matching_condition
return unless question.respond_to?(:selection)

return question.selection == I18n.t("page.none_of_the_above") if routing_condition_none_of_the_above
routing_conditions.find { condition_matches? it }
end

def condition_matches?(condition)
return question.selection == I18n.t("page.none_of_the_above") if condition.answer_value == :none_of_the_above.to_s

routing_conditions.any? && (routing_conditions.first.answer_value == question.selection)
condition.answer_value == question.selection
end

def first_condition_default?
routing_conditions.any? && routing_conditions.first.answer_value.blank?
def first_condition_matches?
return unless question.respond_to?(:selection)

routing_conditions.any? && condition_matches?(routing_conditions.first)
end

def routing_condition_none_of_the_above
routing_conditions.any? && routing_conditions.first.answer_value == :none_of_the_above.to_s
def first_condition_default?
routing_conditions.any? && routing_conditions.first.answer_value.blank?
end
end
60 changes: 36 additions & 24 deletions spec/models/step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@
end

describe "#next_step_slug_after_routing" do
let(:default_next_step_id) { second_step_id }
let(:default_next_step_id) { "11111111" } # dummy value to make the default next step ID obvious when it appears
let(:selection) { "Yes" }
let(:question) { instance_double(Question::Selection, selection:) }
let(:routing_conditions) { [] }
let(:form_document_step) { build(:v2_question_step, id: first_step_id, position: 1, next_step_id: default_next_step_id, routing_conditions:) }
let(:form_document_step) { build(:v2_question_step, id: "00000000", position: 1, next_step_id: default_next_step_id, routing_conditions:) }

describe "basic routing" do
context "without any routing conditions" do
Expand Down Expand Up @@ -224,46 +224,58 @@
end
end

describe "with invalid states" do
context "with multiple conditions and second matches" do
let(:routing_conditions) do
[
OpenStruct.new(answer_value: "No", goto_page_id: first_step_id),
OpenStruct.new(answer_value: "Yes", goto_page_id: second_step_id),
OpenStruct.new(answer_value: "Maybe", goto_page_id: third_step_id),
]
context "with multiple conditions" do
let(:routing_conditions) do
[
OpenStruct.new(answer_value: "No", goto_page_id: first_step_id),
OpenStruct.new(answer_value: "Yes", goto_page_id: second_step_id),
OpenStruct.new(answer_value: "Maybe", goto_page_id: third_step_id),
]
end

context "and first matches" do
let(:selection) { "No" }

it "returns the next_step_slug" do
expect(step.next_step_slug_after_routing).to eq(first_step_id)
end
end

context "and second matches" do
let(:selection) { "Yes" }

it "returns the next_step_slug" do
expect(step.next_step_slug_after_routing).to eq(default_next_step_id)
expect(step.next_step_slug_after_routing).to eq(second_step_id)
end
end

context "with multiple conditions and second is default" do
let(:routing_conditions) do
[
OpenStruct.new(answer_value: "No", goto_page_id: first_step_id),
OpenStruct.new(answer_value: "Yes", goto_page_id: second_step_id),
OpenStruct.new(answer_value: "", goto_page_id: third_step_id),
]
context "and third matches" do
let(:selection) { "Maybe" }

it "returns the next_step_slug" do
expect(step.next_step_slug_after_routing).to eq(third_step_id)
end
let(:selection) { "Something else" }
end

context "but none match" do
let(:selection) { "Something Else" }

it "returns the next_step_slug" do
expect(step.next_step_slug_after_routing).to eq(default_next_step_id)
end
end
end

context "with multiple conditions but no matches" do
describe "with invalid states" do
context "with multiple conditions and default matches" do
let(:routing_conditions) do
[
OpenStruct.new(answer_value: "Yes", goto_page_id: first_step_id),
OpenStruct.new(answer_value: "No", goto_page_id: second_step_id),
OpenStruct.new(answer_value: "Maybe", goto_page_id: third_step_id),
OpenStruct.new(answer_value: "No", goto_page_id: first_step_id),
OpenStruct.new(answer_value: "Yes", goto_page_id: second_step_id),
OpenStruct.new(answer_value: nil, goto_page_id: third_step_id),
]
end
let(:selection) { "Something Else" }
let(:selection) { "Something else" }

it "returns the next_step_slug" do
expect(step.next_step_slug_after_routing).to eq(default_next_step_id)
Expand Down
Loading