|
94 | 94 | end |
95 | 95 | end |
96 | 96 |
|
| 97 | + context 'class with nested belongs_to association' do |
| 98 | + before(:all) do |
| 99 | + ActiveAdminHelpers.setup do |
| 100 | + ActiveAdmin.register(OptionType) |
| 101 | + |
| 102 | + ActiveAdmin.register(Product) |
| 103 | + |
| 104 | + ActiveAdmin.register(OptionValue) do |
| 105 | + belongs_to :option_type |
| 106 | + searchable_select_options(scope: lambda do |params| |
| 107 | + OptionValue.where( |
| 108 | + option_type_id: params[:option_type_id] |
| 109 | + ) |
| 110 | + end, |
| 111 | + text_attribute: :value) |
| 112 | + end |
| 113 | + |
| 114 | + ActiveAdmin.register(Variant) do |
| 115 | + belongs_to :product |
| 116 | + |
| 117 | + form do |f| |
| 118 | + input :price |
| 119 | + input(:option_value, |
| 120 | + as: :searchable_select, |
| 121 | + ajax: { |
| 122 | + resource: OptionValue, |
| 123 | + path_params: { |
| 124 | + option_type_id: f.object.product.option_type_id |
| 125 | + } |
| 126 | + }) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + ActiveAdmin.setup {} |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + describe 'new page with searchable select filter' do |
| 135 | + it 'loads filter input options' do |
| 136 | + option_type = OptionType.create(name: 'Color') |
| 137 | + ot = OptionType.create(name: 'Size') |
| 138 | + OptionValue.create(value: 'Black', option_type: option_type) |
| 139 | + OptionValue.create(value: 'Orange', option_type: option_type) |
| 140 | + OptionValue.create(value: 'M', option_type: ot) |
| 141 | + product = Product.create(name: 'Cap', option_type: option_type) |
| 142 | + |
| 143 | + visit "/admin/products/#{product.id}/variants/new" |
| 144 | + |
| 145 | + expand_select_box |
| 146 | + wait_for_ajax |
| 147 | + |
| 148 | + expect(select_box_items).to eq(%w(Black Orange)) |
| 149 | + end |
| 150 | + |
| 151 | + it 'allows filtering options by term' do |
| 152 | + option_type = OptionType.create(name: 'Color') |
| 153 | + ot = OptionType.create(name: 'Size') |
| 154 | + OptionValue.create(value: 'Black', option_type: option_type) |
| 155 | + OptionValue.create(value: 'Orange', option_type: option_type) |
| 156 | + OptionValue.create(value: 'M', option_type: ot) |
| 157 | + product = Product.create(name: 'Cap', option_type: option_type) |
| 158 | + |
| 159 | + visit "/admin/products/#{product.id}/variants/new" |
| 160 | + |
| 161 | + expand_select_box |
| 162 | + enter_search_term('O') |
| 163 | + wait_for_ajax |
| 164 | + |
| 165 | + expect(select_box_items).to eq(%w(Orange)) |
| 166 | + end |
| 167 | + |
| 168 | + it 'loads more items when scrolling down' do |
| 169 | + option_type = OptionType.create(name: 'Color') |
| 170 | + 15.times { |i| OptionValue.create(value: "Black #{i}", option_type: option_type) } |
| 171 | + product = Product.create(name: 'Cap', option_type: option_type) |
| 172 | + |
| 173 | + visit "/admin/products/#{product.id}/variants/new" |
| 174 | + |
| 175 | + expand_select_box |
| 176 | + wait_for_ajax |
| 177 | + scroll_select_box_list |
| 178 | + wait_for_ajax |
| 179 | + |
| 180 | + expect(select_box_items.size).to eq(15) |
| 181 | + end |
| 182 | + end |
| 183 | + end |
| 184 | + |
97 | 185 | def expand_select_box |
98 | 186 | find('.select2-container').click |
99 | 187 | end |
|
0 commit comments