Merge pull request #5941 from luisramos0/no_spree_specs

[Bye bye Spree] Make OFN independent of spec helpers under core/lib/testing_support
This commit is contained in:
Matt-Yorkley
2020-08-27 17:05:45 +02:00
committed by GitHub
18 changed files with 192 additions and 55 deletions

View File

@@ -1,7 +1,6 @@
module OpenFoodNetwork
class EnterpriseIssueValidator
include Rails.application.routes.url_helpers
include Spree::TestingSupport::UrlHelpers
def initialize(enterprise)
@enterprise = enterprise
@@ -13,14 +12,14 @@ module OpenFoodNetwork
unless shipping_methods_ok?
issues << {
description: I18n.t('admin.enterprise_issues.has_no_shipping_methods', enterprise: @enterprise.name),
link: "<a class='button fullwidth' href='#{spree.new_admin_shipping_method_path}'>#{I18n.t('admin.enterprise_issues.create_new')}</a>"
link: "<a class='button fullwidth' href='#{spree_routes_helper.new_admin_shipping_method_path}'>#{I18n.t('admin.enterprise_issues.create_new')}</a>"
}
end
unless payment_methods_ok?
issues << {
description: I18n.t('admin.enterprise_issues.has_no_payment_methods', enterprise: @enterprise.name),
link: "<a class='button fullwidth' href='#{spree.new_admin_payment_method_path}'>#{I18n.t('admin.enterprise_issues.create_new')}</a>"
link: "<a class='button fullwidth' href='#{spree_routes_helper.new_admin_payment_method_path}'>#{I18n.t('admin.enterprise_issues.create_new')}</a>"
}
end
@@ -64,5 +63,9 @@ module OpenFoodNetwork
@enterprise.payment_methods.available.any?
end
def spree_routes_helper
Spree::Core::Engine.routes.url_helpers
end
end
end

View File

@@ -1,5 +1,4 @@
require 'spec_helper'
require 'spree/testing_support/bar_ability'
describe Spree::Admin::UsersController do
context '#authorize_admin' do
@@ -34,18 +33,31 @@ describe Spree::Admin::UsersController do
expect(response).to redirect_to(spree.edit_admin_user_path(test_user))
end
it 'should deny access to users with an bar role' do
user.spree_roles << Spree::Role.find_or_create_by(name: 'bar')
Spree::Ability.register_ability(BarAbility)
spree_post :index
expect(response).to redirect_to('/unauthorized')
end
describe "with BarAbility" do
class BarAbility
include CanCan::Ability
it 'should deny access to users with an bar role' do
user.spree_roles << Spree::Role.find_or_create_by(name: 'bar')
Spree::Ability.register_ability(BarAbility)
spree_post :update, id: '9'
expect(response).to redirect_to('/unauthorized')
def initialize(user)
user ||= Spree::User.new
return unless user.has_spree_role?('bar')
can [:admin, :index, :show], Spree::Order
end
end
it 'should deny access to users with an bar role' do
user.spree_roles << Spree::Role.find_or_create_by(name: 'bar')
Spree::Ability.register_ability(BarAbility)
spree_post :index
expect(response).to redirect_to('/unauthorized')
end
it 'should deny access to users with an bar role' do
user.spree_roles << Spree::Role.find_or_create_by(name: 'bar')
Spree::Ability.register_ability(BarAbility)
spree_post :update, id: '9'
expect(response).to redirect_to('/unauthorized')
end
end
it 'should deny access to users without an admin role' do

View File

@@ -2,6 +2,7 @@ require 'spec_helper'
describe "States" do
include AuthenticationHelper
include WebHelper
let!(:country) { create(:country) }

View File

@@ -2,6 +2,7 @@ require 'spec_helper'
describe "Tax Categories" do
include AuthenticationHelper
include WebHelper
before(:each) do
login_as_admin_and_visit spree.edit_admin_general_settings_path
@@ -13,9 +14,9 @@ describe "Tax Categories" do
click_link "Tax Categories"
expect(page).to have_content("Listing Tax Categories")
within_row(1) do
expect(column_text(1)).to eq("Clothing")
expect(column_text(2)).to eq("For Clothing")
expect(column_text(3)).to eq("False")
expect(find("td:nth-child(1)").text).to eq("Clothing")
expect(find("td:nth-child(2)").text).to eq("For Clothing")
expect(find("td:nth-child(3)").text).to eq("False")
end
end
end
@@ -44,7 +45,7 @@ describe "Tax Categories" do
it "should be able to update an existing tax category" do
create(:tax_category)
click_link "Tax Categories"
within_row(1) { click_icon :edit }
within_row(1) { find(".icon-edit").click }
fill_in "tax_category_description", with: "desc 99"
click_button "Update"
expect(page).to have_content("successfully updated!")

View File

