Provide blank values for all variant overrides

This commit is contained in:
Rohan Mitchell
2014-12-04 15:22:54 +11:00
parent d3e639aa03
commit abf58c0e02
4 changed files with 48 additions and 3 deletions

View File

@@ -22,6 +22,7 @@ angular.module("ofn.admin").controller "AdminOverrideVariantsCtrl", ($scope, Ind
$scope.addProducts = (products) ->
$scope.products = $scope.products.concat products
VariantOverrides.ensureDataFor hubs, products
$scope.selectHub = ->

View File

@@ -6,3 +6,14 @@ angular.module("ofn.admin").factory "VariantOverrides", (variantOverrides, Index
for vo in variantOverrides
@variantOverrides[vo.hub_id] ||= {}
@variantOverrides[vo.hub_id][vo.variant_id] = vo
ensureDataFor: (hubs, products) ->
for hub in hubs
@variantOverrides[hub.id] ||= {}
for product in products
for variant in product.variants
@variantOverrides[hub.id][variant.id] ||=
variant_id: variant.id
hub_id: hub.id
price: ''
count_on_hand: ''

View File

@@ -5,6 +5,7 @@ describe "OverrideVariantsCtrl", ->
producers = [{id: 2, name: 'Producer'}]
products = [{id: 1, name: 'Product'}]
hubPermissions = {}
VariantOverrides = null
variantOverrides = {}
beforeEach ->
@@ -15,19 +16,22 @@ describe "OverrideVariantsCtrl", ->
null
scope = {}
inject ($controller, Indexer, VariantOverrides) ->
ctrl = $controller 'AdminOverrideVariantsCtrl', {$scope: scope, Indexer: Indexer, hubs: hubs, producers: producers, products: products, hubPermissions: hubPermissions, VariantOverrides: VariantOverrides}
inject ($controller, Indexer, _VariantOverrides_) ->
VariantOverrides = _VariantOverrides_
ctrl = $controller 'AdminOverrideVariantsCtrl', {$scope: scope, Indexer: Indexer, hubs: hubs, producers: producers, products: products, hubPermissions: hubPermissions, VariantOverrides: _VariantOverrides_}
it "initialises the hub list and the chosen hub", ->
expect(scope.hubs).toEqual hubs
expect(scope.hub).toBeNull
expect(scope.hub).toBeNull()
it "adds products", ->
spyOn(VariantOverrides, "ensureDataFor")
expect(scope.products).toEqual []
scope.addProducts ['a', 'b']
expect(scope.products).toEqual ['a', 'b']
scope.addProducts ['c', 'd']
expect(scope.products).toEqual ['a', 'b', 'c', 'd']
expect(VariantOverrides.ensureDataFor).toHaveBeenCalled()
describe "selecting a hub", ->
it "sets the chosen hub", ->

View File

@@ -22,3 +22,32 @@ describe "VariantOverrides service", ->
200: {id: 2, hub_id: 10, variant_id: 200, price: 2, count_on_hand: 2}
20:
300: {id: 3, hub_id: 20, variant_id: 300, price: 3, count_on_hand: 3}
it "ensures blank data available for some products", ->
hubs = [{id: 10}, {id: 20}, {id: 30}]
products = [
{
id: 1
variants: [{id: 100}, {id: 200}, {id: 300}, {id: 400}, {id: 500}]
}
]
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: ''}
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: ''}
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: ''}