mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-27 21:06:49 +00:00
Merge branch 'master' into 2-0-stable-Mar6
This commit is contained in:
967
spec/javascripts/unit/admin/bulk_product_update_spec.js.coffee
Normal file
967
spec/javascripts/unit/admin/bulk_product_update_spec.js.coffee
Normal file
@@ -0,0 +1,967 @@
|
||||
describe "filtering products for submission to database", ->
|
||||
it "accepts an object or an array and only returns an array", ->
|
||||
expect(filterSubmitProducts([])).toEqual []
|
||||
expect(filterSubmitProducts({})).toEqual []
|
||||
expect(filterSubmitProducts(1:
|
||||
id: 1
|
||||
name: "lala"
|
||||
)).toEqual [
|
||||
id: 1
|
||||
name: "lala"
|
||||
]
|
||||
expect(filterSubmitProducts([
|
||||
id: 1
|
||||
name: "lala"
|
||||
])).toEqual [
|
||||
id: 1
|
||||
name: "lala"
|
||||
]
|
||||
expect(filterSubmitProducts(1)).toEqual []
|
||||
expect(filterSubmitProducts("2")).toEqual []
|
||||
expect(filterSubmitProducts(null)).toEqual []
|
||||
|
||||
it "only returns products which have an id property", ->
|
||||
expect(filterSubmitProducts([
|
||||
{
|
||||
id: 1
|
||||
name: "p1"
|
||||
}
|
||||
{
|
||||
notanid: 2
|
||||
name: "p2"
|
||||
}
|
||||
])).toEqual [
|
||||
id: 1
|
||||
name: "p1"
|
||||
]
|
||||
|
||||
it "does not return a product object for products which have no propeties other than an id", ->
|
||||
expect(filterSubmitProducts([
|
||||
{
|
||||
id: 1
|
||||
someunwantedproperty: "something"
|
||||
}
|
||||
{
|
||||
id: 2
|
||||
name: "p2"
|
||||
}
|
||||
])).toEqual [
|
||||
id: 2
|
||||
name: "p2"
|
||||
]
|
||||
|
||||
it "does not return an on_hand count when a product has variants", ->
|
||||
testProduct =
|
||||
id: 1
|
||||
on_hand: 5
|
||||
variants: [
|
||||
id: 1
|
||||
on_hand: 5
|
||||
price: 12.0
|
||||
]
|
||||
|
||||
expect(filterSubmitProducts([testProduct])).toEqual [
|
||||
id: 1
|
||||
variants_attributes: [
|
||||
id: 1
|
||||
on_hand: 5
|
||||
price: 12.0
|
||||
]
|
||||
]
|
||||
|
||||
it "returns variants as variants_attributes", ->
|
||||
testProduct =
|
||||
id: 1
|
||||
variants: [
|
||||
id: 1
|
||||
on_hand: 5
|
||||
price: 12.0
|
||||
unit_value: 250
|
||||
unit_description: "(bottle)"
|
||||
]
|
||||
|
||||
expect(filterSubmitProducts([testProduct])).toEqual [
|
||||
id: 1
|
||||
variants_attributes: [
|
||||
id: 1
|
||||
on_hand: 5
|
||||
price: 12.0
|
||||
unit_value: 250
|
||||
unit_description: "(bottle)"
|
||||
]
|
||||
]
|
||||
|
||||
it "ignores variants without an id, and those for which deleted_at is not null", ->
|
||||
testProduct =
|
||||
id: 1
|
||||
variants: [
|
||||
{
|
||||
id: 1
|
||||
on_hand: 3
|
||||
price: 5.0
|
||||
}
|
||||
{
|
||||
on_hand: 1
|
||||
price: 15.0
|
||||
}
|
||||
{
|
||||
id: 2
|
||||
on_hand: 2
|
||||
deleted_at: new Date()
|
||||
price: 20.0
|
||||
}
|
||||
]
|
||||
|
||||
expect(filterSubmitProducts([testProduct])).toEqual [
|
||||
id: 1
|
||||
variants_attributes: [
|
||||
id: 1
|
||||
on_hand: 3
|
||||
price: 5.0
|
||||
]
|
||||
]
|
||||
|
||||
it "returns variants with a negative id without that id", ->
|
||||
testProduct =
|
||||
id: 1
|
||||
variants: [
|
||||
id: -1
|
||||
on_hand: 5
|
||||
price: 12.0
|
||||
unit_value: 250
|
||||
unit_description: "(bottle)"
|
||||
]
|
||||
|
||||
expect(filterSubmitProducts([testProduct])).toEqual [
|
||||
id: 1
|
||||
variants_attributes: [
|
||||
on_hand: 5
|
||||
price: 12.0
|
||||
unit_value: 250
|
||||
unit_description: "(bottle)"
|
||||
]
|
||||
]
|
||||
|
||||
it "does not return variants_attributes property if variants is an empty array", ->
|
||||
testProduct =
|
||||
id: 1
|
||||
price: 10
|
||||
variants: []
|
||||
|
||||
expect(filterSubmitProducts([testProduct])).toEqual [
|
||||
id: 1
|
||||
price: 10
|
||||
]
|
||||
|
||||
it "returns variant_unit_with_scale as variant_unit and variant_unit_scale", ->
|
||||
testProduct =
|
||||
id: 1
|
||||
variant_unit: 'weight'
|
||||
variant_unit_scale: 1
|
||||
variant_unit_with_scale: 'weight_1'
|
||||
|
||||
expect(filterSubmitProducts([testProduct])).toEqual [
|
||||
id: 1
|
||||
variant_unit: 'weight'
|
||||
variant_unit_scale: 1
|
||||
]
|
||||
|
||||
it "returns stock properties of a product if no variant is provided", ->
|
||||
available_on = new Date()
|
||||
|
||||
testProduct =
|
||||
id: 1
|
||||
name: "TestProduct"
|
||||
on_hand: 0
|
||||
on_demand: false
|
||||
|
||||
expect(filterSubmitProducts([testProduct])).toEqual [
|
||||
id: 1
|
||||
name: "TestProduct"
|
||||
on_hand: 0
|
||||
on_demand: false
|
||||
]
|
||||
|
||||
it "only returns the properties of products which ought to be updated", ->
|
||||
available_on = new Date()
|
||||
|
||||
testProduct =
|
||||
id: 1
|
||||
name: "TestProduct"
|
||||
description: ""
|
||||
available_on: available_on
|
||||
deleted_at: null
|
||||
permalink: null
|
||||
meta_description: null
|
||||
meta_keywords: null
|
||||
tax_category_id: null
|
||||
shipping_category_id: null
|
||||
created_at: null
|
||||
updated_at: null
|
||||
on_hand: 0
|
||||
on_demand: false
|
||||
producer_id: 5
|
||||
group_buy: null
|
||||
group_buy_unit_size: null
|
||||
master:
|
||||
id: 2
|
||||
unit_value: 250
|
||||
unit_description: "foo"
|
||||
variants: [
|
||||
id: 1
|
||||
on_hand: 2
|
||||
price: 10.0
|
||||
unit_value: 250
|
||||
unit_description: "(bottle)"
|
||||
display_as: "bottle"
|
||||
display_name: "nothing"
|
||||
]
|
||||
variant_unit: 'volume'
|
||||
variant_unit_scale: 1
|
||||
variant_unit_name: 'loaf'
|
||||
variant_unit_with_scale: 'volume_1'
|
||||
|
||||
expect(filterSubmitProducts([testProduct])).toEqual [
|
||||
id: 1
|
||||
name: "TestProduct"
|
||||
supplier_id: 5
|
||||
variant_unit: 'volume'
|
||||
variant_unit_scale: 1
|
||||
variant_unit_name: 'loaf'
|
||||
available_on: available_on
|
||||
tax_category_id: null
|
||||
master_attributes:
|
||||
id: 2
|
||||
unit_value: 250
|
||||
unit_description: "foo"
|
||||
variants_attributes: [
|
||||
id: 1
|
||||
on_hand: 2
|
||||
price: 10.0
|
||||
unit_value: 250
|
||||
unit_description: "(bottle)"
|
||||
display_as: "bottle"
|
||||
display_name: "nothing"
|
||||
]
|
||||
]
|
||||
|
||||
describe "AdminProductEditCtrl", ->
|
||||
$ctrl = $scope = $timeout = $httpBackend = BulkProducts = DirtyProducts = DisplayProperties = null
|
||||
|
||||
beforeEach ->
|
||||
module "ofn.admin"
|
||||
module ($provide)->
|
||||
$provide.value "producers", []
|
||||
$provide.value "taxons", []
|
||||
$provide.value "tax_categories", []
|
||||
$provide.value 'SpreeApiKey', 'API_KEY'
|
||||
$provide.value 'columns', []
|
||||
null
|
||||
|
||||
beforeEach inject((_$controller_, _$timeout_, $rootScope, _$httpBackend_, _BulkProducts_, _DirtyProducts_, _DisplayProperties_) ->
|
||||
$scope = $rootScope.$new()
|
||||
$ctrl = _$controller_
|
||||
$timeout = _$timeout_
|
||||
$httpBackend = _$httpBackend_
|
||||
BulkProducts = _BulkProducts_
|
||||
DirtyProducts = _DirtyProducts_
|
||||
DisplayProperties = _DisplayProperties_
|
||||
|
||||
$ctrl "AdminProductEditCtrl", {$scope: $scope, $timeout: $timeout}
|
||||
)
|
||||
|
||||
describe "loading data upon initialisation", ->
|
||||
it "gets a list of producers and then resets products with a list of data", ->
|
||||
$httpBackend.expectGET("/api/users/authorise_api?token=API_KEY").respond success: "Use of API Authorised"
|
||||
spyOn($scope, "fetchProducts").and.returnValue "nothing"
|
||||
$scope.initialise()
|
||||
$httpBackend.flush()
|
||||
expect($scope.fetchProducts.calls.count()).toBe 1
|
||||
expect($scope.spree_api_key_ok).toEqual true
|
||||
|
||||
|
||||
describe "fetching products", ->
|
||||
$q = null
|
||||
deferred = null
|
||||
|
||||
beforeEach inject((_$q_) ->
|
||||
$q = _$q_
|
||||
)
|
||||
|
||||
beforeEach ->
|
||||
deferred = $q.defer()
|
||||
deferred.resolve()
|
||||
spyOn $scope, "resetProducts"
|
||||
spyOn(BulkProducts, "fetch").and.returnValue deferred.promise
|
||||
|
||||
it "calls resetProducts after data has been received", ->
|
||||
$scope.fetchProducts()
|
||||
$scope.$digest()
|
||||
expect($scope.resetProducts).toHaveBeenCalled()
|
||||
|
||||
it "sets the loading property to true before fetching products and unsets it when loading is complete", ->
|
||||
$scope.fetchProducts()
|
||||
expect($scope.loading).toEqual true
|
||||
$scope.$digest()
|
||||
expect($scope.loading).toEqual false
|
||||
|
||||
|
||||
describe "resetting products", ->
|
||||
beforeEach ->
|
||||
spyOn DirtyProducts, "clear"
|
||||
$scope.products = {}
|
||||
$scope.resetProducts [
|
||||
{
|
||||
id: 1
|
||||
name: "P1"
|
||||
}
|
||||
{
|
||||
id: 3
|
||||
name: "P2"
|
||||
}
|
||||
]
|
||||
|
||||
it "resets dirtyProducts", ->
|
||||
expect(DirtyProducts.clear).toHaveBeenCalled()
|
||||
|
||||
|
||||
describe "updating the product on hand count", ->
|
||||
it "updates when product is not available on demand", ->
|
||||
spyOn($scope, "onHand").and.returnValue 123
|
||||
product = {on_demand: false}
|
||||
$scope.updateOnHand(product)
|
||||
expect(product.on_hand).toEqual 123
|
||||
|
||||
it "updates when product's variants are not available on demand", ->
|
||||
spyOn($scope, "onHand").and.returnValue 123
|
||||
product = {on_demand: false, variants: [{on_demand: false}]}
|
||||
$scope.updateOnHand(product)
|
||||
expect(product.on_hand).toEqual 123
|
||||
|
||||
it "does nothing when the product is available on demand", ->
|
||||
product = {on_demand: true}
|
||||
$scope.updateOnHand(product)
|
||||
expect(product.on_hand).toBeUndefined()
|
||||
|
||||
it "does nothing when one of the variants is available on demand", ->
|
||||
product =
|
||||
on_demand: false
|
||||
variants: [
|
||||
{on_demand: false, on_hand: 10}
|
||||
{on_demand: true, on_hand: Infinity}
|
||||
]
|
||||
$scope.updateOnHand(product)
|
||||
expect(product.on_hand).toBeUndefined()
|
||||
|
||||
|
||||
describe "getting on_hand counts when products have variants", ->
|
||||
p1 = undefined
|
||||
p2 = undefined
|
||||
p3 = undefined
|
||||
beforeEach ->
|
||||
p1 = variants:
|
||||
1:
|
||||
id: 1
|
||||
on_hand: 1
|
||||
|
||||
2:
|
||||
id: 2
|
||||
on_hand: 2
|
||||
|
||||
3:
|
||||
id: 3
|
||||
on_hand: 3
|
||||
|
||||
p2 = variants:
|
||||
4:
|
||||
id: 4
|
||||
not_on_hand: 1
|
||||
|
||||
5:
|
||||
id: 5
|
||||
on_hand: 2
|
||||
|
||||
6:
|
||||
id: 6
|
||||
on_hand: 3
|
||||
|
||||
p3 =
|
||||
not_variants:
|
||||
7:
|
||||
id: 7
|
||||
on_hand: 1
|
||||
|
||||
8:
|
||||
id: 8
|
||||
on_hand: 2
|
||||
|
||||
variants:
|
||||
9:
|
||||
id: 9
|
||||
on_hand: 3
|
||||
|
||||
it "sums variant on_hand properties", ->
|
||||
expect($scope.onHand(p1)).toEqual 6
|
||||
|
||||
it "ignores items in variants without an on_hand property (adds 0)", ->
|
||||
expect($scope.onHand(p2)).toEqual 5
|
||||
|
||||
it "ignores on_hand properties of objects in arrays which are not named 'variants' (adds 0)", ->
|
||||
expect($scope.onHand(p3)).toEqual 3
|
||||
|
||||
it "returns 'error' if not given an object with a variants property that is an object", ->
|
||||
expect($scope.onHand([])).toEqual "error"
|
||||
expect($scope.onHand(not_variants: [])).toEqual "error"
|
||||
|
||||
|
||||
describe "determining whether a product has variants that are available on demand", ->
|
||||
it "returns true when at least one variant does", ->
|
||||
product =
|
||||
variants: [
|
||||
{on_demand: false}
|
||||
{on_demand: true}
|
||||
]
|
||||
expect($scope.hasOnDemandVariants(product)).toBe(true)
|
||||
|
||||
it "returns false otherwise", ->
|
||||
product =
|
||||
variants: [
|
||||
{on_demand: false}
|
||||
{on_demand: false}
|
||||
]
|
||||
expect($scope.hasOnDemandVariants(product)).toBe(false)
|
||||
|
||||
|
||||
describe "determining whether a product has variants", ->
|
||||
it "returns true when it does", ->
|
||||
product =
|
||||
variants: [{id: 1}, {id: 2}]
|
||||
expect($scope.hasVariants(product)).toBe(true)
|
||||
|
||||
it "returns false when it does not", ->
|
||||
product =
|
||||
variants: []
|
||||
expect($scope.hasVariants(product)).toBe(false)
|
||||
|
||||
|
||||
describe "determining whether a product has a unit", ->
|
||||
it "returns true when it does", ->
|
||||
product =
|
||||
variant_unit_with_scale: 'weight_1000'
|
||||
expect($scope.hasUnit(product)).toBe(true)
|
||||
|
||||
it "returns false when its unit is undefined", ->
|
||||
product = {}
|
||||
expect($scope.hasUnit(product)).toBe(false)
|
||||
|
||||
|
||||
describe "determining whether a variant has been saved", ->
|
||||
it "returns true when it has a positive id", ->
|
||||
variant = {id: 1}
|
||||
expect($scope.variantSaved(variant)).toBe(true)
|
||||
|
||||
it "returns false when it has no id", ->
|
||||
variant = {}
|
||||
expect($scope.variantSaved(variant)).toBe(false)
|
||||
|
||||
it "returns false when it has a negative id", ->
|
||||
variant = {id: -1}
|
||||
expect($scope.variantSaved(variant)).toBe(false)
|
||||
|
||||
|
||||
describe "submitting products to be updated", ->
|
||||
describe "packing products", ->
|
||||
it "extracts variant_unit_with_scale into variant_unit and variant_unit_scale", ->
|
||||
testProduct =
|
||||
id: 1
|
||||
variant_unit: 'weight'
|
||||
variant_unit_scale: 1
|
||||
variant_unit_with_scale: 'volume_1000'
|
||||
|
||||
$scope.packProduct(testProduct)
|
||||
|
||||
expect(testProduct).toEqual
|
||||
id: 1
|
||||
variant_unit: 'volume'
|
||||
variant_unit_scale: 1000
|
||||
variant_unit_with_scale: 'volume_1000'
|
||||
|
||||
it "extracts a null value into null variant_unit and variant_unit_scale", ->
|
||||
testProduct =
|
||||
id: 1
|
||||
variant_unit: 'weight'
|
||||
variant_unit_scale: 1
|
||||
variant_unit_with_scale: null
|
||||
|
||||
$scope.packProduct(testProduct)
|
||||
|
||||
expect(testProduct).toEqual
|
||||
id: 1
|
||||
variant_unit: null
|
||||
variant_unit_scale: null
|
||||
variant_unit_with_scale: null
|
||||
|
||||
it "extracts when variant_unit_with_scale is 'items'", ->
|
||||
testProduct =
|
||||
id: 1
|
||||
variant_unit: 'weight'
|
||||
variant_unit_scale: 1
|
||||
variant_unit_with_scale: 'items'
|
||||
|
||||
$scope.packProduct(testProduct)
|
||||
|
||||
expect(testProduct).toEqual
|
||||
id: 1
|
||||
variant_unit: 'items'
|
||||
variant_unit_scale: null
|
||||
variant_unit_with_scale: 'items'
|
||||
|
||||
it "packs the master variant", ->
|
||||
spyOn $scope, "packVariant"
|
||||
testVariant = {id: 1}
|
||||
testProduct =
|
||||
id: 1
|
||||
master: testVariant
|
||||
|
||||
$scope.packProduct(testProduct)
|
||||
|
||||
expect($scope.packVariant).toHaveBeenCalledWith(testProduct, testVariant)
|
||||
|
||||
it "packs each variant", ->
|
||||
spyOn $scope, "packVariant"
|
||||
testVariant = {id: 1}
|
||||
testProduct =
|
||||
id: 1
|
||||
variants: {1: testVariant}
|
||||
|
||||
$scope.packProduct(testProduct)
|
||||
|
||||
expect($scope.packVariant).toHaveBeenCalledWith(testProduct, testVariant)
|
||||
|
||||
describe "packing variants", ->
|
||||
testProduct = {id: 123}
|
||||
|
||||
beforeEach ->
|
||||
BulkProducts.products = [testProduct]
|
||||
|
||||
it "extracts unit_value and unit_description from unit_value_with_description", ->
|
||||
testProduct = {id: 123, variant_unit_scale: 1.0}
|
||||
testVariant = {unit_value_with_description: "250.5 (bottle)"}
|
||||
$scope.products = [testProduct]
|
||||
$scope.packVariant(testProduct, testVariant)
|
||||
expect(testVariant).toEqual
|
||||
unit_value: 250.5
|
||||
unit_description: "(bottle)"
|
||||
unit_value_with_description: "250.5 (bottle)"
|
||||
|
||||
it "extracts into unit_value when only a number is provided", ->
|
||||
testProduct = {id: 123, variant_unit_scale: 1.0}
|
||||
testVariant = {unit_value_with_description: "250.5"}
|
||||
$scope.packVariant(testProduct, testVariant)
|
||||
expect(testVariant).toEqual
|
||||
unit_value: 250.5
|
||||
unit_description: ''
|
||||
unit_value_with_description: "250.5"
|
||||
|
||||
it "extracts into unit_description when only a string is provided", ->
|
||||
testVariant = {unit_value_with_description: "Medium"}
|
||||
$scope.packVariant(testProduct, testVariant)
|
||||
expect(testVariant).toEqual
|
||||
unit_value: null
|
||||
unit_description: 'Medium'
|
||||
unit_value_with_description: "Medium"
|
||||
|
||||
it "extracts into unit_description when a string starting with a number is provided", ->
|
||||
testVariant = {unit_value_with_description: "1kg"}
|
||||
$scope.packVariant(testProduct, testVariant)
|
||||
expect(testVariant).toEqual
|
||||
unit_value: null
|
||||
unit_description: '1kg'
|
||||
unit_value_with_description: "1kg"
|
||||
|
||||
it "sets blank values when no value provided", ->
|
||||
testVariant = {unit_value_with_description: ""}
|
||||
$scope.packVariant(testProduct, testVariant)
|
||||
expect(testVariant).toEqual
|
||||
unit_value: null
|
||||
unit_description: ''
|
||||
unit_value_with_description: ""
|
||||
|
||||
it "sets nothing when the field is undefined", ->
|
||||
testVariant = {}
|
||||
$scope.packVariant(testProduct, testVariant)
|
||||
expect(testVariant).toEqual {}
|
||||
|
||||
it "sets zero when the field is zero", ->
|
||||
testProduct = {id: 123, variant_unit_scale: 1.0}
|
||||
testVariant = {unit_value_with_description: "0"}
|
||||
$scope.packVariant(testProduct, testVariant)
|
||||
expect(testVariant).toEqual
|
||||
unit_value: 0
|
||||
unit_description: ''
|
||||
unit_value_with_description: "0"
|
||||
|
||||
it "converts value from chosen unit to base unit", ->
|
||||
testProduct = {id: 123, variant_unit_scale: 1000}
|
||||
testVariant = {unit_value_with_description: "250.5"}
|
||||
BulkProducts.products = [testProduct]
|
||||
$scope.packVariant(testProduct, testVariant)
|
||||
expect(testVariant).toEqual
|
||||
unit_value: 250500
|
||||
unit_description: ''
|
||||
unit_value_with_description: "250.5"
|
||||
|
||||
it "does not convert value when using a non-scaled unit", ->
|
||||
testProduct = {id: 123}
|
||||
testVariant = {unit_value_with_description: "12"}
|
||||
BulkProducts.products = [testProduct]
|
||||
$scope.packVariant(testProduct, testVariant)
|
||||
expect(testVariant).toEqual
|
||||
unit_value: 12
|
||||
unit_description: ''
|
||||
unit_value_with_description: "12"
|
||||
|
||||
|
||||
describe "filtering products", ->
|
||||
beforeEach ->
|
||||
spyOn $scope, "packProduct"
|
||||
spyOn(window, "filterSubmitProducts").and.returnValue [
|
||||
{
|
||||
id: 1
|
||||
value: 3
|
||||
}
|
||||
{
|
||||
id: 2
|
||||
value: 4
|
||||
}
|
||||
]
|
||||
spyOn $scope, "updateProducts"
|
||||
DirtyProducts.addProductProperty 1, "propName", "something"
|
||||
DirtyProducts.addProductProperty 2, "propName", "something"
|
||||
$scope.products =
|
||||
1:
|
||||
id: 1
|
||||
2:
|
||||
id: 2
|
||||
|
||||
$scope.submitProducts()
|
||||
|
||||
it "packs all products and all dirty products", ->
|
||||
expect($scope.packProduct.calls.count()).toBe 4
|
||||
|
||||
it "filters returned dirty products", ->
|
||||
expect(filterSubmitProducts).toHaveBeenCalledWith
|
||||
1:
|
||||
id: 1
|
||||
propName: "something"
|
||||
2:
|
||||
id: 2
|
||||
propName: "something"
|
||||
|
||||
|
||||
it "sends dirty and filtered objects to submitProducts()", ->
|
||||
expect($scope.updateProducts).toHaveBeenCalledWith [
|
||||
{
|
||||
id: 1
|
||||
value: 3
|
||||
}
|
||||
{
|
||||
id: 2
|
||||
value: 4
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
describe "updating products", ->
|
||||
it "submits products to be updated with a http post request to /admin/products/bulk_update", ->
|
||||
$httpBackend.expectPOST("/admin/products/bulk_update").respond "list of products"
|
||||
$scope.updateProducts "list of products"
|
||||
$httpBackend.flush()
|
||||
|
||||
it "runs displaySuccess() when post returns success", ->
|
||||
spyOn $scope, "displaySuccess"
|
||||
spyOn BulkProducts, "updateVariantLists"
|
||||
spyOn DirtyProducts, "clear"
|
||||
|
||||
$scope.bulk_product_form = jasmine.createSpyObj('bulk_product_form', ['$setPristine'])
|
||||
|
||||
$scope.products = [
|
||||
{
|
||||
id: 1
|
||||
name: "P1"
|
||||
}
|
||||
{
|
||||
id: 2
|
||||
name: "P2"
|
||||
}
|
||||
]
|
||||
$httpBackend.expectPOST("/admin/products/bulk_update").respond 200, [
|
||||
{
|
||||
id: 1
|
||||
name: "P1"
|
||||
}
|
||||
{
|
||||
id: 2
|
||||
name: "P2"
|
||||
}
|
||||
]
|
||||
$scope.updateProducts "list of dirty products"
|
||||
$httpBackend.flush()
|
||||
$timeout.flush()
|
||||
expect($scope.displaySuccess).toHaveBeenCalled()
|
||||
expect($scope.bulk_product_form.$setPristine).toHaveBeenCalled
|
||||
expect(DirtyProducts.clear).toHaveBeenCalled()
|
||||
expect(BulkProducts.updateVariantLists).toHaveBeenCalled()
|
||||
|
||||
it "runs displayFailure() when post returns an error", ->
|
||||
spyOn $scope, "displayFailure"
|
||||
$scope.products = "updated list of products"
|
||||
$httpBackend.expectPOST("/admin/products/bulk_update").respond 500, "updated list of products"
|
||||
$scope.updateProducts "updated list of products"
|
||||
$httpBackend.flush()
|
||||
expect($scope.displayFailure).toHaveBeenCalled()
|
||||
|
||||
it "shows an alert with error information when post returns 400 with an errors array", ->
|
||||
spyOn(window, "alert")
|
||||
$scope.products = "updated list of products"
|
||||
$httpBackend.expectPOST("/admin/products/bulk_update").respond 400, { "errors": ["an error"] }
|
||||
$scope.updateProducts "updated list of products"
|
||||
$httpBackend.flush()
|
||||
expect(window.alert).toHaveBeenCalledWith("Saving failed with the following error(s):\nan error\n")
|
||||
|
||||
|
||||
describe "adding variants", ->
|
||||
beforeEach ->
|
||||
spyOn DisplayProperties, 'setShowVariants'
|
||||
|
||||
it "adds first and subsequent variants", ->
|
||||
product = {id: 123, variants: []}
|
||||
$scope.addVariant(product)
|
||||
$scope.addVariant(product)
|
||||
expect(product).toEqual
|
||||
id: 123
|
||||
variants: [
|
||||
{id: -1, price: null, unit_value: null, unit_description: null, on_demand: false, on_hand: null, display_as: null, display_name: null}
|
||||
{id: -2, price: null, unit_value: null, unit_description: null, on_demand: false, on_hand: null, display_as: null, display_name: null}
|
||||
]
|
||||
|
||||
it "shows the variant(s)", ->
|
||||
product = {id: 123, variants: []}
|
||||
$scope.addVariant(product)
|
||||
expect(DisplayProperties.setShowVariants).toHaveBeenCalledWith 123, true
|
||||
|
||||
|
||||
describe "deleting products", ->
|
||||
it "deletes products with a http delete request to /api/products/id/soft_delete", ->
|
||||
spyOn(window, "confirm").and.returnValue true
|
||||
$scope.products = [
|
||||
{
|
||||
id: 9
|
||||
permalink_live: "apples"
|
||||
}
|
||||
{
|
||||
id: 13
|
||||
permalink_live: "oranges"
|
||||
}
|
||||
]
|
||||
$scope.dirtyProducts = {}
|
||||
$httpBackend.expectDELETE("/api/products/13/soft_delete").respond 200, "data"
|
||||
$scope.deleteProduct $scope.products[1]
|
||||
$httpBackend.flush()
|
||||
|
||||
it "removes the specified product from both $scope.products and $scope.dirtyProducts (if it exists there)", ->
|
||||
spyOn(window, "confirm").and.returnValue true
|
||||
$scope.products = [
|
||||
{
|
||||
id: 9
|
||||
permalink_live: "apples"
|
||||
}
|
||||
{
|
||||
id: 13
|
||||
permalink_live: "oranges"
|
||||
}
|
||||
]
|
||||
DirtyProducts.addProductProperty 9, "someProperty", "something"
|
||||
DirtyProducts.addProductProperty 13, "name", "P1"
|
||||
|
||||
$httpBackend.expectDELETE("/api/products/13/soft_delete").respond 200, "data"
|
||||
$scope.deleteProduct $scope.products[1]
|
||||
$httpBackend.flush()
|
||||
expect($scope.products).toEqual [
|
||||
id: 9
|
||||
permalink_live: "apples"
|
||||
]
|
||||
expect(DirtyProducts.all()).toEqual 9:
|
||||
id: 9
|
||||
someProperty: "something"
|
||||
|
||||
|
||||
|
||||
describe "deleting variants", ->
|
||||
describe "when the variant is the only one left on the product", ->
|
||||
it "alerts the user", ->
|
||||
spyOn(window, "alert")
|
||||
$scope.products = [
|
||||
{id: 1, variants: [{id: 1}]}
|
||||
]
|
||||
$scope.deleteVariant $scope.products[0], $scope.products[0].variants[0]
|
||||
expect(window.alert).toHaveBeenCalledWith "The last variant cannot be deleted!"
|
||||
|
||||
describe "when the variant has not been saved", ->
|
||||
it "removes the variant from products and dirtyProducts", ->
|
||||
spyOn(window, "confirm").and.returnValue true
|
||||
$scope.products = [
|
||||
{id: 1, variants: [{id: -1},{id: -2}]}
|
||||
]
|
||||
DirtyProducts.addVariantProperty 1, -1, "something", "something"
|
||||
DirtyProducts.addProductProperty 1, "something", "something"
|
||||
$scope.deleteVariant $scope.products[0], $scope.products[0].variants[0]
|
||||
expect($scope.products).toEqual([
|
||||
{id: 1, variants: [{id: -2}]}
|
||||
])
|
||||
expect(DirtyProducts.all()).toEqual
|
||||
1: { id: 1, something: 'something'}
|
||||
|
||||
|
||||
describe "when the variant has been saved", ->
|
||||
it "deletes variants with a http delete request to /api/products/product_permalink/variants/(variant_id)/soft_delete", ->
|
||||
spyOn(window, "confirm").and.returnValue true
|
||||
$scope.products = [
|
||||
{
|
||||
id: 9
|
||||
permalink_live: "apples"
|
||||
variants: [{
|
||||
id: 3
|
||||
price: 12
|
||||
},
|
||||
{
|
||||
id: 4
|
||||
price: 15
|
||||
}
|
||||
]
|
||||
}
|
||||
{
|
||||
id: 13
|
||||
permalink_live: "oranges"
|
||||
}
|
||||
]
|
||||
$scope.dirtyProducts = {}
|
||||
$httpBackend.expectDELETE("/api/products/apples/variants/3/soft_delete").respond 200, "data"
|
||||
$scope.deleteVariant $scope.products[0], $scope.products[0].variants[0]
|
||||
$httpBackend.flush()
|
||||
|
||||
it "removes the specified variant from both the variants object and $scope.dirtyProducts (if it exists there)", ->
|
||||
spyOn(window, "confirm").and.returnValue true
|
||||
$scope.products = [
|
||||
{
|
||||
id: 9
|
||||
permalink_live: "apples"
|
||||
variants: [
|
||||
{
|
||||
id: 3
|
||||
price: 12.0
|
||||
}
|
||||
{
|
||||
id: 4
|
||||
price: 6.0
|
||||
}
|
||||
]
|
||||
}
|
||||
{
|
||||
id: 13
|
||||
permalink_live: "oranges"
|
||||
}
|
||||
]
|
||||
DirtyProducts.addVariantProperty 9, 3, "price", 12.0
|
||||
DirtyProducts.addVariantProperty 9, 4, "price", 6.0
|
||||
DirtyProducts.addProductProperty 13, "name", "P1"
|
||||
|
||||
$httpBackend.expectDELETE("/api/products/apples/variants/3/soft_delete").respond 200, "data"
|
||||
$scope.deleteVariant $scope.products[0], $scope.products[0].variants[0]
|
||||
$httpBackend.flush()
|
||||
expect($scope.products[0].variants).toEqual [
|
||||
id: 4
|
||||
price: 6.0
|
||||
]
|
||||
expect(DirtyProducts.all()).toEqual
|
||||
9:
|
||||
id: 9
|
||||
variants:
|
||||
4:
|
||||
id: 4
|
||||
price: 6.0
|
||||
|
||||
13:
|
||||
id: 13
|
||||
name: "P1"
|
||||
|
||||
|
||||
|
||||
describe "filtering products", ->
|
||||
describe "clearing filters", ->
|
||||
it "resets filter variables", ->
|
||||
$scope.query = "lala"
|
||||
$scope.producerFilter = "5"
|
||||
$scope.categoryFilter = "6"
|
||||
$scope.resetSelectFilters()
|
||||
expect($scope.query).toBe ""
|
||||
expect($scope.producerFilter).toBe "0"
|
||||
expect($scope.categoryFilter).toBe "0"
|
||||
|
||||
|
||||
describe "converting arrays of objects with ids to an object with ids as keys", ->
|
||||
it "returns an object", ->
|
||||
array = []
|
||||
expect(toObjectWithIDKeys(array)).toEqual {}
|
||||
|
||||
it "adds each object in the array provided with an id to the returned object with the id as its key", ->
|
||||
array = [
|
||||
{
|
||||
id: 1
|
||||
}
|
||||
{
|
||||
id: 3
|
||||
}
|
||||
]
|
||||
expect(toObjectWithIDKeys(array)).toEqual
|
||||
1:
|
||||
id: 1
|
||||
|
||||
3:
|
||||
id: 3
|
||||
|
||||
|
||||
it "ignores items which are not objects and those which do not possess ids", ->
|
||||
array = [
|
||||
{
|
||||
id: 1
|
||||
}
|
||||
"not an object"
|
||||
{
|
||||
notanid: 3
|
||||
}
|
||||
]
|
||||
expect(toObjectWithIDKeys(array)).toEqual 1:
|
||||
id: 1
|
||||
|
||||
|
||||
it "sends arrays with the key 'variants' to itself", ->
|
||||
spyOn(window, "toObjectWithIDKeys").and.callThrough()
|
||||
array = [
|
||||
{
|
||||
id: 1
|
||||
variants: [id: 17]
|
||||
}
|
||||
{
|
||||
id: 2
|
||||
variants:
|
||||
12:
|
||||
id: 12
|
||||
}
|
||||
]
|
||||
products = toObjectWithIDKeys(array)
|
||||
expect(products["1"].variants).toEqual 17:
|
||||
id: 17
|
||||
|
||||
expect(toObjectWithIDKeys).toHaveBeenCalledWith [id: 17]
|
||||
expect(toObjectWithIDKeys).not.toHaveBeenCalledWith {12: {id: 12}}
|
||||
@@ -0,0 +1,161 @@
|
||||
describe 'AdminCreateOrderCycleCtrl', ->
|
||||
ctrl = null
|
||||
scope = null
|
||||
event = null
|
||||
OrderCycle = null
|
||||
Enterprise = null
|
||||
EnterpriseFee = null
|
||||
|
||||
beforeEach ->
|
||||
scope =
|
||||
order_cycle_form: jasmine.createSpyObj('order_cycle_form', ['$dirty'])
|
||||
$watch: jasmine.createSpy('$watch')
|
||||
event =
|
||||
preventDefault: jasmine.createSpy('preventDefault')
|
||||
OrderCycle =
|
||||
exchangeSelectedVariants: jasmine.createSpy('exchangeSelectedVariants').and.returnValue('variants selected')
|
||||
productSuppliedToOrderCycle: jasmine.createSpy('productSuppliedToOrderCycle').and.returnValue('product supplied')
|
||||
variantSuppliedToOrderCycle: jasmine.createSpy('variantSuppliedToOrderCycle').and.returnValue('variant supplied')
|
||||
exchangeDirection: jasmine.createSpy('exchangeDirection').and.returnValue('exchange direction')
|
||||
toggleProducts: jasmine.createSpy('toggleProducts')
|
||||
setExchangeVariants: jasmine.createSpy('setExchangeVariants')
|
||||
addSupplier: jasmine.createSpy('addSupplier')
|
||||
addDistributor: jasmine.createSpy('addDistributor')
|
||||
removeExchange: jasmine.createSpy('removeExchange')
|
||||
addCoordinatorFee: jasmine.createSpy('addCoordinatorFee')
|
||||
removeCoordinatorFee: jasmine.createSpy('removeCoordinatorFee')
|
||||
addExchangeFee: jasmine.createSpy('addExchangeFee')
|
||||
removeExchangeFee: jasmine.createSpy('removeExchangeFee')
|
||||
removeDistributionOfVariant: jasmine.createSpy('removeDistributionOfVariant')
|
||||
create: jasmine.createSpy('create')
|
||||
new: jasmine.createSpy('new').and.returnValue "my order cycle"
|
||||
Enterprise =
|
||||
index: jasmine.createSpy('index').and.returnValue('enterprises list')
|
||||
supplied_products: 'supplied products'
|
||||
suppliedVariants: jasmine.createSpy('suppliedVariants').and.returnValue('supplied variants')
|
||||
totalVariants: jasmine.createSpy('totalVariants').and.returnValue('variants total')
|
||||
EnterpriseFee =
|
||||
index: jasmine.createSpy('index').and.returnValue('enterprise fees list')
|
||||
forEnterprise: jasmine.createSpy('forEnterprise').and.returnValue('enterprise fees for enterprise')
|
||||
ocInstance = {}
|
||||
|
||||
module('admin.orderCycles')
|
||||
inject ($controller) ->
|
||||
ctrl = $controller 'AdminCreateOrderCycleCtrl', {$scope: scope, OrderCycle: OrderCycle, Enterprise: Enterprise, EnterpriseFee: EnterpriseFee, ocInstance: ocInstance}
|
||||
|
||||
|
||||
it 'Loads enterprises and supplied products', ->
|
||||
expect(Enterprise.index).toHaveBeenCalled()
|
||||
expect(scope.enterprises).toEqual('enterprises list')
|
||||
expect(scope.supplied_products).toEqual('supplied products')
|
||||
|
||||
it 'Loads enterprise fees', ->
|
||||
expect(EnterpriseFee.index).toHaveBeenCalled()
|
||||
expect(scope.enterprise_fees).toEqual('enterprise fees list')
|
||||
|
||||
it 'Loads order cycles', ->
|
||||
expect(scope.order_cycle).toEqual('my order cycle')
|
||||
|
||||
describe 'Reporting when all resources are loaded', ->
|
||||
beforeEach inject (RequestMonitor) ->
|
||||
RequestMonitor.loading = false
|
||||
Enterprise.loaded = true
|
||||
EnterpriseFee.loaded = true
|
||||
OrderCycle.loaded = true
|
||||
|
||||
it 'returns true when all resources are loaded', ->
|
||||
expect(scope.loaded()).toBe(true)
|
||||
|
||||
it 'returns false otherwise', ->
|
||||
EnterpriseFee.loaded = false
|
||||
expect(scope.loaded()).toBe(false)
|
||||
|
||||
it "delegates suppliedVariants to Enterprise", ->
|
||||
expect(scope.suppliedVariants('enterprise_id')).toEqual('supplied variants')
|
||||
expect(Enterprise.suppliedVariants).toHaveBeenCalledWith('enterprise_id')
|
||||
|
||||
it 'Delegates exchangeSelectedVariants to OrderCycle', ->
|
||||
expect(scope.exchangeSelectedVariants('exchange')).toEqual('variants selected')
|
||||
expect(OrderCycle.exchangeSelectedVariants).toHaveBeenCalledWith('exchange')
|
||||
|
||||
it "delegates setExchangeVariants to OrderCycle", ->
|
||||
scope.setExchangeVariants('exchange', 'variants', 'selected')
|
||||
expect(OrderCycle.setExchangeVariants).toHaveBeenCalledWith('exchange', 'variants', 'selected')
|
||||
|
||||
it 'Delegates enterpriseTotalVariants to Enterprise', ->
|
||||
expect(scope.enterpriseTotalVariants('enterprise')).toEqual('variants total')
|
||||
expect(Enterprise.totalVariants).toHaveBeenCalledWith('enterprise')
|
||||
|
||||
it 'Delegates productSuppliedToOrderCycle to OrderCycle', ->
|
||||
expect(scope.productSuppliedToOrderCycle('product')).toEqual('product supplied')
|
||||
expect(OrderCycle.productSuppliedToOrderCycle).toHaveBeenCalledWith('product')
|
||||
|
||||
it 'Delegates variantSuppliedToOrderCycle to OrderCycle', ->
|
||||
expect(scope.variantSuppliedToOrderCycle('variant')).toEqual('variant supplied')
|
||||
expect(OrderCycle.variantSuppliedToOrderCycle).toHaveBeenCalledWith('variant')
|
||||
|
||||
it 'Delegates exchangeDirection to OrderCycle', ->
|
||||
expect(scope.exchangeDirection('exchange')).toEqual('exchange direction')
|
||||
expect(OrderCycle.exchangeDirection).toHaveBeenCalledWith('exchange')
|
||||
|
||||
it 'Finds enterprises participating in the order cycle that have fees', ->
|
||||
scope.enterprises =
|
||||
1: {id: 1, name: 'Eaterprises'}
|
||||
2: {id: 2, name: 'Pepper Tree Place'}
|
||||
3: {id: 3, name: 'South East'}
|
||||
OrderCycle.participatingEnterpriseIds = jasmine.createSpy('participatingEnterpriseIds').and.returnValue([2])
|
||||
EnterpriseFee.enterprise_fees = [ {enterprise_id: 2} ] # Pepper Tree Place has a fee
|
||||
expect(scope.enterprisesWithFees()).toEqual([
|
||||
{id: 2, name: 'Pepper Tree Place'}
|
||||
])
|
||||
|
||||
it 'Delegates enterpriseFeesForEnterprise to EnterpriseFee', ->
|
||||
scope.enterpriseFeesForEnterprise('123')
|
||||
expect(EnterpriseFee.forEnterprise).toHaveBeenCalledWith(123)
|
||||
|
||||
it 'Adds order cycle suppliers', ->
|
||||
scope.new_supplier_id = 'new supplier id'
|
||||
scope.addSupplier(event)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.addSupplier).toHaveBeenCalledWith('new supplier id')
|
||||
|
||||
it 'Adds order cycle distributors', ->
|
||||
scope.new_distributor_id = 'new distributor id'
|
||||
scope.addDistributor(event)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.addDistributor).toHaveBeenCalledWith('new distributor id')
|
||||
|
||||
it 'Removes order cycle exchanges', ->
|
||||
scope.removeExchange(event, 'exchange')
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.removeExchange).toHaveBeenCalledWith('exchange')
|
||||
|
||||
it 'Adds coordinator fees', ->
|
||||
scope.addCoordinatorFee(event)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.addCoordinatorFee).toHaveBeenCalled()
|
||||
|
||||
it 'Removes coordinator fees', ->
|
||||
scope.removeCoordinatorFee(event, 0)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.removeCoordinatorFee).toHaveBeenCalledWith(0)
|
||||
|
||||
it 'Adds exchange fees', ->
|
||||
scope.addExchangeFee(event)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.addExchangeFee).toHaveBeenCalled()
|
||||
|
||||
it 'Removes exchange fees', ->
|
||||
scope.removeExchangeFee(event, 'exchange', 0)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.removeExchangeFee).toHaveBeenCalledWith('exchange', 0)
|
||||
|
||||
it 'Removes distribution of a variant', ->
|
||||
scope.removeDistributionOfVariant('variant')
|
||||
expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('variant')
|
||||
|
||||
it 'Submits the order cycle via OrderCycle create', ->
|
||||
eventMock = {preventDefault: jasmine.createSpy()}
|
||||
scope.submit(eventMock,'/admin/order_cycles')
|
||||
expect(eventMock.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.create).toHaveBeenCalledWith('/admin/order_cycles')
|
||||
@@ -0,0 +1,166 @@
|
||||
describe 'AdminEditOrderCycleCtrl', ->
|
||||
ctrl = null
|
||||
scope = null
|
||||
event = null
|
||||
location = null
|
||||
OrderCycle = null
|
||||
Enterprise = null
|
||||
EnterpriseFee = null
|
||||
|
||||
beforeEach ->
|
||||
scope =
|
||||
order_cycle_form: jasmine.createSpyObj('order_cycle_form', ['$dirty', '$setPristine'])
|
||||
$watch: jasmine.createSpy('$watch')
|
||||
event =
|
||||
preventDefault: jasmine.createSpy('preventDefault')
|
||||
location =
|
||||
absUrl: ->
|
||||
'example.com/admin/order_cycles/27/edit'
|
||||
OrderCycle =
|
||||
load: jasmine.createSpy('load')
|
||||
exchangeSelectedVariants: jasmine.createSpy('exchangeSelectedVariants').and.returnValue('variants selected')
|
||||
productSuppliedToOrderCycle: jasmine.createSpy('productSuppliedToOrderCycle').and.returnValue('product supplied')
|
||||
variantSuppliedToOrderCycle: jasmine.createSpy('variantSuppliedToOrderCycle').and.returnValue('variant supplied')
|
||||
exchangeDirection: jasmine.createSpy('exchangeDirection').and.returnValue('exchange direction')
|
||||
toggleProducts: jasmine.createSpy('toggleProducts')
|
||||
setExchangeVariants: jasmine.createSpy('setExchangeVariants')
|
||||
addSupplier: jasmine.createSpy('addSupplier')
|
||||
addDistributor: jasmine.createSpy('addDistributor')
|
||||
removeExchange: jasmine.createSpy('removeExchange')
|
||||
addCoordinatorFee: jasmine.createSpy('addCoordinatorFee')
|
||||
removeCoordinatorFee: jasmine.createSpy('removeCoordinatorFee')
|
||||
addExchangeFee: jasmine.createSpy('addExchangeFee')
|
||||
removeExchangeFee: jasmine.createSpy('removeExchangeFee')
|
||||
removeDistributionOfVariant: jasmine.createSpy('removeDistributionOfVariant')
|
||||
update: jasmine.createSpy('update')
|
||||
Enterprise =
|
||||
index: jasmine.createSpy('index').and.returnValue('enterprises list')
|
||||
supplied_products: 'supplied products'
|
||||
suppliedVariants: jasmine.createSpy('suppliedVariants').and.returnValue('supplied variants')
|
||||
totalVariants: jasmine.createSpy('totalVariants').and.returnValue('variants total')
|
||||
EnterpriseFee =
|
||||
index: jasmine.createSpy('index').and.returnValue('enterprise fees list')
|
||||
forEnterprise: jasmine.createSpy('forEnterprise').and.returnValue('enterprise fees for enterprise')
|
||||
|
||||
module('admin.orderCycles')
|
||||
inject ($controller) ->
|
||||
ctrl = $controller 'AdminEditOrderCycleCtrl', {$scope: scope, $location: location, OrderCycle: OrderCycle, Enterprise: Enterprise, EnterpriseFee: EnterpriseFee}
|
||||
|
||||
it 'Loads enterprises and supplied products', ->
|
||||
expect(Enterprise.index).toHaveBeenCalled()
|
||||
expect(scope.enterprises).toEqual('enterprises list')
|
||||
expect(scope.supplied_products).toEqual('supplied products')
|
||||
|
||||
it 'Loads enterprise fees', ->
|
||||
expect(EnterpriseFee.index).toHaveBeenCalled()
|
||||
expect(scope.enterprise_fees).toEqual('enterprise fees list')
|
||||
|
||||
it 'Loads order cycles', ->
|
||||
expect(OrderCycle.load).toHaveBeenCalledWith('27')
|
||||
|
||||
describe 'Reporting when all resources are loaded', ->
|
||||
beforeEach inject (RequestMonitor) ->
|
||||
RequestMonitor.loading = false
|
||||
Enterprise.loaded = true
|
||||
EnterpriseFee.loaded = true
|
||||
OrderCycle.loaded = true
|
||||
|
||||
it 'returns true when all resources are loaded', ->
|
||||
expect(scope.loaded()).toBe(true)
|
||||
|
||||
it 'returns false otherwise', ->
|
||||
EnterpriseFee.loaded = false
|
||||
expect(scope.loaded()).toBe(false)
|
||||
|
||||
it "delegates suppliedVariants to Enterprise", ->
|
||||
expect(scope.suppliedVariants('enterprise_id')).toEqual('supplied variants')
|
||||
expect(Enterprise.suppliedVariants).toHaveBeenCalledWith('enterprise_id')
|
||||
|
||||
it 'Delegates exchangeSelectedVariants to OrderCycle', ->
|
||||
expect(scope.exchangeSelectedVariants('exchange')).toEqual('variants selected')
|
||||
expect(OrderCycle.exchangeSelectedVariants).toHaveBeenCalledWith('exchange')
|
||||
|
||||
it "delegates setExchangeVariants to OrderCycle", ->
|
||||
scope.setExchangeVariants('exchange', 'variants', 'selected')
|
||||
expect(OrderCycle.setExchangeVariants).toHaveBeenCalledWith('exchange', 'variants', 'selected')
|
||||
|
||||
it 'Delegates totalVariants to Enterprise', ->
|
||||
expect(scope.enterpriseTotalVariants('enterprise')).toEqual('variants total')
|
||||
expect(Enterprise.totalVariants).toHaveBeenCalledWith('enterprise')
|
||||
|
||||
it 'Delegates productSuppliedToOrderCycle to OrderCycle', ->
|
||||
expect(scope.productSuppliedToOrderCycle('product')).toEqual('product supplied')
|
||||
expect(OrderCycle.productSuppliedToOrderCycle).toHaveBeenCalledWith('product')
|
||||
|
||||
it 'Delegates variantSuppliedToOrderCycle to OrderCycle', ->
|
||||
expect(scope.variantSuppliedToOrderCycle('variant')).toEqual('variant supplied')
|
||||
expect(OrderCycle.variantSuppliedToOrderCycle).toHaveBeenCalledWith('variant')
|
||||
|
||||
it 'Delegates exchangeDirection to OrderCycle', ->
|
||||
expect(scope.exchangeDirection('exchange')).toEqual('exchange direction')
|
||||
expect(OrderCycle.exchangeDirection).toHaveBeenCalledWith('exchange')
|
||||
|
||||
it 'Finds enterprises participating in the order cycle that have fees', ->
|
||||
scope.enterprises =
|
||||
1: {id: 1, name: 'Eaterprises'}
|
||||
2: {id: 2, name: 'Pepper Tree Place'}
|
||||
3: {id: 3, name: 'South East'}
|
||||
OrderCycle.participatingEnterpriseIds = jasmine.createSpy('participatingEnterpriseIds').and.returnValue([2])
|
||||
EnterpriseFee.enterprise_fees = [ {enterprise_id: 2} ] # Pepper Tree Place has a fee
|
||||
expect(scope.enterprisesWithFees()).toEqual([
|
||||
{id: 2, name: 'Pepper Tree Place'}
|
||||
])
|
||||
|
||||
it 'Delegates enterpriseFeesForEnterprise to EnterpriseFee', ->
|
||||
scope.enterpriseFeesForEnterprise('123')
|
||||
expect(EnterpriseFee.forEnterprise).toHaveBeenCalledWith(123)
|
||||
|
||||
it 'Adds order cycle suppliers', ->
|
||||
scope.new_supplier_id = 'new supplier id'
|
||||
scope.addSupplier(event)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.addSupplier).toHaveBeenCalledWith('new supplier id')
|
||||
|
||||
it 'Adds order cycle distributors', ->
|
||||
scope.new_distributor_id = 'new distributor id'
|
||||
scope.addDistributor(event)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.addDistributor).toHaveBeenCalledWith('new distributor id')
|
||||
|
||||
it 'Removes order cycle exchanges', ->
|
||||
scope.removeExchange(event, 'exchange')
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.removeExchange).toHaveBeenCalledWith('exchange')
|
||||
expect(scope.order_cycle_form.$dirty).toEqual true
|
||||
|
||||
it 'Adds coordinator fees', ->
|
||||
scope.addCoordinatorFee(event)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.addCoordinatorFee).toHaveBeenCalled()
|
||||
|
||||
it 'Removes coordinator fees', ->
|
||||
scope.removeCoordinatorFee(event, 0)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.removeCoordinatorFee).toHaveBeenCalledWith(0)
|
||||
expect(scope.order_cycle_form.$dirty).toEqual true
|
||||
|
||||
it 'Adds exchange fees', ->
|
||||
scope.addExchangeFee(event)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.addExchangeFee).toHaveBeenCalled()
|
||||
|
||||
it 'Removes exchange fees', ->
|
||||
scope.removeExchangeFee(event, 'exchange', 0)
|
||||
expect(event.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.removeExchangeFee).toHaveBeenCalledWith('exchange', 0)
|
||||
expect(scope.order_cycle_form.$dirty).toEqual true
|
||||
|
||||
it 'Removes distribution of a variant', ->
|
||||
scope.removeDistributionOfVariant('variant')
|
||||
expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('variant')
|
||||
|
||||
it 'Submits the order cycle via OrderCycle update', ->
|
||||
eventMock = {preventDefault: jasmine.createSpy()}
|
||||
scope.submit(eventMock,'/admin/order_cycles')
|
||||
expect(eventMock.preventDefault).toHaveBeenCalled()
|
||||
expect(OrderCycle.update).toHaveBeenCalledWith('/admin/order_cycles', scope.order_cycle_form)
|
||||
@@ -0,0 +1,37 @@
|
||||
describe 'EnterpriseFee service', ->
|
||||
$httpBackend = null
|
||||
EnterpriseFee = null
|
||||
|
||||
beforeEach ->
|
||||
module 'admin.orderCycles'
|
||||
inject ($injector, _$httpBackend_)->
|
||||
EnterpriseFee = $injector.get('EnterpriseFee')
|
||||
$httpBackend = _$httpBackend_
|
||||
$httpBackend.whenGET('/admin/enterprise_fees/for_order_cycle.json').respond [
|
||||
{id: 1, name: "Yayfee", enterprise_id: 1}
|
||||
{id: 2, name: "FeeTwo", enterprise_id: 2}
|
||||
]
|
||||
|
||||
it 'loads enterprise fees', ->
|
||||
enterprise_fees = EnterpriseFee.index()
|
||||
$httpBackend.flush()
|
||||
expected_fees = [
|
||||
new EnterpriseFee.EnterpriseFee({id: 1, name: "Yayfee", enterprise_id: 1})
|
||||
new EnterpriseFee.EnterpriseFee({id: 2, name: "FeeTwo", enterprise_id: 2})
|
||||
]
|
||||
for fee, i in enterprise_fees
|
||||
expect(fee.id).toEqual(expected_fees[i].id)
|
||||
|
||||
it 'reports its loadedness', ->
|
||||
expect(EnterpriseFee.loaded).toBe(false)
|
||||
EnterpriseFee.index()
|
||||
$httpBackend.flush()
|
||||
expect(EnterpriseFee.loaded).toBe(true)
|
||||
|
||||
it 'returns enterprise fees for an enterprise', ->
|
||||
all_enterprise_fees = EnterpriseFee.index()
|
||||
$httpBackend.flush()
|
||||
enterprise_fees = EnterpriseFee.forEnterprise(1)
|
||||
expect(enterprise_fees).toEqual [
|
||||
new EnterpriseFee.EnterpriseFee({id: 1, name: "Yayfee", enterprise_id: 1})
|
||||
]
|
||||
@@ -0,0 +1,75 @@
|
||||
describe 'Enterprise service', ->
|
||||
$httpBackend = null
|
||||
Enterprise = null
|
||||
|
||||
beforeEach ->
|
||||
module 'admin.orderCycles'
|
||||
inject ($injector, _$httpBackend_)->
|
||||
Enterprise = $injector.get('Enterprise')
|
||||
$httpBackend = _$httpBackend_
|
||||
$httpBackend.whenGET('/admin/enterprises/for_order_cycle.json').respond [
|
||||
{id: 1, name: 'One', supplied_products: [1, 2], is_primary_producer: true}
|
||||
{id: 2, name: 'Two', supplied_products: [3, 4]}
|
||||
{id: 3, name: 'Three', supplied_products: [5, 6], sells: 'any'}
|
||||
]
|
||||
|
||||
it 'loads enterprises as a hash', ->
|
||||
enterprises = Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
expect(enterprises).toEqual
|
||||
1: new Enterprise.Enterprise({id: 1, name: 'One', supplied_products: [1, 2], is_primary_producer: true})
|
||||
2: new Enterprise.Enterprise({id: 2, name: 'Two', supplied_products: [3, 4]})
|
||||
3: new Enterprise.Enterprise({id: 3, name: 'Three', supplied_products: [5, 6], sells: 'any'})
|
||||
|
||||
it 'reports its loadedness', ->
|
||||
expect(Enterprise.loaded).toBe(false)
|
||||
Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
expect(Enterprise.loaded).toBe(true)
|
||||
|
||||
it 'loads producers as an array', ->
|
||||
Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
expect(Enterprise.producer_enterprises).toEqual [new Enterprise.Enterprise({id: 1, name: 'One', supplied_products: [1, 2], is_primary_producer: true})]
|
||||
|
||||
it 'loads hubs as an array', ->
|
||||
Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
expect(Enterprise.hub_enterprises).toEqual [new Enterprise.Enterprise({id: 3, name: 'Three', supplied_products: [5, 6], sells: 'any'})]
|
||||
|
||||
it 'collates all supplied products', ->
|
||||
enterprises = Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
expect(Enterprise.supplied_products).toEqual [1, 2, 3, 4, 5, 6]
|
||||
|
||||
it "finds supplied variants for an enterprise", ->
|
||||
spyOn(Enterprise, 'variantsOf').and.returnValue(10)
|
||||
Enterprise.index()
|
||||
$httpBackend.flush()
|
||||
expect(Enterprise.suppliedVariants(1)).toEqual [10, 10]
|
||||
|
||||
describe "finding the variants of a product", ->
|
||||
it "returns the master for products without variants", ->
|
||||
p =
|
||||
master_id: 1
|
||||
variants: []
|
||||
expect(Enterprise.variantsOf(p)).toEqual [1]
|
||||
|
||||
it "returns the variant ids for products with variants", ->
|
||||
p =
|
||||
master_id: 1
|
||||
variants: [{id: 2}, {id: 3}]
|
||||
expect(Enterprise.variantsOf(p)).toEqual [2, 3]
|
||||
|
||||
it 'counts total variants supplied by an enterprise', ->
|
||||
enterprise =
|
||||
supplied_products: [
|
||||
{variants: []},
|
||||
{variants: []},
|
||||
{variants: [{}, {}, {}]}
|
||||
]
|
||||
|
||||
expect(Enterprise.totalVariants(enterprise)).toEqual(5)
|
||||
|
||||
it 'returns zero when enterprise is null', ->
|
||||
expect(Enterprise.totalVariants(null)).toEqual(0)
|
||||
@@ -0,0 +1,520 @@
|
||||
describe 'OrderCycle service', ->
|
||||
OrderCycle = null
|
||||
$httpBackend = null
|
||||
$window = null
|
||||
|
||||
beforeEach ->
|
||||
$window = {navigator: {userAgent: 'foo'}}
|
||||
|
||||
module 'admin.orderCycles', ($provide)->
|
||||
$provide.value('$window', $window)
|
||||
null
|
||||
|
||||
inject ($injector, _$httpBackend_)->
|
||||
OrderCycle = $injector.get('OrderCycle')
|
||||
$httpBackend = _$httpBackend_
|
||||
$httpBackend.whenGET('/admin/order_cycles/123.json').respond
|
||||
id: 123
|
||||
name: 'Test Order Cycle'
|
||||
coordinator_id: 456
|
||||
coordinator_fees: []
|
||||
exchanges: [
|
||||
{sender_id: 1, receiver_id: 456, incoming: true}
|
||||
{sender_id: 456, receiver_id: 2, incoming: false}
|
||||
]
|
||||
$httpBackend.whenGET('/admin/order_cycles/new.json').respond
|
||||
id: 123
|
||||
name: 'New Order Cycle'
|
||||
coordinator_id: 456
|
||||
coordinator_fees: []
|
||||
exchanges: []
|
||||
|
||||
it 'initialises order cycle', ->
|
||||
expect(OrderCycle.order_cycle).toEqual {incoming_exchanges: [], outgoing_exchanges: []}
|
||||
|
||||
it 'counts selected variants in an exchange', ->
|
||||
result = OrderCycle.exchangeSelectedVariants({variants: {1: true, 2: false, 3: true}})
|
||||
expect(result).toEqual(2)
|
||||
|
||||
describe "fetching exchange ids", ->
|
||||
it "gets enterprise ids as ints", ->
|
||||
OrderCycle.order_cycle.incoming_exchanges = [
|
||||
{enterprise_id: 1}
|
||||
{enterprise_id: '2'}
|
||||
]
|
||||
OrderCycle.order_cycle.outgoing_exchanges = [
|
||||
{enterprise_id: 3}
|
||||
{enterprise_id: '4'}
|
||||
]
|
||||
expect(OrderCycle.exchangeIds('incoming')).toEqual [1, 2]
|
||||
|
||||
describe "checking for novel enterprises", ->
|
||||
e1 = {id: 1}
|
||||
e2 = {id: 2}
|
||||
|
||||
beforeEach ->
|
||||
OrderCycle.order_cycle.incoming_exchanges = [{enterprise_id: 1}]
|
||||
OrderCycle.order_cycle.outgoing_exchanges = [{enterprise_id: 1}]
|
||||
|
||||
it "detects novel suppliers", ->
|
||||
expect(OrderCycle.novelSupplier(e1)).toBe false
|
||||
expect(OrderCycle.novelSupplier(e2)).toBe true
|
||||
|
||||
it "detects novel suppliers with enterprise as string id", ->
|
||||
expect(OrderCycle.novelSupplier('1')).toBe false
|
||||
expect(OrderCycle.novelSupplier('2')).toBe true
|
||||
|
||||
it "detects novel distributors", ->
|
||||
expect(OrderCycle.novelDistributor(e1)).toBe false
|
||||
expect(OrderCycle.novelDistributor(e2)).toBe true
|
||||
|
||||
it "detects novel distributors with enterprise as string id", ->
|
||||
expect(OrderCycle.novelDistributor('1')).toBe false
|
||||
expect(OrderCycle.novelDistributor('2')).toBe true
|
||||
|
||||
|
||||
describe 'fetching the direction for an exchange', ->
|
||||
it 'returns "incoming" for incoming exchanges', ->
|
||||
exchange = {id: 1}
|
||||
OrderCycle.order_cycle.incoming_exchanges = [exchange]
|
||||
OrderCycle.order_cycle.outgoing_exchanges = []
|
||||
expect(OrderCycle.exchangeDirection(exchange)).toEqual 'incoming'
|
||||
|
||||
it 'returns "outgoing" for outgoing exchanges', ->
|
||||
exchange = {id: 1}
|
||||
OrderCycle.order_cycle.incoming_exchanges = []
|
||||
OrderCycle.order_cycle.outgoing_exchanges = [exchange]
|
||||
expect(OrderCycle.exchangeDirection(exchange)).toEqual 'outgoing'
|
||||
|
||||
describe "setting exchange variants", ->
|
||||
describe "when I have permissions to edit the variants", ->
|
||||
beforeEach ->
|
||||
OrderCycle.order_cycle["editable_variants_for_outgoing_exchanges"] = { 1: [1, 2, 3] }
|
||||
|
||||
it "sets all variants to the provided value", ->
|
||||
exchange = { enterprise_id: 1, incoming: false, variants: {2: false}}
|
||||
OrderCycle.setExchangeVariants(exchange, [1, 2, 3], true)
|
||||
expect(exchange.variants).toEqual {1: true, 2: true, 3: true}
|
||||
|
||||
describe "when I don't have permissions to edit the variants", ->
|
||||
beforeEach ->
|
||||
OrderCycle.order_cycle["editable_variants_for_outgoing_exchanges"] = { 1: [] }
|
||||
|
||||
it "does not change variants to the provided value", ->
|
||||
exchange = { enterprise_id: 1, incoming: false, variants: {2: false}}
|
||||
OrderCycle.setExchangeVariants(exchange, [1, 2, 3], true)
|
||||
expect(exchange.variants).toEqual {2: false}
|
||||
|
||||
describe 'adding suppliers', ->
|
||||
exchange = null
|
||||
|
||||
beforeEach ->
|
||||
# Initialise OC
|
||||
OrderCycle.new()
|
||||
$httpBackend.flush()
|
||||
|
||||
it 'adds the supplier to incoming exchanges', ->
|
||||
OrderCycle.addSupplier('123')
|
||||
expect(OrderCycle.order_cycle.incoming_exchanges).toEqual [
|
||||
{enterprise_id: '123', incoming: true, active: true, variants: {}, enterprise_fees: []}
|
||||
]
|
||||
|
||||
describe 'adding distributors', ->
|
||||
exchange = null
|
||||
|
||||
beforeEach ->
|
||||
# Initialise OC
|
||||
OrderCycle.new()
|
||||
$httpBackend.flush()
|
||||
|
||||
it 'adds the distributor to outgoing exchanges', ->
|
||||
OrderCycle.addDistributor('123')
|
||||
expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [
|
||||
{enterprise_id: '123', incoming: false, active: true, variants: {}, enterprise_fees: []}
|
||||
]
|
||||
|
||||
describe 'removing exchanges', ->
|
||||
exchange = null
|
||||
|
||||
beforeEach ->
|
||||
spyOn(OrderCycle, 'removeDistributionOfVariant')
|
||||
exchange =
|
||||
enterprise_id: '123'
|
||||
active: true
|
||||
incoming: false
|
||||
variants: {1: true, 2: false, 3: true}
|
||||
enterprise_fees: []
|
||||
|
||||
describe "removing incoming exchanges", ->
|
||||
beforeEach ->
|
||||
exchange.incoming = true
|
||||
OrderCycle.order_cycle.incoming_exchanges = [exchange]
|
||||
|
||||
it 'removes the exchange', ->
|
||||
OrderCycle.removeExchange(exchange)
|
||||
expect(OrderCycle.order_cycle.incoming_exchanges).toEqual []
|
||||
|
||||
it 'removes distribution of all exchange variants', ->
|
||||
OrderCycle.removeExchange(exchange)
|
||||
expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('1')
|
||||
expect(OrderCycle.removeDistributionOfVariant).not.toHaveBeenCalledWith('2')
|
||||
expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('3')
|
||||
|
||||
describe "removing outgoing exchanges", ->
|
||||
beforeEach ->
|
||||
exchange.incoming = false
|
||||
OrderCycle.order_cycle.outgoing_exchanges = [exchange]
|
||||
|
||||
it 'removes the exchange', ->
|
||||
OrderCycle.removeExchange(exchange)
|
||||
expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual []
|
||||
|
||||
it "does not remove distribution of any variants", ->
|
||||
OrderCycle.removeExchange(exchange)
|
||||
expect(OrderCycle.removeDistributionOfVariant).not.toHaveBeenCalled()
|
||||
|
||||
it 'adds coordinator fees', ->
|
||||
# Initialise OC
|
||||
OrderCycle.new()
|
||||
$httpBackend.flush()
|
||||
OrderCycle.addCoordinatorFee()
|
||||
expect(OrderCycle.order_cycle.coordinator_fees).toEqual [{}]
|
||||
|
||||
describe 'removing coordinator fees', ->
|
||||
it 'removes a coordinator fee by index', ->
|
||||
OrderCycle.order_cycle.coordinator_fees = [
|
||||
{id: 1}
|
||||
{id: 2}
|
||||
{id: 3}
|
||||
]
|
||||
OrderCycle.removeCoordinatorFee(1)
|
||||
expect(OrderCycle.order_cycle.coordinator_fees).toEqual [
|
||||
{id: 1}
|
||||
{id: 3}
|
||||
]
|
||||
|
||||
it 'adds exchange fees', ->
|
||||
exchange = {enterprise_fees: []}
|
||||
OrderCycle.addExchangeFee(exchange)
|
||||
expect(exchange.enterprise_fees).toEqual [{}]
|
||||
|
||||
describe 'removing exchange fees', ->
|
||||
it 'removes an exchange fee by index', ->
|
||||
exchange =
|
||||
enterprise_fees: [
|
||||
{id: 1}
|
||||
{id: 2}
|
||||
{id: 3}
|
||||
]
|
||||
OrderCycle.removeExchangeFee(exchange, 1)
|
||||
expect(exchange.enterprise_fees).toEqual [
|
||||
{id: 1}
|
||||
{id: 3}
|
||||
]
|
||||
|
||||
it 'finds participating enterprise ids', ->
|
||||
OrderCycle.order_cycle.incoming_exchanges = [
|
||||
{enterprise_id: 1}
|
||||
{enterprise_id: 2}
|
||||
]
|
||||
OrderCycle.order_cycle.outgoing_exchanges = [
|
||||
{enterprise_id: 2}
|
||||
{enterprise_id: 3}
|
||||
]
|
||||
expect(OrderCycle.participatingEnterpriseIds()).toEqual [1, 2, 3]
|
||||
|
||||
describe 'fetching all variants supplied on incoming exchanges', ->
|
||||
it 'collects variants from incoming exchanges', ->
|
||||
OrderCycle.order_cycle.incoming_exchanges = [
|
||||
{variants: {1: true, 2: false}}
|
||||
{variants: {3: false, 4: true}}
|
||||
{variants: {5: true, 6: false}}
|
||||
]
|
||||
expect(OrderCycle.incomingExchangesVariants()).toEqual [1, 4, 5]
|
||||
|
||||
describe 'checking whether a product is supplied to the order cycle', ->
|
||||
product_master_present = product_variant_present = product_master_absent = product_variant_absent = null
|
||||
|
||||
beforeEach ->
|
||||
product_master_present =
|
||||
name: "Linseed (500g)"
|
||||
master_id: 1
|
||||
variants: []
|
||||
product_variant_present =
|
||||
name: "Linseed (500g)"
|
||||
master_id: 2
|
||||
variants: [{id: 3}, {id: 4}]
|
||||
product_master_absent =
|
||||
name: "Linseed (500g)"
|
||||
master_id: 5
|
||||
variants: []
|
||||
product_variant_absent =
|
||||
name: "Linseed (500g)"
|
||||
master_id: 6
|
||||
variants: [{id: 7}, {id: 8}]
|
||||
|
||||
spyOn(OrderCycle, 'incomingExchangesVariants').and.returnValue([1, 3])
|
||||
|
||||
it 'returns true for products whose master is supplied', ->
|
||||
expect(OrderCycle.productSuppliedToOrderCycle(product_master_present)).toBeTruthy()
|
||||
|
||||
it 'returns true for products for whom a variant is supplied', ->
|
||||
expect(OrderCycle.productSuppliedToOrderCycle(product_variant_present)).toBeTruthy()
|
||||
|
||||
it 'returns false for products whose master is not supplied', ->
|
||||
expect(OrderCycle.productSuppliedToOrderCycle(product_master_absent)).toBeFalsy()
|
||||
|
||||
it 'returns false for products whose variants are not supplied', ->
|
||||
expect(OrderCycle.productSuppliedToOrderCycle(product_variant_absent)).toBeFalsy()
|
||||
|
||||
|
||||
describe 'checking whether a variant is supplied to the order cycle', ->
|
||||
beforeEach ->
|
||||
spyOn(OrderCycle, 'incomingExchangesVariants').and.returnValue([1, 3])
|
||||
|
||||
it 'returns true for variants that are supplied', ->
|
||||
expect(OrderCycle.variantSuppliedToOrderCycle({id: 1})).toBeTruthy()
|
||||
|
||||
it 'returns false for variants that are not supplied', ->
|
||||
expect(OrderCycle.variantSuppliedToOrderCycle({id: 999})).toBeFalsy()
|
||||
|
||||
|
||||
describe 'remove all distribution of a variant', ->
|
||||
it 'removes the variant from every outgoing exchange', ->
|
||||
OrderCycle.order_cycle.outgoing_exchanges = [
|
||||
{variants: {123: true, 234: true}}
|
||||
{variants: {123: true, 333: true}}
|
||||
]
|
||||
OrderCycle.removeDistributionOfVariant('123')
|
||||
expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [
|
||||
{variants: {123: false, 234: true}}
|
||||
{variants: {123: false, 333: true}}
|
||||
]
|
||||
|
||||
describe 'loading an order cycle, reporting loadedness', ->
|
||||
it 'reports its loadedness', ->
|
||||
expect(OrderCycle.loaded).toBe(false)
|
||||
OrderCycle.load('123')
|
||||
$httpBackend.flush()
|
||||
expect(OrderCycle.loaded).toBe(true)
|
||||
|
||||
describe 'loading a new order cycle', ->
|
||||
beforeEach ->
|
||||
OrderCycle.new()
|
||||
$httpBackend.flush()
|
||||
|
||||
|
||||
it 'loads basic fields', ->
|
||||
expect(OrderCycle.order_cycle.id).toEqual(123)
|
||||
expect(OrderCycle.order_cycle.name).toEqual('New Order Cycle')
|
||||
expect(OrderCycle.order_cycle.coordinator_id).toEqual(456)
|
||||
|
||||
it 'initialises the incoming and outgoing exchanges', ->
|
||||
expect(OrderCycle.order_cycle.incoming_exchanges).toEqual []
|
||||
expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual []
|
||||
|
||||
it 'removes the original exchanges array', ->
|
||||
expect(OrderCycle.order_cycle.exchanges).toBeUndefined()
|
||||
|
||||
describe 'loading an existing order cycle', ->
|
||||
beforeEach ->
|
||||
OrderCycle.load('123')
|
||||
$httpBackend.flush()
|
||||
|
||||
it 'loads basic fields', ->
|
||||
expect(OrderCycle.order_cycle.id).toEqual(123)
|
||||
expect(OrderCycle.order_cycle.name).toEqual('Test Order Cycle')
|
||||
expect(OrderCycle.order_cycle.coordinator_id).toEqual(456)
|
||||
|
||||
it 'splits exchanges into incoming and outgoing', ->
|
||||
expect(OrderCycle.order_cycle.incoming_exchanges).toEqual [
|
||||
sender_id: 1
|
||||
enterprise_id: 1
|
||||
incoming: true
|
||||
active: true
|
||||
]
|
||||
|
||||
expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [
|
||||
receiver_id: 2
|
||||
enterprise_id: 2
|
||||
incoming: false
|
||||
active: true
|
||||
]
|
||||
|
||||
it 'removes the original exchanges array', ->
|
||||
expect(OrderCycle.order_cycle.exchanges).toBeUndefined()
|
||||
|
||||
describe 'creating an order cycle', ->
|
||||
beforeEach ->
|
||||
spyOn(OrderCycle, 'confirmNoDistributors').and.returnValue true
|
||||
|
||||
it 'redirects to the destination page on success', ->
|
||||
OrderCycle.order_cycle = 'this is the order cycle'
|
||||
spyOn(OrderCycle, 'dataForSubmit').and.returnValue('this is the submit data')
|
||||
$httpBackend.expectPOST('/admin/order_cycles.json', {
|
||||
order_cycle: 'this is the submit data'
|
||||
}).respond {success: true}
|
||||
|
||||
OrderCycle.create('/destination/page')
|
||||
$httpBackend.flush()
|
||||
expect($window.location).toEqual('/destination/page')
|
||||
|
||||
it 'does not redirect on error', ->
|
||||
OrderCycle.order_cycle = 'this is the order cycle'
|
||||
spyOn(OrderCycle, 'dataForSubmit').and.returnValue('this is the submit data')
|
||||
$httpBackend.expectPOST('/admin/order_cycles.json', {
|
||||
order_cycle: 'this is the submit data'
|
||||
}).respond 400, { errors: [] }
|
||||
|
||||
OrderCycle.create('/destination/page')
|
||||
$httpBackend.flush()
|
||||
expect($window.location).toEqual(undefined)
|
||||
|
||||
describe 'updating an order cycle', ->
|
||||
beforeEach ->
|
||||
spyOn(OrderCycle, 'confirmNoDistributors').and.returnValue true
|
||||
|
||||
it 'redirects to the destination page on success', ->
|
||||
form = jasmine.createSpyObj('order_cycle_form', ['$dirty', '$setPristine'])
|
||||
OrderCycle.order_cycle = 'this is the order cycle'
|
||||
spyOn(OrderCycle, 'dataForSubmit').and.returnValue('this is the submit data')
|
||||
$httpBackend.expectPUT('/admin/order_cycles.json?reloading=1', {
|
||||
order_cycle: 'this is the submit data'
|
||||
}).respond {success: true}
|
||||
|
||||
OrderCycle.update('/destination/page', form)
|
||||
$httpBackend.flush()
|
||||
expect($window.location).toEqual('/destination/page')
|
||||
expect(form.$setPristine.calls.count()).toBe 1
|
||||
|
||||
it 'does not redirect on error', ->
|
||||
OrderCycle.order_cycle = 'this is the order cycle'
|
||||
spyOn(OrderCycle, 'dataForSubmit').and.returnValue('this is the submit data')
|
||||
$httpBackend.expectPUT('/admin/order_cycles.json?reloading=1', {
|
||||
order_cycle: 'this is the submit data'
|
||||
}).respond 400, { errors: [] }
|
||||
|
||||
OrderCycle.update('/destination/page')
|
||||
$httpBackend.flush()
|
||||
expect($window.location).toEqual(undefined)
|
||||
|
||||
describe 'preparing data for form submission', ->
|
||||
it 'calls all the methods', ->
|
||||
OrderCycle.order_cycle = {foo: 'bar'}
|
||||
spyOn(OrderCycle, 'removeInactiveExchanges')
|
||||
spyOn(OrderCycle, 'translateCoordinatorFees')
|
||||
spyOn(OrderCycle, 'translateExchangeFees')
|
||||
OrderCycle.dataForSubmit()
|
||||
expect(OrderCycle.removeInactiveExchanges).toHaveBeenCalled()
|
||||
expect(OrderCycle.translateCoordinatorFees).toHaveBeenCalled()
|
||||
expect(OrderCycle.translateExchangeFees).toHaveBeenCalled()
|
||||
|
||||
it 'removes inactive exchanges', ->
|
||||
data =
|
||||
incoming_exchanges: [
|
||||
{enterprise_id: "1", active: false}
|
||||
{enterprise_id: "2", active: true}
|
||||
{enterprise_id: "3", active: false}
|
||||
]
|
||||
outgoing_exchanges: [
|
||||
{enterprise_id: "4", active: true}
|
||||
{enterprise_id: "5", active: false}
|
||||
{enterprise_id: "6", active: true}
|
||||
]
|
||||
|
||||
data = OrderCycle.removeInactiveExchanges(data)
|
||||
|
||||
expect(data.incoming_exchanges).toEqual [
|
||||
{enterprise_id: "2", active: true}
|
||||
]
|
||||
expect(data.outgoing_exchanges).toEqual [
|
||||
{enterprise_id: "4", active: true}
|
||||
{enterprise_id: "6", active: true}
|
||||
]
|
||||
|
||||
it 'converts coordinator fees into a list of ids', ->
|
||||
order_cycle =
|
||||
coordinator_fees: [
|
||||
{id: 1}
|
||||
{id: 2}
|
||||
]
|
||||
|
||||
data = OrderCycle.translateCoordinatorFees(order_cycle)
|
||||
|
||||
expect(data.coordinator_fees).toBeUndefined()
|
||||
expect(data.coordinator_fee_ids).toEqual [1, 2]
|
||||
|
||||
it "preserves original data when converting coordinator fees", ->
|
||||
OrderCycle.order_cycle =
|
||||
coordinator_fees: [
|
||||
{id: 1}
|
||||
{id: 2}
|
||||
]
|
||||
|
||||
data = OrderCycle.deepCopy()
|
||||
data = OrderCycle.translateCoordinatorFees(data)
|
||||
|
||||
expect(OrderCycle.order_cycle.coordinator_fees).toEqual [{id: 1}, {id: 2}]
|
||||
expect(OrderCycle.order_cycle.coordinator_fee_ids).toBeUndefined()
|
||||
|
||||
describe "converting exchange fees into a list of ids", ->
|
||||
order_cycle = null
|
||||
data = null
|
||||
|
||||
beforeEach ->
|
||||
order_cycle =
|
||||
incoming_exchanges: [
|
||||
enterprise_fees: [
|
||||
{id: 1}
|
||||
{id: 2}
|
||||
]
|
||||
]
|
||||
outgoing_exchanges: [
|
||||
enterprise_fees: [
|
||||
{id: 3}
|
||||
{id: 4}
|
||||
]
|
||||
]
|
||||
OrderCycle.order_cycle = order_cycle
|
||||
|
||||
data = OrderCycle.deepCopy()
|
||||
data = OrderCycle.translateExchangeFees(data)
|
||||
|
||||
it 'converts exchange fees into a list of ids', ->
|
||||
expect(data.incoming_exchanges[0].enterprise_fees).toBeUndefined()
|
||||
expect(data.outgoing_exchanges[0].enterprise_fees).toBeUndefined()
|
||||
expect(data.incoming_exchanges[0].enterprise_fee_ids).toEqual [1, 2]
|
||||
expect(data.outgoing_exchanges[0].enterprise_fee_ids).toEqual [3, 4]
|
||||
|
||||
it "preserves original data when converting exchange fees", ->
|
||||
expect(order_cycle.incoming_exchanges[0].enterprise_fees).toEqual [{id: 1}, {id: 2}]
|
||||
expect(order_cycle.outgoing_exchanges[0].enterprise_fees).toEqual [{id: 3}, {id: 4}]
|
||||
expect(order_cycle.incoming_exchanges[0].enterprise_fee_ids).toBeUndefined()
|
||||
expect(order_cycle.outgoing_exchanges[0].enterprise_fee_ids).toBeUndefined()
|
||||
|
||||
describe "confirming when there are no distributors", ->
|
||||
order_cycle_with_exchanges = order_cycle_without_exchanges = null
|
||||
|
||||
beforeEach ->
|
||||
order_cycle_with_exchanges =
|
||||
outgoing_exchanges: [{}]
|
||||
order_cycle_without_exchanges =
|
||||
outgoing_exchanges: []
|
||||
|
||||
it "returns true when there are distributors", ->
|
||||
spyOn(window, 'confirm')
|
||||
OrderCycle.order_cycle = order_cycle_with_exchanges
|
||||
expect(OrderCycle.confirmNoDistributors()).toBe true
|
||||
expect(window.confirm).not.toHaveBeenCalled()
|
||||
|
||||
it "returns true when there are no distributors but the user confirms", ->
|
||||
spyOn(window, 'confirm').and.returnValue(true)
|
||||
OrderCycle.order_cycle = order_cycle_without_exchanges
|
||||
expect(OrderCycle.confirmNoDistributors()).toBe true
|
||||
expect(window.confirm).toHaveBeenCalled()
|
||||
|
||||
it "returns false when there are no distributors and the user does not confirm", ->
|
||||
spyOn(window, 'confirm').and.returnValue(false)
|
||||
OrderCycle.order_cycle = order_cycle_without_exchanges
|
||||
expect(OrderCycle.confirmNoDistributors()).toBe false
|
||||
expect(window.confirm).toHaveBeenCalled()
|
||||
Reference in New Issue
Block a user