mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-06 02:51:34 +00:00
Moving variants to a service, backreferencing and linking
This commit is contained in:
@@ -1,9 +1,28 @@
|
||||
describe 'Cart service', ->
|
||||
Cart = null
|
||||
orders = []
|
||||
Variants = null
|
||||
variant = {id: 1}
|
||||
order = {
|
||||
line_items: [
|
||||
variant: variant
|
||||
]
|
||||
}
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
angular.module('Darkswarm').value('order', orders)
|
||||
angular.module('Darkswarm').value('currentOrder', order)
|
||||
inject ($injector)->
|
||||
Variants = $injector.get("Variants")
|
||||
Cart = $injector.get("Cart")
|
||||
|
||||
it "backreferences line items", ->
|
||||
expect(Cart.line_items[0].variant.line_item).toBe Cart.line_items[0]
|
||||
|
||||
it "registers variants with the Variants service", ->
|
||||
expect(Variants.variants[1]).toBe variant
|
||||
|
||||
it "creates and backreferences new line items if necessary", ->
|
||||
Cart.register_variant(v2 = {id: 2})
|
||||
expect(Cart.line_items[1].variant).toBe v2
|
||||
expect(Cart.line_items[1].variant.line_item).toBe Cart.line_items[1]
|
||||
|
||||
|
||||
@@ -2,21 +2,27 @@ describe 'Products service', ->
|
||||
$httpBackend = null
|
||||
Products = null
|
||||
Enterprises = null
|
||||
Variants = null
|
||||
CurrentHubMock = {}
|
||||
product =
|
||||
test: "cats"
|
||||
supplier:
|
||||
id: 9
|
||||
price: 11
|
||||
variants: []
|
||||
currentOrder =
|
||||
line_items: []
|
||||
product = null
|
||||
beforeEach ->
|
||||
product =
|
||||
test: "cats"
|
||||
supplier:
|
||||
id: 9
|
||||
price: 11
|
||||
variants: []
|
||||
module 'Darkswarm'
|
||||
module ($provide)->
|
||||
$provide.value "CurrentHub", CurrentHubMock
|
||||
$provide.value "currentOrder", currentOrder
|
||||
null
|
||||
inject ($injector, _$httpBackend_)->
|
||||
Products = $injector.get("Products")
|
||||
Enterprises = $injector.get("Enterprises")
|
||||
Variants = $injector.get("Variants")
|
||||
$httpBackend = _$httpBackend_
|
||||
|
||||
it "Fetches products from the backend on init", ->
|
||||
@@ -31,6 +37,13 @@ describe 'Products service', ->
|
||||
$httpBackend.flush()
|
||||
expect(Products.products[0].supplier).toBe Enterprises.enterprises_by_id["9"]
|
||||
|
||||
it "registers variants with the Variants service", ->
|
||||
spyOn(Variants, "register")
|
||||
product.variants = [{id: 1}]
|
||||
$httpBackend.expectGET("/shop/products").respond([product])
|
||||
$httpBackend.flush()
|
||||
expect(Variants.register).toHaveBeenCalledWith product.variants[0]
|
||||
|
||||
describe "determining the price to display for a product", ->
|
||||
it "displays the product price when the product does not have variants", ->
|
||||
$httpBackend.expectGET("/shop/products").respond([product])
|
||||
@@ -42,3 +55,4 @@ describe 'Products service', ->
|
||||
$httpBackend.expectGET("/shop/products").respond([product])
|
||||
$httpBackend.flush()
|
||||
expect(Products.products[0].price).toEqual 22
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
describe 'Variants service', ->
|
||||
Variants = null
|
||||
variant =
|
||||
id: 1
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
inject ($injector)->
|
||||
Variants = $injector.get("Variants")
|
||||
|
||||
it "will create a new variant", ->
|
||||
expect(Variants.register(variant)).toBe variant
|
||||
|
||||
it "will return an existing variant rather than duplicating", ->
|
||||
Variants.register(variant)
|
||||
expect(Variants.register({id: 1})).toBe variant
|
||||
Reference in New Issue
Block a user