|
1 | 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 |
| 2 | + let(:object) { double('object', gender: "male") } |
| 3 | + let(:field) do |
| 4 | + Superform::Rails::Field.new(:gender, parent: nil, object: object) |
26 | 5 | end |
27 | 6 |
|
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 |
| 7 | + it 'renders a checked radio when value matches' do |
| 8 | + html = render(field.radio("male")) |
| 9 | + expect(html).to include('type="radio"') |
| 10 | + expect(html).to include('name="gender"') |
| 11 | + expect(html).to include('value="male"') |
| 12 | + expect(html).to include('checked') |
| 13 | + end |
66 | 14 |
|
67 | | - expect(html).to include('id="status_1"') |
68 | | - expect(html).to include('id="status_2"') |
69 | | - end |
| 15 | + it 'renders an unchecked radio when value does not match' do |
| 16 | + html = render(field.radio("female")) |
| 17 | + expect(html).to include('value="female"') |
| 18 | + expect(html).not_to include('checked') |
| 19 | + end |
70 | 20 |
|
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 |
| 21 | + it 'accepts custom attributes' do |
| 22 | + html = render(field.radio("male", class: "radio-input")) |
| 23 | + expect(html).to include('class="radio-input"') |
| 24 | + end |
77 | 25 |
|
78 | | - expect(html).to include('value="active"') |
79 | | - expect(html).to include('value="inactive"') |
80 | | - expect(html.scan(/checked/).count).to eq(1) |
| 26 | + it 'renders a group with correct checked state' do |
| 27 | + html = "" |
| 28 | + ["male", "female", "other"].each do |value| |
| 29 | + html += render(field.radio(value)) |
81 | 30 | end |
82 | 31 |
|
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 |
| 32 | + expect(html.scan(/type="radio"/).count).to eq(3) |
| 33 | + expect(html.scan(/name="gender"/).count).to eq(3) |
| 34 | + expect(html.scan(/checked/).count).to eq(1) |
| 35 | + expect(html).to match(/<input[^>]*value="male"[^>]*checked/) |
88 | 36 | end |
89 | 37 | end |
0 commit comments