mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
Extracts helper methods into helper file
The idea is to split the main spec into several smaller ones; these would share the helper file
This commit is contained in:
committed by
David Cook
parent
a83daae873
commit
5cf8eb5efc
@@ -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
|
||||
|
||||
99
spec/support/products_helper.rb
Normal file
99
spec/support/products_helper.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user