|
| 1 | +RSpec.describe Superform::Rails::Components::Datalist, type: :view do |
| 2 | + let(:object) { double("object", time_zone: "Pacific Time (US & Canada)") } |
| 3 | + let(:field) do |
| 4 | + Superform::Rails::Field.new(:time_zone, parent: nil, object: object) |
| 5 | + end |
| 6 | + |
| 7 | + it "renders an input linked to a datalist" do |
| 8 | + html = render(field.datalist("Eastern", "Central", "Pacific")) |
| 9 | + |
| 10 | + expect(html).to include('<input') |
| 11 | + expect(html).to include('list="time_zone_datalist"') |
| 12 | + expect(html).to include('<datalist id="time_zone_datalist">') |
| 13 | + expect(html).to include('<option value="Eastern">') |
| 14 | + expect(html).to include('<option value="Central">') |
| 15 | + expect(html).to include('<option value="Pacific">') |
| 16 | + end |
| 17 | + |
| 18 | + it "sets standard field attributes on the input" do |
| 19 | + html = render(field.datalist("Eastern")) |
| 20 | + |
| 21 | + expect(html).to include('id="time_zone"') |
| 22 | + expect(html).to include('name="time_zone"') |
| 23 | + end |
| 24 | + |
| 25 | + it "passes through HTML attributes to the input" do |
| 26 | + html = render(field.datalist("Eastern", class: "form-input", placeholder: "Start typing...")) |
| 27 | + |
| 28 | + expect(html).to include('class="form-input"') |
| 29 | + expect(html).to include('placeholder="Start typing..."') |
| 30 | + end |
| 31 | + |
| 32 | + it "accepts value/label pairs" do |
| 33 | + html = render(field.datalist(["est", "Eastern"], ["cst", "Central"])) |
| 34 | + |
| 35 | + expect(html).to include('value="est"') |
| 36 | + expect(html).to include("Eastern") |
| 37 | + expect(html).to include('value="cst"') |
| 38 | + expect(html).to include("Central") |
| 39 | + end |
| 40 | + |
| 41 | + it "accepts a hash of options" do |
| 42 | + html = render(field.datalist({ "est" => "Eastern", "cst" => "Central" })) |
| 43 | + |
| 44 | + expect(html).to include('value="est"') |
| 45 | + expect(html).to include("Eastern") |
| 46 | + end |
| 47 | + |
| 48 | + it "renders with a block for custom options" do |
| 49 | + component = field.datalist do |d| |
| 50 | + d.options("Eastern", "Central", "Pacific") |
| 51 | + end |
| 52 | + html = render(component) |
| 53 | + |
| 54 | + expect(html).to include('<input') |
| 55 | + expect(html).to include('list="time_zone_datalist"') |
| 56 | + expect(html).to include('<option value="Eastern">') |
| 57 | + expect(html).to include('<option value="Central">') |
| 58 | + expect(html).to include('<option value="Pacific">') |
| 59 | + end |
| 60 | + |
| 61 | + it "works as a one-liner for time zones" do |
| 62 | + zones = ["Eastern Time (US & Canada)", "Central Time (US & Canada)", "Pacific Time (US & Canada)"] |
| 63 | + html = render(field.datalist(*zones)) |
| 64 | + |
| 65 | + expect(html).to include('value="Eastern Time (US & Canada)"') |
| 66 | + expect(html).to include('value="Pacific Time (US & Canada)"') |
| 67 | + end |
| 68 | +end |
0 commit comments