@@ -2,6 +2,7 @@ require 'spec_helper'
describe "Taxonomies" do
include AuthenticationHelper
include WebHelper
before(:each) do
login_as_admin_and_visit spree.edit_admin_general_settings_path
@@ -43,7 +44,7 @@ describe "Taxonomies" do
it "should allow an admin to update an existing taxonomy" do
create(:taxonomy)
click_link "Taxonomies"
within_row(1) { click_icon :edit }
within_row(1) { find(".icon-edit").click }
fill_in "taxonomy_name", with: "sports 99"
click_button "Update"
expect(page).to have_content("successfully updated!")

View File

@@ -2,6 +2,7 @@ require 'spec_helper'
describe "Zones" do
include AuthenticationHelper
include WebHelper
before do
login_as_admin

View File

@@ -33,8 +33,8 @@ feature '
fill_in 'enterprise_group_name', with: 'EGEGEG'
fill_in 'enterprise_group_description', with: 'This is a description'
check 'enterprise_group_on_front_page'
select2_search e1.name, from: 'Enterprises'
select2_search e2.name, from: 'Enterprises'
targetted_select2_search e1.name, from: '#s2id_enterprise_group_enterprise_ids'
targetted_select2_search e2.name, from: '#s2id_enterprise_group_enterprise_ids'
click_link 'Contact'
fill_in 'enterprise_group_address_attributes_phone', with: '000'
fill_in 'enterprise_group_address_attributes_address1', with: 'My Street'

View File

@@ -105,7 +105,7 @@ feature '
uncheck 'Only show complete orders'
page.find('a.icon-search').click
click_icon :edit
find(".icon-edit").click
expect(page).to have_current_path spree.edit_admin_order_path(incomplete_order)
end

View File

@@ -6,7 +6,6 @@ feature '
', js: true do
include WebHelper
include AuthenticationHelper
include ::Spree::TestingSupport::AuthorizationHelpers
context "as an enterprise user" do
before do

View File

@@ -534,7 +534,7 @@ feature 'Subscriptions' do
def add_variant_to_subscription(variant, quantity)
row_count = all("#subscription-line-items .item").length
variant_name = variant.full_name.present? ? "#{variant.name} - #{variant.full_name}" : variant.name
select2_search variant.name, from: I18n.t(:name_or_sku), dropdown_css: ".select2-drop", select_text: variant_name
targetted_select2_search variant.name, from: "#s2id_add_variant_id", dropdown_css: ".select2-drop", select_text: variant_name
fill_in "add_quantity", with: quantity
click_link "Add"
expect(page).to have_selector("#subscription-line-items .item", count: row_count + 1)

View File

@@ -1,3 +1,4 @@
require 'spec_helper'
require 'open_food_network/enterprise_issue_validator'
module OpenFoodNetwork

View File

@@ -2,7 +2,7 @@
require 'rspec/expectations'
require 'spree/i18n'
require 'spree/testing_support/i18n'
require 'support/i18n_translations_checker'
describe "i18n" do
before do

View File

@@ -3,6 +3,8 @@
require 'spec_helper'
describe Spree::Money do
include PreferencesHelper
before do
configure_spree_preferences do |config|
config.currency = "USD"

View File

