Merge pull request #11400 from jibees/buu-update/refactor-tests

🚧 BUU: refactor/improve testing
This commit is contained in:
Maikel
2023-08-17 16:32:00 +10:00
committed by GitHub
8 changed files with 100 additions and 49 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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

4
spec/reflex_helper.rb Normal file
View File

@@ -0,0 +1,4 @@
# frozen_string_literal: true
require "base_spec_helper"
require "stimulus_reflex_testing/rspec"

View File

@@ -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

View File

@@ -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)