mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-07 22:46:06 +00:00
WIP: BOM Refactor, building order fetch and update logic with ngResource, updating rails routes
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
describe "Orders service", ->
|
||||
Orders = OrderResource = orders = $httpBackend = null
|
||||
|
||||
beforeEach ->
|
||||
module 'admin.orders'
|
||||
|
||||
this.addMatchers
|
||||
toDeepEqual: (expected) ->
|
||||
return angular.equals(this.actual, expected)
|
||||
|
||||
inject ($q, _$httpBackend_, _Orders_, _OrderResource_) ->
|
||||
Orders = _Orders_
|
||||
OrderResource = _OrderResource_
|
||||
$httpBackend = _$httpBackend_
|
||||
|
||||
describe "#index", ->
|
||||
result = response = null
|
||||
|
||||
beforeEach ->
|
||||
response = [{ id: 5, name: 'Order 1'}]
|
||||
$httpBackend.expectGET('/admin/orders.json').respond 200, response
|
||||
result = Orders.index()
|
||||
$httpBackend.flush()
|
||||
|
||||
it "stores returned data in @ordersByID, with ids as keys", ->
|
||||
# OrderResource returns instances of Resource rather than raw objects
|
||||
expect(Orders.ordersByID).toDeepEqual { 5: response[0] }
|
||||
|
||||
it "stores returned data in @pristineByID, with ids as keys", ->
|
||||
expect(Orders.pristineByID).toDeepEqual { 5: response[0] }
|
||||
|
||||
it "returns an array of orders", ->
|
||||
expect(result).toDeepEqual response
|
||||
|
||||
|
||||
describe "#save", ->
|
||||
result = null
|
||||
|
||||
describe "success", ->
|
||||
order = null
|
||||
resolved = false
|
||||
|
||||
beforeEach ->
|
||||
order = new OrderResource({ id: 15, permalink: 'order1', name: 'Order 1' })
|
||||
$httpBackend.expectPUT('/admin/orders/order1.json').respond 200, { id: 15, name: 'Order 1'}
|
||||
Orders.save(order).then( -> resolved = true)
|
||||
$httpBackend.flush()
|
||||
|
||||
it "updates the pristine copy of the order", ->
|
||||
# Resource results have extra properties ($then, $promise) that cause them to not
|
||||
# be exactly equal to the response object provided to the expectPUT clause above.
|
||||
expect(Orders.pristineByID[15]).toEqual order
|
||||
|
||||
it "resolves the promise", ->
|
||||
expect(resolved).toBe(true);
|
||||
|
||||
|
||||
describe "failure", ->
|
||||
order = null
|
||||
rejected = false
|
||||
|
||||
beforeEach ->
|
||||
order = new OrderResource( { id: 15, permalink: 'permalink', name: 'Order 1' } )
|
||||
$httpBackend.expectPUT('/admin/orders/permalink.json').respond 422, { error: 'obj' }
|
||||
Orders.save(order).catch( -> rejected = true)
|
||||
$httpBackend.flush()
|
||||
|
||||
it "does not update the pristine copy of the order", ->
|
||||
expect(Orders.pristineByID[15]).toBeUndefined()
|
||||
|
||||
it "rejects the promise", ->
|
||||
expect(rejected).toBe(true);
|
||||
|
||||
describe "#saved", ->
|
||||
describe "when attributes of the object have been altered", ->
|
||||
beforeEach ->
|
||||
spyOn(Orders, "diff").andReturn ["attr1", "attr2"]
|
||||
|
||||
it "returns false", ->
|
||||
expect(Orders.saved({})).toBe false
|
||||
|
||||
describe "when attributes of the object have not been altered", ->
|
||||
beforeEach ->
|
||||
spyOn(Orders, "diff").andReturn []
|
||||
|
||||
it "returns false", ->
|
||||
expect(Orders.saved({})).toBe true
|
||||
|
||||
|
||||
describe "diff", ->
|
||||
beforeEach ->
|
||||
Orders.pristineByID = { 23: { id: 23, name: "ent1", is_primary_producer: true } }
|
||||
|
||||
it "returns a list of properties that have been altered", ->
|
||||
expect(Orders.diff({ id: 23, name: "order123", is_primary_producer: true })).toEqual ["name"]
|
||||
|
||||
|
||||
describe "resetAttribute", ->
|
||||
order = { id: 23, name: "ent1", is_primary_producer: true }
|
||||
|
||||
beforeEach ->
|
||||
Orders.pristineByID = { 23: { id: 23, name: "order1", is_primary_producer: true } }
|
||||
|
||||
it "resets the specified value according to the pristine record", ->
|
||||
Orders.resetAttribute(order, "name")
|
||||
expect(order.name).toEqual "order1"
|
||||
Reference in New Issue
Block a user