From f8d3467d46bbe7a148f5122fe7f488c242b96014 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Wed, 11 Sep 2024 01:59:43 +0500 Subject: [PATCH] 11200: add specs --- app/helpers/admin/products_helper.rb | 7 +- app/models/column_preference.rb | 2 +- .../_product_variant_row.html.haml | 2 +- spec/system/admin/products_v3/actions_spec.rb | 81 +++++++++++-------- spec/system/admin/products_v3/create_spec.rb | 1 + spec/system/admin/products_v3/index_spec.rb | 1 - spec/system/admin/products_v3/update_spec.rb | 3 +- 7 files changed, 59 insertions(+), 38 deletions(-) diff --git a/app/helpers/admin/products_helper.rb b/app/helpers/admin/products_helper.rb index d3b06806ba..9b404f634b 100644 --- a/app/helpers/admin/products_helper.rb +++ b/app/helpers/admin/products_helper.rb @@ -10,8 +10,11 @@ module Admin end end - def prepare_new_variant(product) - product.variants.build + def prepare_new_variant(product, producer_options) + # e.g producer_options = [['producer name', id]] + product.variants.build do |new_variant| + new_variant.supplier_id = producer_options.first.second if producer_options.one? + end end def unit_value_with_description(variant) diff --git a/app/models/column_preference.rb b/app/models/column_preference.rb index 093d2d5f30..73c59f22a5 100644 --- a/app/models/column_preference.rb +++ b/app/models/column_preference.rb @@ -37,7 +37,7 @@ class ColumnPreference < ApplicationRecord end def self.valid_columns_for(action_name) - __send__("#{action_name}_columns").keys.map(&:to_s) + get_default_preferences(action_name, Spree::User.new).keys.map(&:to_s) end def self.known_actions diff --git a/app/views/admin/products_v3/_product_variant_row.html.haml b/app/views/admin/products_v3/_product_variant_row.html.haml index b41e7c56b5..12b6702cba 100644 --- a/app/views/admin/products_v3/_product_variant_row.html.haml +++ b/app/views/admin/products_v3/_product_variant_row.html.haml @@ -11,7 +11,7 @@ %tr.condensed{ id: dom_id(variant), 'data-controller': "variant", 'class': "nested-form-wrapper", 'data-new-record': variant.new_record? ? "true" : false } = render partial: 'variant_row', locals: { variant:, f: variant_form, category_options:, tax_category_options:, producer_options: } - = form.fields_for("products][#{product_index}][variants_attributes][NEW_RECORD", prepare_new_variant(product)) do |new_variant_form| + = form.fields_for("products][#{product_index}][variants_attributes][NEW_RECORD", prepare_new_variant(product, producer_options)) do |new_variant_form| %template{ 'data-nested-form-target': "template" } %tr.condensed{ 'data-controller': "variant", 'class': "nested-form-wrapper", 'data-new-record': "true" } = render partial: 'variant_row', locals: { variant: new_variant_form.object, f: new_variant_form, category_options:, tax_category_options:, producer_options: } diff --git a/spec/system/admin/products_v3/actions_spec.rb b/spec/system/admin/products_v3/actions_spec.rb index e864218c1d..41a8f0fc6f 100644 --- a/spec/system/admin/products_v3/actions_spec.rb +++ b/spec/system/admin/products_v3/actions_spec.rb @@ -34,42 +34,59 @@ RSpec.describe 'As an enterprise user, I can manage my products' do describe "column selector" do let!(:product) { create(:simple_product) } - before do - visit admin_products_url + context "with one producer only" do + before do + visit admin_products_url + end + + it "hides column and remembers saved preference" do + # Name shows by default + expect(page).to have_checked_field "Name" + expect(page).to have_selector "th", text: "Name" + expect_other_columns_visible + + # Producer is hidden by if only one producer is present + expect(page).not_to have_checked_field "Producer" + expect(page).not_to have_selector "th", text: "Producer" + + # Name is hidden + ofn_drop_down("Columns").click + within ofn_drop_down("Columns") do + uncheck "Name" + end + expect(page).not_to have_selector "th", text: "Name" + expect_other_columns_visible + + # Preference saved + click_on "Save as default" + expect(page).to have_content "Column preferences saved" + refresh + + # Preference remembered + ofn_drop_down("Columns").click + within ofn_drop_down("Columns") do + expect(page).to have_unchecked_field "Name" + end + expect(page).not_to have_selector "th", text: "Name" + expect_other_columns_visible + end + + def expect_other_columns_visible + expect(page).to have_selector "th", text: "Price" + expect(page).to have_selector "th", text: "On Hand" + end end - it "hides column and remembers saved preference" do - # Name shows by default - expect(page).to have_checked_field "Name" - expect(page).to have_selector "th", text: "Name" - expect_other_columns_visible + context "with multiple producers" do + let!(:producer2) { create(:supplier_enterprise, owner: user) } - # Name is hidden - ofn_drop_down("Columns").click - within ofn_drop_down("Columns") do - uncheck "Name" + before { visit admin_products_url } + + it "has selected producer column by default" do + # Producer shows by default + expect(page).to have_checked_field "Producer" + expect(page).to have_selector "th", text: "Producer" end - expect(page).not_to have_selector "th", text: "Name" - expect_other_columns_visible - - # Preference saved - click_on "Save as default" - expect(page).to have_content "Column preferences saved" - refresh - - # Preference remembered - ofn_drop_down("Columns").click - within ofn_drop_down("Columns") do - expect(page).to have_unchecked_field "Name" - end - expect(page).not_to have_selector "th", text: "Name" - expect_other_columns_visible - end - - def expect_other_columns_visible - expect(page).to have_selector "th", text: "Producer" - expect(page).to have_selector "th", text: "Price" - expect(page).to have_selector "th", text: "On Hand" end end diff --git a/spec/system/admin/products_v3/create_spec.rb b/spec/system/admin/products_v3/create_spec.rb index ce3b1502cf..fbde227269 100644 --- a/spec/system/admin/products_v3/create_spec.rb +++ b/spec/system/admin/products_v3/create_spec.rb @@ -7,6 +7,7 @@ RSpec.describe 'As an enterprise user, I can manage my products' do include WebHelper let!(:supplier) { create(:supplier_enterprise) } + let!(:supplier2) { create(:supplier_enterprise) } let!(:taxon) { create(:taxon) } describe "creating a new product" do diff --git a/spec/system/admin/products_v3/index_spec.rb b/spec/system/admin/products_v3/index_spec.rb index 3d5f30c03f..15990c3460 100644 --- a/spec/system/admin/products_v3/index_spec.rb +++ b/spec/system/admin/products_v3/index_spec.rb @@ -36,7 +36,6 @@ RSpec.describe 'As an enterprise user, I can manage my products' do expect(page).to have_selector "th", text: "Unit" expect(page).to have_selector "th", text: "Price" expect(page).to have_selector "th", text: "On Hand" - expect(page).to have_selector "th", text: "Producer" expect(page).to have_selector "th", text: "Category" expect(page).to have_selector "th", text: "Tax Category" expect(page).to have_selector "th", text: "Inherits Properties?" diff --git a/spec/system/admin/products_v3/update_spec.rb b/spec/system/admin/products_v3/update_spec.rb index fec4236177..57245ecb6b 100644 --- a/spec/system/admin/products_v3/update_spec.rb +++ b/spec/system/admin/products_v3/update_spec.rb @@ -9,7 +9,8 @@ RSpec.describe 'As an enterprise user, I can update my products' do include FileHelper let(:producer) { create(:supplier_enterprise) } - let(:user) { create(:user, enterprises: [producer]) } + let(:producer2) { create(:supplier_enterprise) } + let(:user) { create(:user, enterprises: [producer, producer2]) } before do login_as user