From cc003c99d54ed1b0508d4bc6a0c46322bf81a8bc Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Wed, 28 Nov 2018 19:31:39 +0800 Subject: [PATCH] Ensure in UI compatible VO count and on demand --- .../variant_overrides_controller.js.coffee | 12 ++++ .../_products_variants.html.haml | 4 +- spec/features/admin/variant_overrides_spec.rb | 36 ++++++++++- ...ariant_overrides_controller_spec.js.coffee | 63 +++++++++++++++++++ 4 files changed, 112 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/admin/variant_overrides/controllers/variant_overrides_controller.js.coffee b/app/assets/javascripts/admin/variant_overrides/controllers/variant_overrides_controller.js.coffee index 4598ad817d..c0c6144f49 100644 --- a/app/assets/javascripts/admin/variant_overrides/controllers/variant_overrides_controller.js.coffee +++ b/app/assets/javascripts/admin/variant_overrides/controllers/variant_overrides_controller.js.coffee @@ -109,3 +109,15 @@ angular.module("admin.variantOverrides").controller "AdminVariantOverridesCtrl", StatusMessage.display 'success', t('js.variant_overrides.stock_reset') .error (data, status) -> $timeout -> StatusMessage.display 'failure', $scope.updateError(data, status) + + $scope.selectLimitedStockIfCountOnHandSet = (hubId, variantId) -> + variantOverride = $scope.variantOverrides[hubId][variantId] + if variantOverride.count_on_hand? && variantOverride.count_on_hand != '' && variantOverride.on_demand != false + variantOverride.on_demand = false + DirtyVariantOverrides.set hubId, variantId, variantOverride.id, 'on_demand', false + + $scope.clearCountOnHandUnlessLimitedStock = (hubId, variantId) -> + variantOverride = $scope.variantOverrides[hubId][variantId] + unless variantOverride.on_demand == false && variantOverride.count_on_hand? + variantOverride.count_on_hand = null + DirtyVariantOverrides.set hubId, variantId, variantOverride.id, 'count_on_hand', null diff --git a/app/views/admin/variant_overrides/_products_variants.html.haml b/app/views/admin/variant_overrides/_products_variants.html.haml index 5a3f3b2b39..38614ad2ba 100644 --- a/app/views/admin/variant_overrides/_products_variants.html.haml +++ b/app/views/admin/variant_overrides/_products_variants.html.haml @@ -8,9 +8,9 @@ %td.price{ ng: { show: 'columns.price.visible' } } %input{name: 'variant-overrides-{{ variant.id }}-price', type: 'text', ng: {model: 'variantOverrides[hub_id][variant.id].price'}, placeholder: '{{ variant.price }}', 'ofn-track-variant-override' => 'price'} %td.on_hand{ ng: { show: 'columns.on_hand.visible' } } - %input{name: 'variant-overrides-{{ variant.id }}-count_on_hand', type: 'text', ng: {model: 'variantOverrides[hub_id][variant.id].count_on_hand'}, placeholder: '{{ variant.on_hand }}', 'ofn-track-variant-override' => 'count_on_hand'} + %input{name: 'variant-overrides-{{ variant.id }}-count_on_hand', type: 'text', ng: { model: 'variantOverrides[hub_id][variant.id].count_on_hand', change: 'selectLimitedStockIfCountOnHandSet(hub_id, variant.id)' }, placeholder: '{{ variant.on_hand }}', 'ofn-track-variant-override' => 'count_on_hand'} %td.on_demand{ ng: { show: 'columns.on_demand.visible' } } - %select{ name: 'variant-overrides-{{ variant.id }}-on_demand', ng: { model: 'variantOverrides[hub_id][variant.id].on_demand', options: 'option.value as option.description for option in onDemandOptions' }, 'ofn-track-variant-override' => 'on_demand' } + %select{ name: 'variant-overrides-{{ variant.id }}-on_demand', ng: { model: 'variantOverrides[hub_id][variant.id].on_demand', change: 'clearCountOnHandUnlessLimitedStock(hub_id, variant.id)', options: 'option.value as option.description for option in onDemandOptions' }, 'ofn-track-variant-override' => 'on_demand' } %option{ value: '' }= t(".on_demand.use_producer_settings") %td.reset{ ng: { show: 'columns.reset.visible' } } %input{name: 'variant-overrides-{{ variant.id }}-resettable', type: 'checkbox', ng: {model: 'variantOverrides[hub_id][variant.id].resettable'}, placeholder: '{{ variant.resettable }}', 'ofn-track-variant-override' => 'resettable'} diff --git a/spec/features/admin/variant_overrides_spec.rb b/spec/features/admin/variant_overrides_spec.rb index 07273dfc19..9be249de4d 100644 --- a/spec/features/admin/variant_overrides_spec.rb +++ b/spec/features/admin/variant_overrides_spec.rb @@ -339,9 +339,43 @@ feature %q{ first("div#bulk-actions-dropdown div.menu div.menu_item", text: "Reset Stock Levels To Defaults").click page.should have_content "Save changes first" end + + describe "ensuring that on demand and count on hand settings are compatible" do + it "changes to limited stock when count on hand is set" do + # It sets on_demand to false when count_on_hand is filled. + fill_in "variant-overrides-#{variant.id}-count_on_hand", with: "200" + expect(page).to have_select "variant-overrides-#{variant.id}-on_demand", selected: I18n.t("js.variant_overrides.on_demand.no") + + # It saves the changes. + click_button I18n.t("save_changes") + expect(page).to have_content I18n.t("js.changes_saved") + + vo.reload + expect(vo.count_on_hand).to eq(200) + expect(vo.on_demand).to eq(false) + end + + it "clears count on hand when not limited stock" do + # It clears count_on_hand when selecting true on_demand. + fill_in "variant-overrides-#{variant.id}-count_on_hand", with: "200" + select I18n.t("js.variant_overrides.on_demand.yes"), from: "variant-overrides-#{variant.id}-on_demand" + expect(page).to have_input "variant-overrides-#{variant.id}-count_on_hand", with: "" + + # It clears count_on_hand when selecting nil on_demand. + fill_in "variant-overrides-#{variant.id}-count_on_hand", with: "200" + select I18n.t("admin.variant_overrides.products_variants.on_demand.use_producer_settings"), from: "variant-overrides-#{variant.id}-on_demand" + expect(page).to have_input "variant-overrides-#{variant.id}-count_on_hand", with: "" + + # It saves the changes. + click_button I18n.t("save_changes") + expect(page).to have_content I18n.t("js.changes_saved") + vo.reload + expect(vo.count_on_hand).to be_nil + expect(vo.on_demand).to be_nil + end + end end end - end describe "when manually placing an order" do diff --git a/spec/javascripts/unit/admin/controllers/variant_overrides_controller_spec.js.coffee b/spec/javascripts/unit/admin/controllers/variant_overrides_controller_spec.js.coffee index 450041a417..1ac20b4318 100644 --- a/spec/javascripts/unit/admin/controllers/variant_overrides_controller_spec.js.coffee +++ b/spec/javascripts/unit/admin/controllers/variant_overrides_controller_spec.js.coffee @@ -87,3 +87,66 @@ describe "VariantOverridesCtrl", -> $httpBackend.flush() expect(VariantOverrides.updateData).toHaveBeenCalledWith variant_overrides_mock expect(StatusMessage.display).toHaveBeenCalledWith 'success', 'Stocks reset to defaults.' + + describe "ensuring that on demand and count on hand settings are compatible", -> + describe "changing to limited stock when count on hand is set", -> + beforeEach -> + scope.variantOverrides = {123: {2: {id: 5, on_demand: true}}} + + describe "when count on hand is set", -> + beforeEach -> + scope.variantOverrides[123][2].count_on_hand = 1 + + it "changes to limited stock and registers dirty attribute", -> + scope.selectLimitedStockIfCountOnHandSet(123, 2) + expect(scope.variantOverrides[123][2].on_demand).toBe(false) + dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2] + expect(dirtyVariantOverride.on_demand).toBe(false) + + describe "when count on hand is null", -> + beforeEach -> + scope.variantOverrides[123][2].count_on_hand = null + + it "does not change to limited stock", -> + scope.selectLimitedStockIfCountOnHandSet(123, 2) + expect(scope.variantOverrides[123][2].on_demand).toBe(true) + + describe "when count on hand is blank string", -> + beforeEach -> + scope.variantOverrides[123][2].count_on_hand = '' + + it "does not change to limited stock", -> + scope.selectLimitedStockIfCountOnHandSet(123, 2) + expect(scope.variantOverrides[123][2].on_demand).toBe(true) + + describe "clearing count on hand when not limited stock", -> + beforeEach -> + scope.variantOverrides = {123: {2: {id: 5, count_on_hand: 1}}} + + describe "when on demand is false", -> + beforeEach -> + scope.variantOverrides[123][2].on_demand = false + + it "does not clear count on hand", -> + scope.clearCountOnHandUnlessLimitedStock(123, 2) + expect(scope.variantOverrides[123][2].count_on_hand).toEqual(1) + + describe "when on demand is true", -> + beforeEach -> + scope.variantOverrides[123][2].on_demand = true + + it "clears count on hand and registers dirty attribute", -> + scope.clearCountOnHandUnlessLimitedStock(123, 2) + expect(scope.variantOverrides[123][2].count_on_hand).toBeNull() + dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2] + expect(dirtyVariantOverride.count_on_hand).toBeNull() + + describe "when on demand is null", -> + beforeEach -> + scope.variantOverrides[123][2].on_demand = null + + it "clears count on hand and registers dirty attribute", -> + scope.clearCountOnHandUnlessLimitedStock(123, 2) + expect(scope.variantOverrides[123][2].count_on_hand).toBeNull() + dirtyVariantOverride = DirtyVariantOverrides.dirtyVariantOverrides[123][2] + expect(dirtyVariantOverride.count_on_hand).toBeNull()