diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index 0f4dc4ad3f..d5c3f5383c 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -28,7 +28,7 @@ module Admin flash[:success] = I18n.t('admin.products_v3.bulk_update.success') redirect_to [:index, { page: @page, per_page: @per_page, search_term: @search_term, - producer_id: @producer_id, category_id: @category_id }] + producer_id: @producer_id, category_id: @category_id, tags_name_in: @tags }] elsif product_set.errors.present? @error_counts = { saved: product_set.saved_count, invalid: product_set.invalid.count } @@ -120,7 +120,7 @@ module Admin @search_term = params[:search_term] || params[:_search_term] @producer_id = params[:producer_id] || params[:_producer_id] @category_id = params[:category_id] || params[:_category_id] - @tags = params[:tags_name_in] || params[:_tags_name_in] + @tags = params[:tags_name_in] || [] end def init_pagination_params diff --git a/app/views/admin/products_v3/_table.html.haml b/app/views/admin/products_v3/_table.html.haml index 1f5fbf1ebe..068f531fe5 100644 --- a/app/views/admin/products_v3/_table.html.haml +++ b/app/views/admin/products_v3/_table.html.haml @@ -13,6 +13,8 @@ = hidden_field_tag :search_term, @search_term = hidden_field_tag :producer_id, @producer_id = hidden_field_tag :category_id, @category_id + - @tags.each do |tag| + = hidden_field_tag 'tags_name_in[]', tag %table.products{ 'data-column-preferences-target': "table", class: (hide_producer_column?(producer_options) ? 'hide-producer' : '') } %colgroup diff --git a/spec/support/tom_select_helper.rb b/spec/support/tom_select_helper.rb index d066bbe6ea..2f39b15b33 100644 --- a/spec/support/tom_select_helper.rb +++ b/spec/support/tom_select_helper.rb @@ -36,4 +36,21 @@ module TomSelectHelper find('.ts-dropdown .ts-dropdown-content .option', text: /#{Regexp.quote(value)}/i).click end + + def expect_tomselect_selected_options(from, *options) + tomselect_control = page + .find("[name='#{from}']") + .sibling(".ts-wrapper") + .find('.ts-control') + + within(tomselect_control) do + # options in case of we want to expect multiselect options + options.each do |option| + expect(page).to have_css( + "div[data-ts-item]", + text: option + ) + end + end + end end diff --git a/spec/system/admin/products_v3/update_spec.rb b/spec/system/admin/products_v3/update_spec.rb index 320f15d3b2..0da94594f6 100644 --- a/spec/system/admin/products_v3/update_spec.rb +++ b/spec/system/admin/products_v3/update_spec.rb @@ -731,6 +731,44 @@ RSpec.describe 'As an enterprise user, I can update my products' do expect(page).to have_css row_containing_name("zucchinis") end end + + context "when filters are applied" do + before do + variant_b1 # invoke to create the variant + variant_a1.tags.create!(name: 'organic') + visit admin_products_url + end + it 'persists the filters after update' do + # Ensure all products are shown + expect_products_count_to_be 2 + + supplier_name = variant_a1.supplier.name + category_name = variant_a1.primary_taxon.name + tag_name = variant_a1.tags.first.name + tomselect_select supplier_name, from: "producer_id" + tomselect_select category_name, from: "category_id" + tomselect_multiselect tag_name, from: "tags_name_in" + click_button "Search" + + # Ensure filters are selected and applied after search + expect_tomselect_selected_options("producer_id", supplier_name) + expect_tomselect_selected_options("category_id", category_name) + expect_tomselect_selected_options("tags_name_in[]", tag_name) + expect_products_count_to_be 1 + + within row_containing_name("Medium box") do + fill_in "Price", with: "10.25" + end + click_button "Save changes" + expect(page).to have_content "Changes saved" + + # Ensure filters still selected and applied even after update + expect_tomselect_selected_options("producer_id", supplier_name) + expect_tomselect_selected_options("category_id", category_name) + expect_tomselect_selected_options("tags_name_in[]", tag_name) + expect_products_count_to_be 1 + end + end end describe "edit image" do