1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
# frozen_string_literal: true
require 'test_helper'
Formtastic::FormBuilder.action_class_finder = Formtastic::ActionClassFinder
Formtastic::FormBuilder.input_class_finder = Formtastic::InputClassFinder
class FormtasticSpec < Minitest::Spec
include ViewTestHelper
include Formtastic::Helpers::FormHelper
class Thing < Struct.new(:name)
extend ActiveModel::Naming
include ActiveModel::Conversion
def persisted?
false
end
end
class User < Struct.new(:sex, :age)
extend ActiveModel::Naming
include ActiveModel::Conversion
extend Enumerize
enumerize :sex, :in => [:male, :female]
def persisted?
false
end
end
class Post < Struct.new(:category, :title)
extend ActiveModel::Naming
include ActiveModel::Conversion
extend Enumerize
enumerize :categories, :in => [:music, :games], :multiple => true
def persisted?
false
end
end
class Registration < Struct.new(:sex)
extend Enumerize
enumerize :sex, in: [:male, :female]
end
before { $VERBOSE = nil }
after { $VERBOSE = true }
let(:user) { User.new }
let(:post) { Post.new }
it 'renders select with enumerized values' do
concat(semantic_form_for(user) do |f|
f.input :sex
end)
assert_select 'select option[value=male]'
assert_select 'select option[value=female]'
end
it 'renders multiple select with enumerized values' do
concat(semantic_form_for(post) do |f|
f.input(:categories)
end)
assert_select 'select[multiple=multiple]'
assert_select 'select option[value=music]'
assert_select 'select option[value=games]'
end
it 'renders multiple select with selected enumerized value' do
post.categories << :music
concat(semantic_form_for(post) do |f|
f.input(:categories)
end)
assert_select 'select[multiple=multiple]'
assert_select 'select option[value=music][selected=selected]'
assert_select 'select option[value=games][selected=selected]', count: 0
end
it 'renders checkboxes with enumerized values' do
concat(semantic_form_for(post) do |f|
f.input(:categories, as: :check_boxes)
end)
assert_select 'select[multiple=multiple]', count: 0
assert_select 'input[type=checkbox][value=music]'
assert_select 'input[type=checkbox][value=games]'
end
it 'renders checkboxes with selected enumerized value' do
post.categories << :music
concat(semantic_form_for(post) do |f|
f.input(:categories, as: :check_boxes)
end)
assert_select 'input[type=checkbox][value=music][checked=checked]'
assert_select 'input[type=checkbox][value=games][checked=checked]', count: 0
end
it 'renders radio buttons with enumerized values' do
concat(semantic_form_for(user) do |f|
f.input :sex, :as => :radio
end)
assert_select 'input[type=radio][value=male]'
assert_select 'input[type=radio][value=female]'
end
it 'does not affect not enumerized attributes' do
concat(semantic_form_for(user) do |f|
f.input(:age)
end)
assert_select 'input[type=text]'
end
it 'does not affect not enumerized classes' do
concat(semantic_form_for(Thing.new) do |f|
f.input(:name)
end)
assert_select 'input[type=text]'
end
it 'renders select with enumerized values for non-ActiveModel object' do
concat(semantic_form_for(Registration.new, as: 'registration', url: '/') do |f|
f.input(:sex)
end)
assert_select 'select option[value=male]'
assert_select 'select option[value=female]'
end
it 'does not affect forms without object' do
concat(semantic_form_for('') do |f|
f.input(:name)
end)
assert_select 'input[type=text]'
end
end
|