|
| 1 | +RSpec.describe Superform::Rails::HTML5::Validations, type: :view do |
| 2 | + describe "#validation_attributes" do |
| 3 | + let(:form) { Superform::Rails::Form.new(model, action: "/test") } |
| 4 | + |
| 5 | + context "with combined validators" do |
| 6 | + let(:model) { Product.new } |
| 7 | + |
| 8 | + it "merges all validation attributes" do |
| 9 | + field = form.field(:name) |
| 10 | + attrs = field.validation_attributes |
| 11 | + expect(attrs).to eq({ required: true, minlength: 2, maxlength: 100 }) |
| 12 | + end |
| 13 | + |
| 14 | + it "returns only relevant attributes per field" do |
| 15 | + field = form.field(:quantity) |
| 16 | + attrs = field.validation_attributes |
| 17 | + expect(attrs).to eq({ min: 0, max: 1000, step: 1 }) |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + context "with conditional validators" do |
| 22 | + let(:model) { ConditionalUser.new } |
| 23 | + |
| 24 | + it "returns empty hash when all validators are conditional" do |
| 25 | + field = form.field(:username) |
| 26 | + attrs = field.validation_attributes |
| 27 | + expect(attrs).to eq({}) |
| 28 | + end |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + describe "Field method overrides" do |
| 33 | + let(:model) { User.new } |
| 34 | + let(:form) { Superform::Rails::Form.new(model, action: "/users") } |
| 35 | + |
| 36 | + describe "#input" do |
| 37 | + it "injects required attribute for presence-validated field" do |
| 38 | + html = render(form) { |f| f.render f.field(:first_name).input } |
| 39 | + expect(html).to include('required') |
| 40 | + expect(html).to include('name="user[first_name]"') |
| 41 | + end |
| 42 | + |
| 43 | + it "does not inject required for non-validated field" do |
| 44 | + html = render(form) { |f| f.render f.field(:last_name).input } |
| 45 | + expect(html).not_to include('required') |
| 46 | + end |
| 47 | + |
| 48 | + it "allows user kwargs to override validation attributes" do |
| 49 | + html = render(form) { |f| f.render f.field(:first_name).input(required: false) } |
| 50 | + expect(html).not_to include('required') |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + describe "#textarea" do |
| 55 | + it "injects required attribute for presence-validated field" do |
| 56 | + html = render(form) { |f| f.render f.field(:first_name).textarea } |
| 57 | + expect(html).to include('required') |
| 58 | + expect(html).to include('<textarea') |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + describe "#checkbox" do |
| 63 | + it "injects required attribute for presence-validated field" do |
| 64 | + html = render(form) { |f| f.render f.field(:first_name).checkbox } |
| 65 | + expect(html).to include('required') |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + describe "#select" do |
| 70 | + it "injects required attribute for presence-validated field" do |
| 71 | + html = render(form) { |f| f.render f.field(:first_name).select(["A", "B"]) } |
| 72 | + expect(html).to include('required') |
| 73 | + expect(html).to include('<select') |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + describe "#radio" do |
| 78 | + it "injects required attribute for presence-validated field" do |
| 79 | + html = render(form) { |f| f.render f.field(:first_name).radio("male") } |
| 80 | + expect(html).to include('required') |
| 81 | + expect(html).to include('type="radio"') |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + describe "convenience methods" do |
| 86 | + let(:model) { Product.new } |
| 87 | + let(:form) { Superform::Rails::Form.new(model, action: "/products") } |
| 88 | + |
| 89 | + it "injects validation attributes through #number" do |
| 90 | + html = render(form) { |f| f.render f.field(:quantity).number } |
| 91 | + expect(html).to include('type="number"') |
| 92 | + expect(html).to include('min="0"') |
| 93 | + expect(html).to include('max="1000"') |
| 94 | + expect(html).to include('step="1"') |
| 95 | + end |
| 96 | + |
| 97 | + it "injects validation attributes through #text" do |
| 98 | + html = render(form) { |f| f.render f.field(:name).text } |
| 99 | + expect(html).to include('type="text"') |
| 100 | + expect(html).to include('required') |
| 101 | + expect(html).to include('minlength="2"') |
| 102 | + expect(html).to include('maxlength="100"') |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + describe "novalidate" do |
| 108 | + let(:model) { User.new } |
| 109 | + |
| 110 | + context "when novalidate is false (default)" do |
| 111 | + let(:form) { Superform::Rails::Form.new(model, action: "/users") } |
| 112 | + |
| 113 | + it "does not add novalidate to form tag" do |
| 114 | + html = render(form) |
| 115 | + expect(html).not_to include('novalidate') |
| 116 | + end |
| 117 | + |
| 118 | + it "injects validation attributes" do |
| 119 | + html = render(form) { |f| f.render f.field(:first_name).input } |
| 120 | + expect(html).to include('required') |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + context "when novalidate is true" do |
| 125 | + let(:novalidate_form_class) do |
| 126 | + Class.new(Superform::Rails::Form) do |
| 127 | + def novalidate = true |
| 128 | + end |
| 129 | + end |
| 130 | + let(:form) { novalidate_form_class.new(model, action: "/users") } |
| 131 | + |
| 132 | + it "adds novalidate to form tag" do |
| 133 | + html = render(form) |
| 134 | + expect(html).to include('novalidate') |
| 135 | + end |
| 136 | + |
| 137 | + it "does not inject validation attributes" do |
| 138 | + html = render(form) { |f| f.render f.field(:first_name).input } |
| 139 | + expect(html).not_to match(/required(?!.*novalidate)/) |
| 140 | + # The form tag itself has novalidate, but the input should not have required |
| 141 | + input_tag = html.match(/<input[^>]*name="user\[first_name\]"[^>]*>/)[0] |
| 142 | + expect(input_tag).not_to include('required') |
| 143 | + end |
| 144 | + end |
| 145 | + end |
| 146 | +end |
0 commit comments