Skip to content

Commit 1137a1f

Browse files
committed
Add a spec for a FormStateMachine.event_path
Spe'cing out the various expected event paths for changing the state of a Form.
1 parent 9f35489 commit 1137a1f

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

spec/state_machines/form_state_machine_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,53 @@ def after_archive; end
402402
end
403403
end
404404
end
405+
406+
describe ".event_path" do
407+
it "returns the event for a state one transition away" do
408+
expect(FakeForm.event_path(from: :live, to: :archived))
409+
.to eq %i[archive_live_form]
410+
end
411+
412+
it "returns the events to fire in order when the target state needs intermediate transitions" do
413+
# no event goes directly from draft to archived, so the form has to be
414+
# made live on the way
415+
expect(FakeForm.event_path(from: :draft, to: :archived))
416+
.to eq %i[make_live archive_live_form]
417+
end
418+
419+
it "returns the events to fire in order when the target state needs two intermediate transitions" do
420+
# reaching archived_with_draft from draft means passing through both the
421+
# live and live_with_draft states
422+
expect(FakeForm.event_path(from: :draft, to: :archived_with_draft))
423+
.to eq %i[make_live create_draft_from_live_form archive_live_form]
424+
end
425+
426+
it "returns the shortest sequence of events when there is more than one route" do
427+
# an archived_with_draft form could reach archived by being made live
428+
# and archived again, but deleting its draft gets there in one transition
429+
expect(FakeForm.event_path(from: :archived_with_draft, to: :archived))
430+
.to eq %i[delete_draft_from_archived_form]
431+
end
432+
433+
it "returns an empty path when the form is already in the target state" do
434+
expect(FakeForm.event_path(from: :live, to: :live)).to eq []
435+
end
436+
437+
it "returns nil when no sequence of events reaches the target state" do
438+
# no event transitions a form back to draft once it has been made live
439+
expect(FakeForm.event_path(from: :live, to: :draft)).to be_nil
440+
end
441+
442+
it "never routes through the delete_form event" do
443+
# delete_form is the only transition into the deleted state, but firing
444+
# it would destroy the form, so deleted is treated as unreachable
445+
expect(FakeForm.event_path(from: :draft, to: :deleted)).to be_nil
446+
end
447+
448+
it "never routes through the language-specific live events" do
449+
# make_english_version_live also transitions from draft to live, but it
450+
# publishes only one translation, so the path uses make_live
451+
expect(FakeForm.event_path(from: :draft, to: :live)).to eq %i[make_live]
452+
end
453+
end
405454
end

0 commit comments

Comments
 (0)