|
114 | 114 | end |
115 | 115 | end |
116 | 116 |
|
| 117 | + describe "enum auto-detection" do |
| 118 | + let(:enum_class) do |
| 119 | + Class.new do |
| 120 | + def self.defined_enums |
| 121 | + { "status" => { "draft" => 0, "published" => 1, "archived" => 2 } } |
| 122 | + end |
| 123 | + |
| 124 | + def self.try(method) |
| 125 | + send(method) if respond_to?(method) |
| 126 | + end |
| 127 | + end |
| 128 | + end |
| 129 | + let(:object) { enum_class.new.tap { |o| o.define_singleton_method(:status) { "published" } } } |
| 130 | + let(:field) { Superform::Rails::Field.new(:status, parent: nil, object: object) } |
| 131 | + |
| 132 | + it "auto-detects enum options when no args given" do |
| 133 | + choices = field.radios |
| 134 | + values = choices.map { |c| [c.value, c.text] } |
| 135 | + expect(values).to eq([["draft", "Draft"], ["published", "Published"], ["archived", "Archived"]]) |
| 136 | + end |
| 137 | + |
| 138 | + it "checks the radio matching the current value" do |
| 139 | + html = "" |
| 140 | + field.radios.each do |choice| |
| 141 | + html += render(choice.radio) |
| 142 | + end |
| 143 | + |
| 144 | + expect(html).to match(/<input[^>]*value="published"[^>]*checked/) |
| 145 | + expect(html).not_to match(/<input[^>]*value="draft"[^>]*checked/) |
| 146 | + end |
| 147 | + |
| 148 | + it "uses explicit options when provided (skips enum)" do |
| 149 | + choices = field.radios("active", "inactive") |
| 150 | + values = choices.map { |c| [c.value, c.text] } |
| 151 | + expect(values).to eq([["active", "active"], ["inactive", "inactive"]]) |
| 152 | + end |
| 153 | + |
| 154 | + it "returns empty choices when field is not an enum" do |
| 155 | + non_enum_field = Superform::Rails::Field.new(:name, parent: nil, object: object) |
| 156 | + object.define_singleton_method(:name) { "test" } |
| 157 | + choices = non_enum_field.radios |
| 158 | + expect(choices.count).to eq(0) |
| 159 | + end |
| 160 | + |
| 161 | + it "returns empty choices when object is nil" do |
| 162 | + nil_field = Superform::Rails::Field.new(:status, parent: nil, object: nil) |
| 163 | + choices = nil_field.radios |
| 164 | + expect(choices.count).to eq(0) |
| 165 | + end |
| 166 | + |
| 167 | + it "works with checkboxes too" do |
| 168 | + choices = field.checkboxes |
| 169 | + values = choices.map { |c| [c.value, c.text] } |
| 170 | + expect(values).to eq([["draft", "Draft"], ["published", "Published"], ["archived", "Archived"]]) |
| 171 | + end |
| 172 | + end |
| 173 | + |
| 174 | + describe "default label text" do |
| 175 | + let(:object) { double("object", plan_id: 1) } |
| 176 | + let(:field) do |
| 177 | + Superform::Rails::Field.new(:plan_id, parent: nil, object: object) |
| 178 | + end |
| 179 | + |
| 180 | + it "renders choice.text when label is called without a block" do |
| 181 | + html = "" |
| 182 | + field.radios([1, "Basic"], [2, "Pro"]).each do |choice| |
| 183 | + html += render(choice.label) |
| 184 | + end |
| 185 | + |
| 186 | + expect(html).to include("Basic") |
| 187 | + expect(html).to include("Pro") |
| 188 | + expect(html).to include('for="plan_id_0"') |
| 189 | + expect(html).to include('for="plan_id_1"') |
| 190 | + end |
| 191 | + end |
| 192 | + |
117 | 193 | describe "is Enumerable" do |
118 | 194 | let(:object) { double("object", status: "active") } |
119 | 195 | let(:field) do |
|
0 commit comments