From 34c603a9c335edaad3e90f7df03a9d3cef0b678b Mon Sep 17 00:00:00 2001 From: Steve Pettitt Date: Tue, 18 Aug 2015 21:35:00 +0100 Subject: [PATCH] Added angular service tests for variant overrides stock reset --- .../variant_overrides_controller.js.coffee | 6 +- .../services/variant_overrides.js.coffee | 8 +- .../admin/variant_overrides_controller.rb | 6 +- app/models/variant_override.rb | 18 +++-- .../api/admin/variant_override_serializer.rb | 2 +- .../_products_variants.html.haml | 4 +- .../services/variant_overrides_spec.js.coffee | 74 ++++++++++++------- 7 files changed, 76 insertions(+), 42 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 8aed964949..c2fab4e7c7 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 @@ -59,6 +59,7 @@ angular.module("admin.variantOverrides").controller "AdminVariantOverridesCtrl", StatusMessage.display 'progress', 'Saving...' DirtyVariantOverrides.save() .success (updatedVos) -> + console.log DirtyVariantOverrides.all() DirtyVariantOverrides.clear() VariantOverrides.updateIds updatedVos StatusMessage.display 'success', 'Changes saved.' @@ -82,4 +83,7 @@ angular.module("admin.variantOverrides").controller "AdminVariantOverridesCtrl", "Oh no! I was unable to save your changes." $scope.resetStock = -> - variantOverrides.resetStock() + VariantOverrides.resetStock() + .success (updatedVos) -> + VariantOverrides.updateData updatedVos + $timeout -> StatusMessage.display 'success', 'Stocks reset to defaults.' diff --git a/app/assets/javascripts/admin/variant_overrides/services/variant_overrides.js.coffee b/app/assets/javascripts/admin/variant_overrides/services/variant_overrides.js.coffee index a3573a3cde..559305b1a3 100644 --- a/app/assets/javascripts/admin/variant_overrides/services/variant_overrides.js.coffee +++ b/app/assets/javascripts/admin/variant_overrides/services/variant_overrides.js.coffee @@ -1,4 +1,4 @@ -angular.module("admin.variantOverrides").factory "VariantOverrides", (variantOverrides) -> +angular.module("admin.variantOverrides").factory "VariantOverrides", (variantOverrides, $http) -> new class VariantOverrides variantOverrides: {} @@ -30,4 +30,8 @@ angular.module("admin.variantOverrides").factory "VariantOverrides", (variantOve method: "POST" url: "/admin/variant_overrides/bulk_reset" data: - variant_overrides: @all() + variant_overrides: variantOverrides + + updateData: (updatedVos) -> + for vo in updatedVos + @variantOverrides[vo.hub_id][vo.variant_id] = vo diff --git a/app/controllers/admin/variant_overrides_controller.rb b/app/controllers/admin/variant_overrides_controller.rb index f45661f048..66e8d97baf 100644 --- a/app/controllers/admin/variant_overrides_controller.rb +++ b/app/controllers/admin/variant_overrides_controller.rb @@ -37,12 +37,10 @@ module Admin # Ensure we're authorised to update all variant overrides vo_set.collection.each { |vo| authorize! :update, vo } - vo.set.collection.each { |vo| vo.reset_stock! } + vo_set.collection.map! { |vo| vo = vo.reset_stock! } + render json: vo_set.collection, each_serializer: Api::Admin::VariantOverrideSerializer if vo_set.errors.present? render json: { errors: vo_set.errors }, status: 400 - else - # Return saved VOs with IDs - render json: vo_set.collection, each_serializer: Api::Admin::VariantOverrideSerializer end end diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index f983f9b818..c7d111fefc 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -57,12 +57,20 @@ class VariantOverride < ActiveRecord::Base def reset_stock! if default_stock? - update_attributes :count_on_hand => default_stock - else - # Could remove as not resetting where there is no default is intended behaviour - Bugsnag.notify RuntimeError.new "Attempting to reset stock for a VariantOverride where a default level is not present" + self.attributes = { count_on_hand: default_stock } + self.save + end + self + end + + def self.reset_stock!(hub, variant) + vo = self.for(hub, variant) + + if vo.nil? + Bugsnag.notify RuntimeError.new "Attempting to reset stock level for a variant without a VariantOverride." + else + vo.reset_stock! end - end private diff --git a/app/serializers/api/admin/variant_override_serializer.rb b/app/serializers/api/admin/variant_override_serializer.rb index d4f584d9ea..54b4419fb7 100644 --- a/app/serializers/api/admin/variant_override_serializer.rb +++ b/app/serializers/api/admin/variant_override_serializer.rb @@ -1,3 +1,3 @@ class Api::Admin::VariantOverrideSerializer < ActiveModel::Serializer - attributes :id, :hub_id, :variant_id, :sku, :price, :count_on_hand, :on_demand + attributes :id, :hub_id, :variant_id, :sku, :price, :count_on_hand, :on_demand, :default_stock end diff --git a/app/views/admin/variant_overrides/_products_variants.html.haml b/app/views/admin/variant_overrides/_products_variants.html.haml index f14cebec93..510689b83d 100644 --- a/app/views/admin/variant_overrides/_products_variants.html.haml +++ b/app/views/admin/variant_overrides/_products_variants.html.haml @@ -12,7 +12,7 @@ %td.on_demand{ ng: { show: 'columns.on_demand.visible' } } %input.field{ :type => 'checkbox', name: 'variant-overrides-{{ variant.id }}-on_demand', ng: { model: 'variantOverrides[hub.id][variant.id].on_demand' }, 'ofn-track-variant-override' => 'on_demand' } %td - %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' => 'price'} + %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'} %td - %input{name: 'variant-overrides-{{ variant.id }}-default-stock', type: 'text', ng: {model: 'variantOverrides[hub.id][variant.id].default_stock'}, placeholder: '{{ variant.default_stock }}', 'ofn-track-variant-override' => 'price'} + %input{name: 'variant-overrides-{{ variant.id }}-default-stock', type: 'text', ng: {model: 'variantOverrides[hub.id][variant.id].default_stock'}, placeholder: '{{ variant.default_stock }}', 'ofn-track-variant-override' => 'default_stock'} diff --git a/spec/javascripts/unit/admin/services/variant_overrides_spec.js.coffee b/spec/javascripts/unit/admin/services/variant_overrides_spec.js.coffee index b379dc5915..5ffd71cd64 100644 --- a/spec/javascripts/unit/admin/services/variant_overrides_spec.js.coffee +++ b/spec/javascripts/unit/admin/services/variant_overrides_spec.js.coffee @@ -1,9 +1,9 @@ describe "VariantOverrides service", -> - VariantOverrides = null + VariantOverrides = $httpBackend = null variantOverrides = [ - {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1} - {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2} - {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3} + {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1, default_stock: ''} + {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2, default_stock: ''} + {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3, default_stock: ''} ] beforeEach -> @@ -12,16 +12,17 @@ describe "VariantOverrides service", -> $provide.value "variantOverrides", variantOverrides null - beforeEach inject (_VariantOverrides_) -> + beforeEach inject (_VariantOverrides_, _$httpBackend_) -> VariantOverrides = _VariantOverrides_ + $httpBackend = _$httpBackend_ it "indexes variant overrides by hub_id -> variant_id", -> expect(VariantOverrides.variantOverrides).toEqual 10: - 100: {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1} - 200: {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2} + 100: {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1, default_stock: ''} + 200: {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2, default_stock: ''} 20: - 300: {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3} + 300: {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3, default_stock: ''} it "ensures blank data available for some products", -> hubs = [{id: 10}, {id: 20}, {id: 30}] @@ -34,35 +35,54 @@ describe "VariantOverrides service", -> VariantOverrides.ensureDataFor hubs, products expect(VariantOverrides.variantOverrides).toEqual 10: - 100: {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1} - 200: {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2} - 300: { hub_id: 10, variant_id: 300, price: '', count_on_hand: ''} - 400: { hub_id: 10, variant_id: 400, price: '', count_on_hand: ''} - 500: { hub_id: 10, variant_id: 500, price: '', count_on_hand: ''} + 100: {id: 1, hub_id: 10, variant_id: 100, price: 1, count_on_hand: 1, default_stock: ''} + 200: {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2, default_stock: ''} + 300: { hub_id: 10, variant_id: 300, price: '', count_on_hand: '', default_stock: ''} + 400: { hub_id: 10, variant_id: 400, price: '', count_on_hand: '', default_stock: ''} + 500: { hub_id: 10, variant_id: 500, price: '', count_on_hand: '', default_stock: ''} 20: - 100: { hub_id: 20, variant_id: 100, price: '', count_on_hand: ''} - 200: { hub_id: 20, variant_id: 200, price: '', count_on_hand: ''} - 300: {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3} - 400: { hub_id: 20, variant_id: 400, price: '', count_on_hand: ''} - 500: { hub_id: 20, variant_id: 500, price: '', count_on_hand: ''} + 100: { hub_id: 20, variant_id: 100, price: '', count_on_hand: '', default_stock: ''} + 200: { hub_id: 20, variant_id: 200, price: '', count_on_hand: '', default_stock: ''} + 300: {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3, default_stock: ''} + 400: { hub_id: 20, variant_id: 400, price: '', count_on_hand: '', default_stock: ''} + 500: { hub_id: 20, variant_id: 500, price: '', count_on_hand: '', default_stock: ''} 30: - 100: { hub_id: 30, variant_id: 100, price: '', count_on_hand: ''} - 200: { hub_id: 30, variant_id: 200, price: '', count_on_hand: ''} - 300: { hub_id: 30, variant_id: 300, price: '', count_on_hand: ''} - 400: { hub_id: 30, variant_id: 400, price: '', count_on_hand: ''} - 500: { hub_id: 30, variant_id: 500, price: '', count_on_hand: ''} + 100: { hub_id: 30, variant_id: 100, price: '', count_on_hand: '', default_stock: ''} + 200: { hub_id: 30, variant_id: 200, price: '', count_on_hand: '', default_stock: ''} + 300: { hub_id: 30, variant_id: 300, price: '', count_on_hand: '', default_stock: ''} + 400: { hub_id: 30, variant_id: 400, price: '', count_on_hand: '', default_stock: ''} + 500: { hub_id: 30, variant_id: 500, price: '', count_on_hand: '', default_stock: ''} it "updates the IDs of variant overrides", -> VariantOverrides.variantOverrides[2] = {} - VariantOverrides.variantOverrides[2][3] = {hub_id: 2, variant_id: 3, price: "4.0", count_on_hand: 5} - VariantOverrides.variantOverrides[2][8] = {hub_id: 2, variant_id: 8, price: "9.0", count_on_hand: 10} + VariantOverrides.variantOverrides[2][3] = {hub_id: 2, variant_id: 3, price: "4.0", count_on_hand: 5, default_stock: ''} + VariantOverrides.variantOverrides[2][8] = {hub_id: 2, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: ''} updatedVos = [ - {id: 1, hub_id: 2, variant_id: 3, price: "4.0", count_on_hand: 5} - {id: 6, hub_id: 2, variant_id: 8, price: "9.0", count_on_hand: 10} + {id: 1, hub_id: 2, variant_id: 3, price: "4.0", count_on_hand: 5, default_stock: ''} + {id: 6, hub_id: 2, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: ''} ] VariantOverrides.updateIds updatedVos expect(VariantOverrides.variantOverrides[2][3].id).toEqual 1 expect(VariantOverrides.variantOverrides[2][8].id).toEqual 6 + + it "sends an HTTP request to reset stock", -> + $httpBackend.expectPOST("/admin/variant_overrides/bulk_reset", variant_overrides: variantOverrides).respond 200 + VariantOverrides.resetStock variantOverrides + $httpBackend.flush() + + it "updates the variant overrides on the page with new data", -> + VariantOverrides.variantOverrides[1] = + 3: {id: 1, hub_id: 1, variant_id: 3, price: "4.0", count_on_hand: 5, default_stock: 3} + 8: {id: 2, hub_id: 1, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: ''} + # Updated count on hand to 3 + updatedVos = [ + {id: 1, hub_id: 1, variant_id: 3, price: "4.0", count_on_hand: 3, default_stock: 3} + ] + + VariantOverrides.updateData(updatedVos) + expect(VariantOverrides.variantOverrides[1]).toEqual + 3: {id: 1, hub_id: 1, variant_id: 3, price: "4.0", count_on_hand: 3, default_stock: 3} + 8: {id: 2, hub_id: 1, variant_id: 8, price: "9.0", count_on_hand: 10, default_stock: ''}