diff --git a/Gemfile b/Gemfile index 6eab0a3447..b0fd553864 100644 --- a/Gemfile +++ b/Gemfile @@ -159,6 +159,7 @@ group :test, :development do gem 'rspec-retry', require: false gem 'rswag-specs' gem 'shoulda-matchers' + gem 'stimulus_reflex_testing' gem 'timecop' end diff --git a/Gemfile.lock b/Gemfile.lock index 201e64776e..8265fceb4a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -705,6 +705,8 @@ GEM rack (>= 2, < 4) railties (>= 5.2, < 8) redis (>= 4.0, < 6.0) + stimulus_reflex_testing (0.3.0) + stimulus_reflex (>= 3.3.0) stringex (2.8.6) stripe (8.6.0) swd (1.3.0) @@ -899,6 +901,7 @@ DEPENDENCIES spring-commands-rspec state_machines-activerecord stimulus_reflex (= 3.5.0.rc3) + stimulus_reflex_testing stringex (~> 2.8.5) stripe timecop diff --git a/app/reflexes/products_reflex.rb b/app/reflexes/products_reflex.rb index 443ac5f0d3..295341bc02 100644 --- a/app/reflexes/products_reflex.rb +++ b/app/reflexes/products_reflex.rb @@ -96,7 +96,7 @@ class ProductsReflex < ApplicationReflex end def ransack_query - query = { s: "name desc" } + query = {} query.merge!(supplier_id_in: @producer_id) if @producer_id.present? if @search_term.present? query.merge!(Spree::Variant::SEARCH_KEY => @search_term) diff --git a/app/views/spree/admin/shared/_product_sub_menu.html.haml b/app/views/spree/admin/shared/_product_sub_menu.html.haml index 3013263108..1215f1a02d 100644 --- a/app/views/spree/admin/shared/_product_sub_menu.html.haml +++ b/app/views/spree/admin/shared/_product_sub_menu.html.haml @@ -5,4 +5,4 @@ = tab :variant_overrides, url: main_app.admin_inventory_path, match_path: '/inventory' = tab :import, url: main_app.admin_product_import_path, match_path: '/product_import' - if feature?(:admin_style_v3, spree_current_user) - = tab :products_v3, url: main_app.admin_products_v3_path + = tab :products_v3, url: main_app.admin_products_v3_index_path diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 3f2259c35a..dea9207fb2 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -70,7 +70,7 @@ Openfoodnetwork::Application.routes.draw do post '/product_import/reset_absent', to: 'product_import#reset_absent_products', as: 'product_import_reset_async' constraints FeatureToggleConstraint.new(:admin_style_v3) do - get '/products_v3', to: 'products_v3#index' + resources :products_v3, only: :index end resources :variant_overrides do diff --git a/spec/reflex_helper.rb b/spec/reflex_helper.rb new file mode 100644 index 0000000000..4f36f3b6f8 --- /dev/null +++ b/spec/reflex_helper.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +require "base_spec_helper" +require "stimulus_reflex_testing/rspec" diff --git a/spec/reflexes/products_reflex_spec.rb b/spec/reflexes/products_reflex_spec.rb new file mode 100644 index 0000000000..4b1b6a6b90 --- /dev/null +++ b/spec/reflexes/products_reflex_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require "reflex_helper" + +describe ProductsReflex, type: :reflex do + let(:current_user) { create(:admin_user) } # todo: set up an enterprise user to test permissions + let(:context) { + { url: admin_products_v3_index_url, connection: { current_user: } } + } + + before do + # activate feature toggle admin_style_v3 to use new admin interface + Flipper.enable(:admin_style_v3) + end + + describe 'fetch' do + subject{ build_reflex(method_name: :fetch, **context) } + + describe "sorting" do + let!(:product_z) { create(:simple_product, name: "Zucchini") } + # let!(:product_b) { create(:simple_product, name: "bananas") } # Fails on macOS + let!(:product_a) { create(:simple_product, name: "Apples") } + + it "Should sort products alphabetically by default" do + subject.run(:fetch) + + expect(subject.get(:products).to_a).to eq [ + product_a, + # product_b, + product_z, + ] + end + end + end +end diff --git a/spec/system/admin/products_v3/products_spec.rb b/spec/system/admin/products_v3/products_spec.rb index c83e073fd9..1935200e6d 100644 --- a/spec/system/admin/products_v3/products_spec.rb +++ b/spec/system/admin/products_v3/products_spec.rb @@ -11,15 +11,6 @@ describe 'As an admin, I can see the new product page' do 70.times do |i| let!("product_#{i}".to_sym) { create(:simple_product, name: "product #{i}") } end - # create a product with a name that can be searched - let!(:product_by_name) { create(:simple_product, name: "searchable product") } - # create a product with a supplier that can be searched - let!(:producer) { create(:supplier_enterprise, name: "Producer 1") } - let!(:product_by_supplier) { create(:simple_product, supplier: producer) } - # create a product with a category that can be searched - let!(:product_by_category) { - create(:simple_product, primary_taxon: create(:taxon, name: "Category 1")) - } before do # activate feature toggle admin_style_v3 to use new admin interface @@ -28,13 +19,26 @@ describe 'As an admin, I can see the new product page' do end it "can see the new product page" do - visit "/admin/products_v3" + visit admin_products_v3_index_url expect(page).to have_content "Bulk Edit Products" end - context "pagination" do - before :each do - visit "/admin/products_v3" + describe "sorting" do + let!(:product_z) { create(:simple_product, name: "Bananas") } + let!(:product_a) { create(:simple_product, name: "Apples") } + + before do + visit admin_products_v3_index_url + end + + it "Should sort products alphabetically by default" do + expect(page).to have_content /Apples.*Bananas/ + end + end + + describe "pagination" do + before do + visit admin_products_v3_index_url end it "has a pagination, has 15 products per page by default and can change the page" do @@ -56,12 +60,15 @@ describe 'As an admin, I can see the new product page' do end end - context "search" do - before :each do - visit "/admin/products_v3" + describe "search" do + before do + visit admin_products_v3_index_url end - context "search by search term" do + context "product has searchable term" do + # create a product with a name that can be searched + let!(:product_by_name) { create(:simple_product, name: "searchable product") } + it "can search for a product" do search_for "searchable product" @@ -81,34 +88,7 @@ describe 'As an admin, I can see the new product page' do expect_page_to_be 1 expect_products_count_to_be 1 end - end - context "search by producer" do - it "has a producer select" do - expect(page).to have_selector "select#producer_id" - end - - it "can search for a product" do - search_by_producer "Producer 1" - - expect(page).to have_select "producer_id", selected: "Producer 1" - expect_page_to_be 1 - expect_products_count_to_be 1 - end - end - - context "search by category" do - it "can search for a product" do - search_by_category "Category 1" - - expect(page).to have_select "category_id", selected: "Category 1" - expect_page_to_be 1 - expect_products_count_to_be 1 - expect(page).to have_selector "table.products tbody tr td", text: product_by_category.name - end - end - - context "clear filters" do it "can clear filters" do search_for "searchable product" expect(page).to have_field "search_term", with: "searchable product" @@ -121,15 +101,43 @@ describe 'As an admin, I can see the new product page' do expect_page_to_be 1 expect_products_count_to_be 15 end - end - context "no results" do it "shows a message when there are no results" do search_for "no results" expect(page).to have_content "No products found for your search criteria" expect(page).to have_link "Clear search" end end + + context "product has producer" do + # create a product with a supplier that can be searched + let!(:producer) { create(:supplier_enterprise, name: "Producer 1") } + let!(:product_by_supplier) { create(:simple_product, supplier: producer) } + + it "can search for a product" do + search_by_producer "Producer 1" + + expect(page).to have_select "producer_id", selected: "Producer 1" + expect_page_to_be 1 + expect_products_count_to_be 1 + end + end + + context "product has category" do + # create a product with a category that can be searched + let!(:product_by_category) { + create(:simple_product, primary_taxon: create(:taxon, name: "Category 1")) + } + + it "can search for a product" do + search_by_category "Category 1" + + expect(page).to have_select "category_id", selected: "Category 1" + expect_page_to_be 1 + expect_products_count_to_be 1 + expect(page).to have_selector "table.products tbody tr td", text: product_by_category.name + end + end end def expect_page_to_be(page_number)