Skip to content

Commit 748f049

Browse files
committed
Move pre-selection test for belongs to case to end-to-end spec
Kept getting error of the form undefined method `admin_product_product_variant_path' for #<Admin::VariantsController> when trying execute the test as part of the request spec. When running the test individually everything worked, but once the end-to-end test had run, the error occurred. I therefore suspect this might be caused by setting up the nested Active Admin resources twice. Testing both cases as part of the end-to-end spec helps work around the problem.
1 parent dcf19bc commit 748f049

File tree

2 files changed

+20
-48
lines changed

2 files changed

+20
-48
lines changed

spec/features/ajax_params_spec.rb

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -50,52 +50,4 @@
5050
expect(response.body).to have_selector('.searchable-select-input' \
5151
"[data-ajax-url*='#{url_matcher}']")
5252
end
53-
54-
context 'when using belongs_to' do
55-
before(:each) do
56-
ActiveAdminHelpers.setup do
57-
58-
ActiveAdmin.register(OptionType)
59-
60-
ActiveAdmin.register(Product) do
61-
62-
ActiveAdmin.register(OptionValue) do
63-
belongs_to :option_type
64-
searchable_select_options(scope: lambda do |params|
65-
OptionValue.where(
66-
option_type_id: params[:option_type_id]
67-
)
68-
end,
69-
text_attribute: :value)
70-
end
71-
72-
ActiveAdmin.register(Variant) do
73-
belongs_to :product
74-
75-
form do |f|
76-
f.input(:option_value,
77-
as: :searchable_select,
78-
ajax: {
79-
resource: OptionValue,
80-
path_params: {
81-
option_type_id: f.object.product.option_type_id
82-
}
83-
})
84-
end
85-
end
86-
end
87-
end
88-
89-
it 'pre-select items in the associations' do
90-
option_type = OptionType.create
91-
product = Product.create(option_type: option_type)
92-
option_value = OptionValue.create(option_type: option_type, value: 'Red')
93-
variant = Variant.create(product: product, option_value: option_value)
94-
95-
get "/admin/products/#{product.id}/variants/#{variant.id}/edit"
96-
97-
expect(response.body).to have_selector('.searchable-select-input option[selected]',
98-
count: 1)
99-
end
100-
end
10153
end

spec/features/end_to_end_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,22 @@
180180
expect(select_box_items.size).to eq(15)
181181
end
182182
end
183+
184+
describe 'edit page with searchable select filter' do
185+
it 'preselects item' do
186+
option_type = OptionType.create(name: 'Color')
187+
ot = OptionType.create(name: 'Size')
188+
option_value = OptionValue.create(value: 'Black', option_type: option_type)
189+
OptionValue.create(value: 'Orange', option_type: option_type)
190+
OptionValue.create(value: 'M', option_type: ot)
191+
product = Product.create(name: 'Cap', option_type: option_type)
192+
variant = Variant.create(product: product, option_value: option_value)
193+
194+
visit "/admin/products/#{product.id}/variants/#{variant.id}/edit"
195+
196+
expect(select_box_selected_item_text).to eq('Black')
197+
end
198+
end
183199
end
184200

185201
def expand_select_box
@@ -198,6 +214,10 @@ def select_box_items
198214
all('.select2-dropdown li').map(&:text)
199215
end
200216

217+
def select_box_selected_item_text
218+
find('.select2-selection').text
219+
end
220+
201221
def wait_for_ajax
202222
Timeout.timeout(Capybara.default_max_wait_time) do
203223
sleep 0.1

0 commit comments

Comments
 (0)