|
| 1 | +RSpec.describe Superform::Rails::Components::Radio, type: :view do |
| 2 | + describe 'single radio' do |
| 3 | + let(:object) { double('object', gender: "male") } |
| 4 | + let(:field) do |
| 5 | + Superform::Rails::Field.new(:gender, parent: nil, object: object) |
| 6 | + end |
| 7 | + |
| 8 | + it 'renders a checked radio when value matches' do |
| 9 | + html = render(field.radio("male")) |
| 10 | + expect(html).to include('type="radio"') |
| 11 | + expect(html).to include('name="gender"') |
| 12 | + expect(html).to include('value="male"') |
| 13 | + expect(html).to include('checked') |
| 14 | + end |
| 15 | + |
| 16 | + it 'renders an unchecked radio when value does not match' do |
| 17 | + html = render(field.radio("female")) |
| 18 | + expect(html).to include('value="female"') |
| 19 | + expect(html).not_to include('checked') |
| 20 | + end |
| 21 | + |
| 22 | + it 'accepts custom attributes' do |
| 23 | + html = render(field.radio("male", class: "radio-input")) |
| 24 | + expect(html).to include('class="radio-input"') |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + describe 'radio collection' do |
| 29 | + let(:object) { double('object', status: "active") } |
| 30 | + let(:field) do |
| 31 | + Superform::Rails::Field.new(:status, parent: nil, object: object) |
| 32 | + end |
| 33 | + |
| 34 | + it 'returns an enumerable' do |
| 35 | + collection = field.radio("active", "inactive", "pending") |
| 36 | + expect(collection).to respond_to(:each) |
| 37 | + end |
| 38 | + |
| 39 | + it 'yields options with value, label, and radio' do |
| 40 | + options = field.radio(["active", "Active"], ["inactive", "Inactive"]).to_a |
| 41 | + expect(options.length).to eq(2) |
| 42 | + expect(options.first.value).to eq("active") |
| 43 | + expect(options.first.label).to eq("Active") |
| 44 | + expect(options.first).to respond_to(:radio) |
| 45 | + end |
| 46 | + |
| 47 | + it 'renders radios with correct checked state' do |
| 48 | + html = "" |
| 49 | + field.radio(["active", "Active"], ["inactive", "Inactive"], ["pending", "Pending"]).each do |status| |
| 50 | + html += render(status.radio) |
| 51 | + end |
| 52 | + |
| 53 | + expect(html.scan(/type="radio"/).count).to eq(3) |
| 54 | + expect(html.scan(/name="status"/).count).to eq(3) |
| 55 | + expect(html.scan(/checked/).count).to eq(1) |
| 56 | + expect(html).to match(/<input[^>]*value="active"[^>]*checked/) |
| 57 | + expect(html).not_to match(/<input[^>]*value="inactive"[^>]*checked/) |
| 58 | + expect(html).not_to match(/<input[^>]*value="pending"[^>]*checked/) |
| 59 | + end |
| 60 | + |
| 61 | + it 'generates unique IDs for each radio' do |
| 62 | + html = "" |
| 63 | + field.radio("active", "inactive").each do |status| |
| 64 | + html += render(status.radio) |
| 65 | + end |
| 66 | + |
| 67 | + expect(html).to include('id="status_1"') |
| 68 | + expect(html).to include('id="status_2"') |
| 69 | + end |
| 70 | + |
| 71 | + it 'works with the label pattern from the docs' do |
| 72 | + html = "" |
| 73 | + field.radio(["active", "Active"], ["inactive", "Inactive"]).each do |status| |
| 74 | + # Simulating: label { status.radio; whitespace; plain status.value.humanize } |
| 75 | + html += render(status.radio) |
| 76 | + end |
| 77 | + |
| 78 | + expect(html).to include('value="active"') |
| 79 | + expect(html).to include('value="inactive"') |
| 80 | + expect(html.scan(/checked/).count).to eq(1) |
| 81 | + end |
| 82 | + |
| 83 | + it 'works with single-value options (value used as label)' do |
| 84 | + options = field.radio("active", "inactive").to_a |
| 85 | + expect(options.first.value).to eq("active") |
| 86 | + expect(options.first.label).to eq("active") |
| 87 | + end |
| 88 | + end |
| 89 | +end |
0 commit comments