|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative 'test_helper' |
| 4 | + |
| 5 | +class EventHumanNameTest < BaseTestCase |
| 6 | + def setup |
| 7 | + @model = new_model do |
| 8 | + include ActiveModel::Validations |
| 9 | + attr_accessor :status |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + def test_should_allow_custom_human_name_on_event |
| 14 | + machine = StateMachines::Machine.new(@model, :status, initial: :parked) do |
| 15 | + event :start, human_name: 'Start Engine' do |
| 16 | + transition parked: :running |
| 17 | + end |
| 18 | + |
| 19 | + event :stop do |
| 20 | + transition running: :parked |
| 21 | + end |
| 22 | + |
| 23 | + event :pause, human_name: 'Temporarily Pause' do |
| 24 | + transition running: :paused |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + assert_equal 'Start Engine', machine.events[:start].human_name(@model) |
| 29 | + assert_equal 'Temporarily Pause', machine.events[:pause].human_name(@model) |
| 30 | + end |
| 31 | + |
| 32 | + def test_should_not_override_custom_event_human_name_with_translation |
| 33 | + # Set up I18n translations |
| 34 | + I18n.backend.store_translations(:en, { |
| 35 | + activemodel: { |
| 36 | + state_machines: { |
| 37 | + events: { |
| 38 | + ignite: 'Translation for Ignite', |
| 39 | + park: 'Translation for Park', |
| 40 | + repair: 'Translation for Repair' |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + machine = StateMachines::Machine.new(@model, :status, initial: :parked) do |
| 47 | + event :ignite, human_name: 'Custom Ignition' do |
| 48 | + transition parked: :idling |
| 49 | + end |
| 50 | + |
| 51 | + event :park do |
| 52 | + transition idling: :parked |
| 53 | + end |
| 54 | + |
| 55 | + event :repair, human_name: 'Custom Repair Process' do |
| 56 | + transition any => :parked |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + # Custom human names should be preserved |
| 61 | + assert_equal 'Custom Ignition', machine.events[:ignite].human_name(@model) |
| 62 | + assert_equal 'Custom Repair Process', machine.events[:repair].human_name(@model) |
| 63 | + |
| 64 | + # Event without custom human_name should use translation |
| 65 | + assert_equal 'Translation for Park', machine.events[:park].human_name(@model) |
| 66 | + end |
| 67 | + |
| 68 | + def test_should_allow_custom_event_human_name_as_string |
| 69 | + machine = StateMachines::Machine.new(@model, :status) do |
| 70 | + event :activate, human_name: 'Turn On' |
| 71 | + end |
| 72 | + |
| 73 | + assert_equal 'Turn On', machine.events[:activate].human_name(@model) |
| 74 | + end |
| 75 | + |
| 76 | + def test_should_allow_custom_event_human_name_as_lambda |
| 77 | + machine = StateMachines::Machine.new(@model, :status) do |
| 78 | + event :process, human_name: ->(event, klass) { "#{klass.name}: #{event.name.to_s.capitalize} Action" } |
| 79 | + end |
| 80 | + |
| 81 | + assert_equal 'Foo: Process Action', machine.events[:process].human_name(@model) |
| 82 | + end |
| 83 | + |
| 84 | + def test_should_use_default_translation_when_no_custom_event_human_name |
| 85 | + machine = StateMachines::Machine.new(@model, :status) do |
| 86 | + event :idle |
| 87 | + end |
| 88 | + |
| 89 | + # Should fall back to humanized version when no translation exists |
| 90 | + assert_equal 'idle', machine.events[:idle].human_name(@model) |
| 91 | + end |
| 92 | + |
| 93 | + def test_should_handle_nil_event_human_name |
| 94 | + machine = StateMachines::Machine.new(@model, :status) do |
| 95 | + event :wait |
| 96 | + end |
| 97 | + |
| 98 | + # Explicitly set to nil |
| 99 | + machine.events[:wait].human_name = nil |
| 100 | + |
| 101 | + # When human_name is nil, Event#human_name returns nil |
| 102 | + assert_nil machine.events[:wait].human_name(@model) |
| 103 | + end |
| 104 | + |
| 105 | + def test_should_preserve_event_human_name_through_multiple_definitions |
| 106 | + machine = StateMachines::Machine.new(@model, :status, initial: :draft) |
| 107 | + |
| 108 | + # First define event with custom human name |
| 109 | + machine.event :publish, human_name: 'Make Public' do |
| 110 | + transition draft: :published |
| 111 | + end |
| 112 | + |
| 113 | + # Redefine the same event (this should not override the human_name) |
| 114 | + machine.event :publish do |
| 115 | + transition pending: :published |
| 116 | + end |
| 117 | + |
| 118 | + assert_equal 'Make Public', machine.events[:publish].human_name(@model) |
| 119 | + end |
| 120 | + |
| 121 | + def test_should_work_with_state_machine_helper_method |
| 122 | + @model.class_eval do |
| 123 | + state_machine :status, initial: :pending do |
| 124 | + event :approve, human_name: 'Grant Approval' do |
| 125 | + transition pending: :approved |
| 126 | + end |
| 127 | + |
| 128 | + event :reject do |
| 129 | + transition pending: :rejected |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + machine = @model.state_machine(:status) |
| 135 | + assert_equal 'Grant Approval', machine.events[:approve].human_name(@model) |
| 136 | + end |
| 137 | + |
| 138 | + def test_should_handle_complex_i18n_lookup_with_custom_event_human_name |
| 139 | + # Set up complex I18n structure |
| 140 | + I18n.backend.store_translations(:en, { |
| 141 | + activemodel: { |
| 142 | + state_machines: { |
| 143 | + foo: { |
| 144 | + status: { |
| 145 | + events: { |
| 146 | + submit: 'Model Specific Submit' |
| 147 | + } |
| 148 | + } |
| 149 | + }, |
| 150 | + status: { |
| 151 | + events: { |
| 152 | + submit: 'Machine Specific Submit' |
| 153 | + } |
| 154 | + }, |
| 155 | + events: { |
| 156 | + submit: 'Generic Submit' |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + }) |
| 161 | + |
| 162 | + machine = StateMachines::Machine.new(@model, :status) do |
| 163 | + event :submit, human_name: 'Send for Review' do |
| 164 | + transition draft: :pending |
| 165 | + end |
| 166 | + end |
| 167 | + |
| 168 | + # Should use the custom human_name, not any of the I18n translations |
| 169 | + assert_equal 'Send for Review', machine.events[:submit].human_name(@model) |
| 170 | + end |
| 171 | + |
| 172 | + def teardown |
| 173 | + # Clear I18n translations after each test |
| 174 | + I18n.backend.reload! |
| 175 | + end |
| 176 | +end |
0 commit comments