diff --git a/spec/base_spec_helper.rb b/spec/base_spec_helper.rb index 9a07aea552..02007105ec 100644 --- a/spec/base_spec_helper.rb +++ b/spec/base_spec_helper.rb @@ -302,4 +302,5 @@ RSpec.configure do |config| config.include Features::TrixEditorHelper, type: :system config.include DownloadsHelper, type: :system config.include ReportsHelper, type: :system + config.include ProductsHelper, type: :system end diff --git a/spec/support/products_helper.rb b/spec/support/products_helper.rb new file mode 100644 index 0000000000..f9baececed --- /dev/null +++ b/spec/support/products_helper.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +module ProductsHelper + def create_products(amount) + amount.times do |i| + create(:simple_product, name: "product #{i}", supplier: producer) + end + end + + def expect_page_to_be(page_number) + expect(page).to have_selector ".pagination .page.current", text: page_number.to_s + end + + def expect_per_page_to_be(per_page) + expect(page).to have_selector "#per_page", text: per_page.to_s + end + + def expect_products_count_to_be(count) + expect(page).to have_selector("table.products tbody", count:) + end + + def search_for(term) + fill_in "search_term", with: term + click_button "Search" + end + + def search_by_producer(producer) + tomselect_select producer, from: "producer_id" + click_button "Search" + end + + def search_by_category(category) + tomselect_select category, from: "category_id" + click_button "Search" + end + + # Selector for table row that has an input with this value. + # Because there are no visible labels, the user has to assume which product it is, based on the + # visible name. + def row_containing_name(value) + "tr:has(input[aria-label=Name][value='#{value}'])" + end + + # Wait for an element with the given CSS selector and class to be present + def wait_for_class(selector, class_name) + max_wait_time = Capybara.default_max_wait_time + Timeout.timeout(max_wait_time) do + sleep(0.1) until page.has_css?(selector, class: class_name, visible: false) + end + end + + def expect_page_to_have_image(url) + expect(page).to have_selector("img[src$='#{url}']") + end + + def tax_category_column + @tax_category_column ||= '[data-controller="variant"] > td:nth-child(10)' + end + + def validate_tomselect_without_search!(page, field_name, search_selector) + open_tomselect_to_validate!(page, field_name) do + expect(page).not_to have_selector(search_selector) + end + end + + def validate_tomselect_with_search!(page, field_name, search_selector) + open_tomselect_to_validate!(page, field_name) do + expect(page).to have_selector(search_selector) + end + end + + def random_producer(product) + Enterprise.is_primary_producer + .where.not(id: product.supplier.id) + .pluck(:name).sample + end + + def random_category(variant) + Spree::Taxon + .where.not(id: variant.primary_taxon.id) + .pluck(:name).sample + end + + def random_tax_category + Spree::TaxCategory + .pluck(:name).sample + end + + def all_input_values + page.find_all('input[type=text]').map(&:value).join + end + + def click_product_clone(product_name) + within row_containing_name(product_name) do + page.find(".vertical-ellipsis-menu").click + click_link('Clone') + end + end +end diff --git a/spec/system/admin/products_v3/products_spec.rb b/spec/system/admin/products_v3/products_spec.rb index 3398c66d68..dbcc5f4e19 100644 --- a/spec/system/admin/products_v3/products_spec.rb +++ b/spec/system/admin/products_v3/products_spec.rb @@ -1709,100 +1709,4 @@ RSpec.describe 'As an enterprise user, I can manage my products', feature: :admi it_behaves_like "creating a new variant (bulk)", "on_hand" it_behaves_like "creating a new variant (bulk)", "on_demand" end - - def create_products(amount) - amount.times do |i| - create(:simple_product, name: "product #{i}", supplier: producer) - end - end - - def expect_page_to_be(page_number) - expect(page).to have_selector ".pagination .page.current", text: page_number.to_s - end - - def expect_per_page_to_be(per_page) - expect(page).to have_selector "#per_page", text: per_page.to_s - end - - def expect_products_count_to_be(count) - expect(page).to have_selector("table.products tbody", count:) - end - - def search_for(term) - fill_in "search_term", with: term - click_button "Search" - end - - def search_by_producer(producer) - tomselect_select producer, from: "producer_id" - click_button "Search" - end - - def search_by_category(category) - tomselect_select category, from: "category_id" - click_button "Search" - end - - # Selector for table row that has an input with this value. - # Because there are no visible labels, the user has to assume which product it is, based on the - # visible name. - def row_containing_name(value) - "tr:has(input[aria-label=Name][value='#{value}'])" - end - - # Wait for an element with the given CSS selector and class to be present - def wait_for_class(selector, class_name) - max_wait_time = Capybara.default_max_wait_time - Timeout.timeout(max_wait_time) do - sleep(0.1) until page.has_css?(selector, class: class_name, visible: false) - end - end - - def expect_page_to_have_image(url) - expect(page).to have_selector("img[src$='#{url}']") - end - - def tax_category_column - @tax_category_column ||= '[data-controller="variant"] > td:nth-child(10)' - end - - def validate_tomselect_without_search!(page, field_name, search_selector) - open_tomselect_to_validate!(page, field_name) do - expect(page).not_to have_selector(search_selector) - end - end - - def validate_tomselect_with_search!(page, field_name, search_selector) - open_tomselect_to_validate!(page, field_name) do - expect(page).to have_selector(search_selector) - end - end - - def random_producer(product) - Enterprise.is_primary_producer - .where.not(id: product.supplier.id) - .pluck(:name).sample - end - - def random_category(variant) - Spree::Taxon - .where.not(id: variant.primary_taxon.id) - .pluck(:name).sample - end - - def random_tax_category - Spree::TaxCategory - .pluck(:name).sample - end - - def all_input_values - page.find_all('input[type=text]').map(&:value).join - end - - def click_product_clone(product_name) - within row_containing_name(product_name) do - page.find(".vertical-ellipsis-menu").click - click_link('Clone') - end - end end