mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-12 03:50:22 +00:00
WIP: More work on panel content, styling and data submission logic
This commit is contained in:
@@ -4,14 +4,12 @@ describe "EnterprisesCtrl", ->
|
||||
Enterprises = null
|
||||
|
||||
beforeEach ->
|
||||
shops = "list of shops"
|
||||
|
||||
module('admin.enterprises')
|
||||
inject ($controller, $rootScope, _Enterprises_) ->
|
||||
scope = $rootScope
|
||||
Enterprises = _Enterprises_
|
||||
spyOn(Enterprises, "index").andReturn "list of enterprises"
|
||||
ctrl = $controller 'enterprisesCtrl', {$scope: scope, Enterprises: Enterprises, shops: shops}
|
||||
ctrl = $controller 'enterprisesCtrl', {$scope: scope, Enterprises: Enterprises}
|
||||
|
||||
describe "setting the shop on scope", ->
|
||||
it "calls Enterprises#index with the correct params", ->
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
describe "indexPanelCtrl", ->
|
||||
ctrl = null
|
||||
scope = null
|
||||
Enterprises = null
|
||||
|
||||
beforeEach ->
|
||||
module('admin.enterprises')
|
||||
inject ($controller, $rootScope, _Enterprises_) ->
|
||||
scope = $rootScope.$new()
|
||||
$rootScope.object = { some: "object" }
|
||||
Enterprises = _Enterprises_
|
||||
ctrl = $controller 'indexPanelCtrl', {$scope: scope, Enterprises: Enterprises}
|
||||
|
||||
describe "initialisation", ->
|
||||
it "pulls object from the parent scope and points the 'enterprise' on the current scope to it", inject ($rootScope) ->
|
||||
expect(scope.enterprise).toBe $rootScope.object
|
||||
|
||||
describe "saving changes on an enterprise", ->
|
||||
describe "when changes have been made", ->
|
||||
deferred = null
|
||||
|
||||
beforeEach inject ($q) ->
|
||||
spyOn(scope, "saved").andReturn false
|
||||
deferred = $q.defer()
|
||||
spyOn(Enterprises, "save").andReturn(deferred.promise)
|
||||
scope.save()
|
||||
|
||||
it "sets scope.saving to true", ->
|
||||
expect(scope.saving).toBe true
|
||||
|
||||
describe "when the save is successful", ->
|
||||
beforeEach inject ($rootScope) ->
|
||||
deferred.resolve()
|
||||
$rootScope.$digest()
|
||||
|
||||
|
||||
it "sets scope.saving to false", ->
|
||||
expect(scope.saving).toBe false
|
||||
|
||||
describe "when the save is unsuccessful", ->
|
||||
beforeEach inject ($rootScope) ->
|
||||
deferred.reject({ status: 404 })
|
||||
$rootScope.$digest()
|
||||
|
||||
it "sets scope.saving to false", ->
|
||||
expect(scope.saving).toBe false
|
||||
@@ -8,12 +8,13 @@ describe "Enterprises service", ->
|
||||
Enterprises = _Enterprises_
|
||||
EnterpriseResource = _EnterpriseResource_
|
||||
$httpBackend = _$httpBackend_
|
||||
$httpBackend.expectGET('/admin/enterprises.json').respond 200, [{ id: 5, name: 'Enterprise 1'}]
|
||||
|
||||
|
||||
describe "#index", ->
|
||||
result = null
|
||||
|
||||
beforeEach ->
|
||||
$httpBackend.expectGET('/admin/enterprises.json').respond 200, [{ id: 5, name: 'Enterprise 1'}]
|
||||
expect(Enterprises.loaded).toBe false
|
||||
result = Enterprises.index()
|
||||
$httpBackend.flush()
|
||||
@@ -29,3 +30,76 @@ describe "Enterprises service", ->
|
||||
|
||||
it "sets @loaded to true", ->
|
||||
expect(Enterprises.loaded).toBe true
|
||||
|
||||
|
||||
describe "#save", ->
|
||||
result = null
|
||||
|
||||
describe "success", ->
|
||||
enterprise = null
|
||||
resolved = false
|
||||
|
||||
beforeEach ->
|
||||
enterprise = new EnterpriseResource( { id: 15, permalink: 'enterprise1', name: 'Enterprise 1' } )
|
||||
$httpBackend.expectPUT('/admin/enterprises/enterprise1.json').respond 200, { id: 15, name: 'Enterprise 1'}
|
||||
Enterprises.save(enterprise).then( -> resolved = true)
|
||||
$httpBackend.flush()
|
||||
|
||||
it "updates the pristine copy of the enterprise", ->
|
||||
# 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(Enterprises.pristine_by_id[15]).toEqual enterprise
|
||||
|
||||
it "resolves the promise", ->
|
||||
expect(resolved).toBe(true);
|
||||
|
||||
|
||||
describe "failure", ->
|
||||
enterprise = null
|
||||
rejected = false
|
||||
|
||||
beforeEach ->
|
||||
enterprise = new EnterpriseResource( { id: 15, permalink: 'permalink', name: 'Enterprise 1' } )
|
||||
$httpBackend.expectPUT('/admin/enterprises/permalink.json').respond 422, { error: 'obj' }
|
||||
Enterprises.save(enterprise).catch( -> rejected = true)
|
||||
$httpBackend.flush()
|
||||
|
||||
it "does not update the pristine copy of the enterprise", ->
|
||||
expect(Enterprises.pristine_by_id[15]).toBeUndefined()
|
||||
|
||||
it "rejects the promise", ->
|
||||
expect(rejected).toBe(true);
|
||||
|
||||
describe "#saved", ->
|
||||
describe "when attributes of the object have been altered", ->
|
||||
beforeEach ->
|
||||
spyOn(Enterprises, "diff").andReturn ["attr1", "attr2"]
|
||||
|
||||
it "returns false", ->
|
||||
expect(Enterprises.saved({})).toBe false
|
||||
|
||||
describe "when attributes of the object have not been altered", ->
|
||||
beforeEach ->
|
||||
spyOn(Enterprises, "diff").andReturn []
|
||||
|
||||
it "returns false", ->
|
||||
expect(Enterprises.saved({})).toBe true
|
||||
|
||||
|
||||
describe "diff", ->
|
||||
beforeEach ->
|
||||
Enterprises.pristine_by_id = { 23: { id: 23, name: "ent1", is_primary_producer: true } }
|
||||
|
||||
it "returns a list of properties that have been altered", ->
|
||||
expect(Enterprises.diff({ id: 23, name: "enterprise123", is_primary_producer: true })).toEqual ["name"]
|
||||
|
||||
|
||||
describe "resetAttribute", ->
|
||||
enterprise = { id: 23, name: "ent1", is_primary_producer: true }
|
||||
|
||||
beforeEach ->
|
||||
Enterprises.pristine_by_id = { 23: { id: 23, name: "enterprise1", is_primary_producer: true } }
|
||||
|
||||
it "resets the specified value according to the pristine record", ->
|
||||
Enterprises.resetAttribute(enterprise, "name")
|
||||
expect(enterprise.name).toEqual "enterprise1"
|
||||
|
||||
Reference in New Issue
Block a user