@@ -32,8 +32,7 @@ Shoulda::Matchers.configure do |config|
end
end
# Allow connections to phantomjs/selenium whilst raising errors
# when connecting to external sites
# Allow connections to selenium whilst raising errors when connecting to external sites
require 'webmock/rspec'
WebMock.enable!
WebMock.disable_net_connect!(
@@ -44,10 +43,7 @@ WebMock.disable_net_connect!(
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |f| require f }
require 'spree/testing_support/capybara_ext'
require 'spree/api/testing_support/setup'
require 'spree/testing_support/authorization_helpers'
require 'spree/testing_support/preferences'
require 'support/api_helper'
# Capybara config
@@ -65,6 +61,11 @@ end
Capybara.default_max_wait_time = 30
Capybara.configure do |config|
config.match = :prefer_exact
config.ignore_hidden_elements = true
end
require "paperclip/matchers"
# Override setting in Spree engine: Spree::Core::MailSettings
@@ -155,7 +156,7 @@ RSpec.configure do |config|
config.include Spree::UrlHelpers
config.include Spree::CheckoutHelpers
config.include Spree::MoneyHelper
config.include Spree::TestingSupport::Preferences
config.include PreferencesHelper
config.include ControllerRequestsHelper, type: :controller
config.include Devise::TestHelpers, type: :controller
config.extend Spree::Api::TestingSupport::Setup, type: :controller

View File

@@ -0,0 +1,98 @@
# frozen_string_literal: true
# This file exists solely to test whether or not there are missing translations
# within the code that Spree's test suite covers.
#
# If there is a translation referenced which has no corresponding key within the
# .yml file, then there will be a message output at the end of the suite showing
# that.
#
# If there is a translation within the locale file which *isn't* used in the
# test, this will also be shown at the end of the suite run.
module Spree
class << self
attr_accessor :used_translations, :missing_translation_messages,
:unused_translations, :unused_translation_messages
alias_method :normal_t, :t
end
def self.t(*args)
original_args = args.dup
options = args.extract_options!
self.used_translations ||= []
[*args.first].each do |translation_key|
key = ([*options[:scope]] << translation_key).join('.')
self.used_translations << key
end
normal_t(*original_args)
end
def self.check_missing_translations
self.missing_translation_messages = []
self.used_translations ||= []
used_translations.map { |a| a.split('.') }.each do |translation_keys|
root = translations
processed_keys = []
translation_keys.each do |key|
begin
root = root.fetch(key.to_sym)
processed_keys << key.to_sym
rescue KeyError
error = "#{(processed_keys << key).join('.')} (#{I18n.locale})"
unless Spree.missing_translation_messages.include?(error)
Spree.missing_translation_messages << error
end
end
end
end
end
def self.check_unused_translations
self.used_translations ||= []
self.unused_translation_messages = []
self.unused_translations = []
load_translations(translations)
translation_diff = unused_translations - used_translations
translation_diff.each do |translation|
Spree.unused_translation_messages << "#{translation} (#{I18n.locale})"
end
end
def self.load_translations(hash, root = [])
hash.each do |k, v|
if v.is_a?(Hash)
load_translations(v, root.dup << k)
else
key = (root + [k]).join('.')
unused_translations << key
end
end
end
private_class_method :load_translations
def self.translations
@translations ||= I18n.backend.__send__(:translations)[I18n.locale][:spree]
end
private_class_method :translations
end
RSpec.configure do |config|
# Need to check here again because this is used in i18n_spec too.
if ENV['CHECK_TRANSLATIONS']
config.after :suite do
Spree.check_missing_translations
if Spree.missing_translation_messages.any?
puts "\nThere are missing translations within Spree:"
puts Spree.missing_translation_messages.sort
exit(1)
end
Spree.check_unused_translations
if Spree.unused_translation_messages.any?
puts "\nThere are unused translations within Spree:"
puts Spree.unused_translation_messages.sort
exit(1)
end
end
end
end

View File

@@ -6,7 +6,6 @@ RSpec::Matchers.define :have_select2 do |id, options = {}|
match do |node|
@id, @options, @node = id, options, node
# id = find_label_by_text(locator)
from = "#s2id_#{id}"
results = []
@@ -35,7 +34,6 @@ RSpec::Matchers.define :have_select2 do |id, options = {}|
match_when_negated do |node|
@id, @options, @node = id, options, node
# id = find_label_by_text(locator)
from = "#s2id_#{id}"
results = []

View File

@@ -0,0 +1,23 @@
# frozen_string_literal: true
module PreferencesHelper
# Resets all preferences to default values, you can
# pass a block to override the defaults with a block
#
# reset_spree_preferences do |config|
# config.site_name = "my fancy pants store"
# end
#
def reset_spree_preferences(&config_block)
Spree::Preferences::Store.instance.persistence = false
Spree::Preferences::Store.instance.clear_cache
config = Rails.application.config.spree.preferences
configure_spree_preferences(&config_block) if block_given?
end
def configure_spree_preferences
config = Rails.application.config.spree.preferences
yield(config) if block_given?
end
end

View File

@@ -95,16 +95,29 @@ module WebHelper
end
end
def within_row(num, &block)
within("table.index tbody tr:nth-child(#{num})", &block)
end
def select2_select(value, options)
id = options[:from]
options[:from] = "#s2id_#{id}"
targetted_select2(value, options)
end
def targetted_select2(value, options)
# find select2 element and click it
find(options[:from]).find('a').click
select_select2_result(value)
end
def select_select2_result(value)
sleep(1)
page.execute_script(%Q{$("div.select2-result-label:contains('#{value}')").mouseup()})
end
# Support having different texts to search for and to click in the select2
# field.
#
# This overrides the method in Spree.
def targetted_select2_search(value, options)
page.execute_script %{$('#{options[:from]}').select2('open')}
page.execute_script "$('#{options[:dropdown_css]} input.select2-input').val('#{value}').trigger('keyup-change');"
@@ -124,25 +137,8 @@ module WebHelper
page.execute_script "jQuery('#{selector}').select2('close');"
end
def select2_search_async(value, options)
id = find_label_by_text(options[:from])
options[:from] = "#s2id_#{id}"
targetted_select2_search_async(value, options)
end
def targetted_select2_search_async(value, options)
page.execute_script %{$('#{options[:from]}').select2('open')}
page.execute_script "$('#{options[:dropdown_css]} input.select2-input').val('#{value}').trigger('keyup-change');"
select_select2_result_async(value)
end
def select_select2_result_async(value)
while page.has_selector? "div.select2-searching"
return if page.has_selector? "div.select2-no-results"
sleep 0.2
end
page.execute_script(%{$("div.select2-result-label:contains('#{value}')").mouseup()})
def set_select2_field(field, value)
page.execute_script %Q{$('#{field}').select2('val', '#{value}')}
end
def accept_js_alert