mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
Merge branch 'master' into redesign
Conflicts: app/controllers/home_controller.rb app/views/producers/index.html.haml app/views/shared/menu/_mobile_menu.html.haml app/views/shop/products/_form.html.haml config/routes.rb spec/controllers/shops_controller_spec.rb
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
//= require angularjs-file-upload
|
||||
//= require lodash.underscore.js
|
||||
//= require angular-flash.min.js
|
||||
//= require shared/ng-tags-input.min.js
|
||||
//= require shared/mm-foundation-tpls-0.2.2.min.js
|
||||
//= require textAngular.min.js
|
||||
//= require textAngular-sanitize.min.js
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
describe "CustomersCtrl", ->
|
||||
ctrl = null
|
||||
scope = null
|
||||
Customers = null
|
||||
|
||||
beforeEach ->
|
||||
shops = "list of shops"
|
||||
|
||||
module('admin.customers')
|
||||
inject ($controller, $rootScope, _Customers_) ->
|
||||
scope = $rootScope
|
||||
Customers = _Customers_
|
||||
ctrl = $controller 'customersCtrl', {$scope: scope, Customers: Customers, shops: shops}
|
||||
|
||||
describe "setting the shop on scope", ->
|
||||
beforeEach ->
|
||||
spyOn(Customers, "index").andReturn "list of customers"
|
||||
scope.$apply ->
|
||||
scope.shop = {id: 1}
|
||||
|
||||
it "calls Customers#index with the correct params", ->
|
||||
expect(Customers.index).toHaveBeenCalledWith({enterprise_id: 1})
|
||||
|
||||
it "resets $scope.customers with the result of Customers#index", ->
|
||||
expect(scope.customers).toEqual "list of customers"
|
||||
@@ -0,0 +1,31 @@
|
||||
describe "Customers service", ->
|
||||
Customers = CustomerResource = customers = $httpBackend = null
|
||||
|
||||
beforeEach ->
|
||||
module 'admin.customers'
|
||||
|
||||
inject ($q, _$httpBackend_, _Customers_, _CustomerResource_) ->
|
||||
Customers = _Customers_
|
||||
CustomerResource = _CustomerResource_
|
||||
$httpBackend = _$httpBackend_
|
||||
$httpBackend.expectGET('/admin/customers.json?enterprise_id=2').respond 200, [{ id: 5, email: 'someone@email.com'}]
|
||||
|
||||
describe "#index", ->
|
||||
result = null
|
||||
|
||||
beforeEach ->
|
||||
expect(Customers.loaded).toBe false
|
||||
result = Customers.index(enterprise_id: 2)
|
||||
$httpBackend.flush()
|
||||
|
||||
it "stores returned data in @customers, with ids as keys", ->
|
||||
# This is super weird and freaking annoying. I think resource results have extra
|
||||
# properties ($then, $promise) that cause them to not be equal to the reponse object
|
||||
# provided to the expectGET clause above.
|
||||
expect(Customers.customers).toEqual [ new CustomerResource({ id: 5, email: 'someone@email.com'}) ]
|
||||
|
||||
it "returns @customers", ->
|
||||
expect(result).toEqual Customers.customers
|
||||
|
||||
it "sets @loaded to true", ->
|
||||
expect(Customers.loaded).toBe true
|
||||
@@ -0,0 +1,17 @@
|
||||
describe "ColumnsCtrl", ->
|
||||
ctrl = null
|
||||
scope = null
|
||||
Columns = null
|
||||
|
||||
beforeEach ->
|
||||
Columns = { columns: { name: { visible: true} } }
|
||||
|
||||
module('admin.indexUtils')
|
||||
inject ($controller, $rootScope) ->
|
||||
scope = $rootScope
|
||||
ctrl = $controller 'ColumnsCtrl', {$scope: scope, Columns: Columns}
|
||||
|
||||
it "initialises data", ->
|
||||
expect(scope.columns).toEqual Columns.columns
|
||||
expect(scope.predicate).toEqual ""
|
||||
expect(scope.reverse).toEqual false
|
||||
@@ -0,0 +1,15 @@
|
||||
describe "Columns service", ->
|
||||
Columns = null
|
||||
|
||||
beforeEach ->
|
||||
module 'admin.indexUtils'
|
||||
|
||||
inject (_Columns_) ->
|
||||
Columns = _Columns_
|
||||
|
||||
Columns.columns = ["something"]
|
||||
|
||||
describe "setting columns", ->
|
||||
it "sets resets @columns and copies each column of the provided object across", ->
|
||||
Columns.setColumns({ name: { visible: true } })
|
||||
expect(Columns.columns).toEqual { name: { visible: true } }
|
||||
@@ -0,0 +1,149 @@
|
||||
describe "Pending Changes", ->
|
||||
resourcesMock = pendingChanges = null
|
||||
|
||||
beforeEach ->
|
||||
|
||||
resourcesMock =
|
||||
update: jasmine.createSpy('update').andCallFake (change) ->
|
||||
$promise:
|
||||
then: (successFn, errorFn) ->
|
||||
return successFn({propertyName: "new_value"}) if change.success
|
||||
errorFn("error")
|
||||
|
||||
module 'admin.indexUtils', ($provide) ->
|
||||
$provide.value 'resources', resourcesMock
|
||||
return
|
||||
|
||||
inject (_pendingChanges_) ->
|
||||
pendingChanges = _pendingChanges_
|
||||
|
||||
|
||||
describe "adding a new change", ->
|
||||
it "adds a new object with key of id if it does not already exist", ->
|
||||
expect(pendingChanges.pendingChanges).toEqual {}
|
||||
expect(pendingChanges.pendingChanges["1"]).not.toBeDefined()
|
||||
pendingChanges.add 1, "propertyName", { a: 1 }
|
||||
expect(pendingChanges.pendingChanges["1"]).toBeDefined()
|
||||
|
||||
it "adds a new object with key of the altered attribute name if it does not already exist", ->
|
||||
pendingChanges.add 1, "propertyName", { a: 1 }
|
||||
expect(pendingChanges.pendingChanges["1"]).toBeDefined()
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName"]).toEqual { a: 1 }
|
||||
|
||||
it "replaces the existing object when adding a change to an attribute which already exists", ->
|
||||
pendingChanges.add 1, "propertyName", { a: 1 }
|
||||
expect(pendingChanges.pendingChanges["1"]).toBeDefined()
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName"]).toEqual { a: 1 }
|
||||
pendingChanges.add 1, "propertyName", { b: 2 }
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName"]).toEqual { b: 2 }
|
||||
|
||||
it "adds an attribute to key to a line item object when one already exists", ->
|
||||
pendingChanges.add 1, "propertyName1", { a: 1 }
|
||||
pendingChanges.add 1, "propertyName2", { b: 2 }
|
||||
expect(pendingChanges.pendingChanges["1"]).toEqual { propertyName1: { a: 1}, propertyName2: { b: 2 } }
|
||||
|
||||
describe "removing all existing changes", ->
|
||||
it "resets pendingChanges object", ->
|
||||
pendingChanges.pendingChanges = { 1: { "propertyName1": { a: 1 }, "propertyName2": { b: 2 } } }
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName1"]).toBeDefined()
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName2"]).toBeDefined()
|
||||
pendingChanges.removeAll()
|
||||
expect(pendingChanges.pendingChanges["1"]).not.toBeDefined()
|
||||
expect(pendingChanges.pendingChanges).toEqual {}
|
||||
|
||||
describe "removing an existing change", ->
|
||||
it "deletes a change if it exists", ->
|
||||
pendingChanges.pendingChanges = { 1: { "propertyName1": { a: 1 }, "propertyName2": { b: 2 } } }
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName1"]).toBeDefined()
|
||||
pendingChanges.remove 1, "propertyName1"
|
||||
expect(pendingChanges.pendingChanges["1"]).toBeDefined()
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName1"]).not.toBeDefined()
|
||||
|
||||
it "deletes a line item object if it is empty", ->
|
||||
pendingChanges.pendingChanges = { 1: { "propertyName1": { a: 1 } } }
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName1"]).toBeDefined()
|
||||
pendingChanges.remove 1, "propertyName1"
|
||||
expect(pendingChanges.pendingChanges["1"]).not.toBeDefined()
|
||||
|
||||
it "does nothing if key with specified attribute does not exist", ->
|
||||
pendingChanges.pendingChanges = { 1: { "propertyName1": { a: 1 } } }
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName1"]).toBeDefined()
|
||||
pendingChanges.remove 1, "propertyName2"
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName1"]).toEqual { a: 1 }
|
||||
|
||||
it "does nothing if key with specified id does not exist", ->
|
||||
pendingChanges.pendingChanges = { 1: { "propertyName1": { a: 1 } } }
|
||||
expect(pendingChanges.pendingChanges["1"]["propertyName1"]).toBeDefined()
|
||||
pendingChanges.remove 2, "propertyName1"
|
||||
expect(pendingChanges.pendingChanges["1"]).toEqual { "propertyName1": { a: 1 } }
|
||||
|
||||
describe "submitting an individual change to the server", ->
|
||||
change = null
|
||||
beforeEach ->
|
||||
object = {id: 1}
|
||||
scope = { reset: jasmine.createSpy('reset'), success: jasmine.createSpy('success'), error: jasmine.createSpy('error') };
|
||||
attr = "propertyName"
|
||||
change = { object: object, scope: scope, attr: attr }
|
||||
|
||||
|
||||
it "sends the correct object to dataSubmitter", ->
|
||||
pendingChanges.submit change
|
||||
expect(resourcesMock.update.calls.length).toEqual 1
|
||||
expect(resourcesMock.update).toHaveBeenCalledWith change
|
||||
|
||||
describe "successful request", ->
|
||||
beforeEach ->
|
||||
change.success = true
|
||||
|
||||
it "calls remove with id and attribute name", ->
|
||||
spyOn(pendingChanges, "remove").andCallFake(->)
|
||||
pendingChanges.submit change
|
||||
expect(pendingChanges.remove.calls.length).toEqual 1
|
||||
expect(pendingChanges.remove).toHaveBeenCalledWith 1, "propertyName"
|
||||
|
||||
it "calls reset on the relevant scope", ->
|
||||
pendingChanges.submit change
|
||||
expect(change.scope.reset).toHaveBeenCalledWith "new_value"
|
||||
|
||||
it "calls success on the relevant scope", ->
|
||||
pendingChanges.submit change
|
||||
expect(change.scope.success).toHaveBeenCalled()
|
||||
|
||||
describe "unsuccessful request", ->
|
||||
beforeEach ->
|
||||
change.success = false
|
||||
|
||||
it "does not call remove", ->
|
||||
spyOn(pendingChanges, "remove").andCallFake(->)
|
||||
pendingChanges.submit change
|
||||
expect(pendingChanges.remove).not.toHaveBeenCalled()
|
||||
|
||||
it "does not call reset on the relevant scope", ->
|
||||
pendingChanges.submit change
|
||||
expect(change.scope.reset).not.toHaveBeenCalled()
|
||||
|
||||
it "calls error on the relevant scope", ->
|
||||
pendingChanges.submit change
|
||||
expect(change.scope.error).toHaveBeenCalled()
|
||||
|
||||
describe "cycling through all changes to submit to server", ->
|
||||
it "sends the correct object to dataSubmitter", ->
|
||||
spyOn(pendingChanges, "submit").andCallFake(->)
|
||||
pendingChanges.pendingChanges =
|
||||
1: { "prop1": { attr: "prop1", value: 1 }, "prop2": { attr: "prop2", value: 2 } }
|
||||
2: { "prop1": { attr: "prop1", value: 2 }, "prop2": { attr: "prop2", value: 4 } }
|
||||
7: { "prop2": { attr: "prop2", value: 5 } }
|
||||
pendingChanges.submitAll()
|
||||
expect(pendingChanges.submit.calls.length).toEqual 5
|
||||
expect(pendingChanges.submit).toHaveBeenCalledWith { attr: "prop1", value: 1 }
|
||||
expect(pendingChanges.submit).toHaveBeenCalledWith { attr: "prop2", value: 2 }
|
||||
expect(pendingChanges.submit).toHaveBeenCalledWith { attr: "prop1", value: 2 }
|
||||
expect(pendingChanges.submit).toHaveBeenCalledWith { attr: "prop2", value: 4 }
|
||||
expect(pendingChanges.submit).toHaveBeenCalledWith { attr: "prop2", value: 5 }
|
||||
|
||||
it "returns an array of promises representing all sumbit requests", ->
|
||||
spyOn(pendingChanges, "submit").andCallFake (change) -> change.value
|
||||
pendingChanges.pendingChanges =
|
||||
1: { "prop1": { attr: "prop1", value: 1 } }
|
||||
2: { "prop1": { attr: "prop1", value: 2 }, "prop2": { attr: "prop1", value: 4 } }
|
||||
expect(pendingChanges.submitAll()).toEqual [ 1, 2, 4 ]
|
||||
@@ -0,0 +1,52 @@
|
||||
describe "switchClass service", ->
|
||||
elementMock = timeoutMock = {}
|
||||
removeClass = addClass = switchClassService = null
|
||||
|
||||
beforeEach ->
|
||||
addClass = jasmine.createSpy('addClass')
|
||||
removeClass = jasmine.createSpy('removeClass')
|
||||
elementMock =
|
||||
addClass: addClass
|
||||
removeClass: removeClass
|
||||
timeoutMock = jasmine.createSpy('timeout').andReturn "new timeout"
|
||||
timeoutMock.cancel = jasmine.createSpy('timeout.cancel')
|
||||
|
||||
beforeEach ->
|
||||
module "ofn.admin" , ($provide) ->
|
||||
$provide.value '$timeout', timeoutMock
|
||||
return
|
||||
|
||||
beforeEach inject (switchClass) ->
|
||||
switchClassService = switchClass
|
||||
|
||||
it "calls addClass on the element once", ->
|
||||
switchClassService elementMock, "addClass", [], false
|
||||
expect(addClass).toHaveBeenCalledWith "addClass"
|
||||
expect(addClass.calls.length).toEqual 1
|
||||
|
||||
it "calls removeClass on the element for ", ->
|
||||
switchClassService elementMock, "", ["remClass1", "remClass2", "remClass3"], false
|
||||
expect(removeClass).toHaveBeenCalledWith "remClass1"
|
||||
expect(removeClass).toHaveBeenCalledWith "remClass2"
|
||||
expect(removeClass).toHaveBeenCalledWith "remClass3"
|
||||
expect(removeClass.calls.length).toEqual 3
|
||||
|
||||
it "call cancel on element.timout only if it exists", ->
|
||||
switchClassService elementMock, "", [], false
|
||||
expect(timeoutMock.cancel).not.toHaveBeenCalled()
|
||||
elementMock.timeout = true
|
||||
switchClassService elementMock, "", [], false
|
||||
expect(timeoutMock.cancel).toHaveBeenCalled()
|
||||
|
||||
it "doesn't set up a new timeout if 'timeout' is false", ->
|
||||
switchClassService elementMock, "class1", ["class2"], false
|
||||
expect(timeoutMock).not.toHaveBeenCalled()
|
||||
|
||||
it "doesn't set up a new timeout if 'timeout' is a string", ->
|
||||
switchClassService elementMock, "class1", ["class2"], "string"
|
||||
expect(timeoutMock).not.toHaveBeenCalled()
|
||||
|
||||
it "sets up a new timeout if 'timeout' parameter is an integer", ->
|
||||
switchClassService elementMock, "class1", ["class2"], 1000
|
||||
expect(timeoutMock).toHaveBeenCalled()
|
||||
expect(elementMock.timeout).toEqual "new timeout"
|
||||
@@ -376,236 +376,6 @@ describe "AdminOrderMgmtCtrl", ->
|
||||
sp = scope.filteredLineItems[0].price
|
||||
expect(scope.weightAdjustedPrice(scope.filteredLineItems[0], old_value)).toEqual sp
|
||||
|
||||
|
||||
describe "managing pending changes", ->
|
||||
dataSubmitter = pendingChangesService = null
|
||||
|
||||
beforeEach ->
|
||||
dataSubmitter = jasmine.createSpy('dataSubmitter').andReturn {
|
||||
then: (thenFn) ->
|
||||
thenFn({propertyName: "new_value"})
|
||||
}
|
||||
|
||||
beforeEach ->
|
||||
module "ofn.admin", ($provide) ->
|
||||
$provide.value 'dataSubmitter', dataSubmitter
|
||||
return
|
||||
|
||||
beforeEach inject (pendingChanges) ->
|
||||
pendingChangesService = pendingChanges
|
||||
|
||||
describe "adding a new change", ->
|
||||
it "adds a new object with key of id if it does not already exist", ->
|
||||
expect(pendingChangesService.pendingChanges).toEqual {}
|
||||
expect(pendingChangesService.pendingChanges["1"]).not.toBeDefined()
|
||||
pendingChangesService.add 1, "propertyName", { a: 1 }
|
||||
expect(pendingChangesService.pendingChanges["1"]).toBeDefined()
|
||||
|
||||
it "adds a new object with key of the altered attribute name if it does not already exist", ->
|
||||
pendingChangesService.add 1, "propertyName", { a: 1 }
|
||||
expect(pendingChangesService.pendingChanges["1"]).toBeDefined()
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName"]).toEqual { a: 1 }
|
||||
|
||||
it "replaces the existing object when adding a change to an attribute which already exists", ->
|
||||
pendingChangesService.add 1, "propertyName", { a: 1 }
|
||||
expect(pendingChangesService.pendingChanges["1"]).toBeDefined()
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName"]).toEqual { a: 1 }
|
||||
pendingChangesService.add 1, "propertyName", { b: 2 }
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName"]).toEqual { b: 2 }
|
||||
|
||||
it "adds an attribute to key to a line item object when one already exists", ->
|
||||
pendingChangesService.add 1, "propertyName1", { a: 1 }
|
||||
pendingChangesService.add 1, "propertyName2", { b: 2 }
|
||||
expect(pendingChangesService.pendingChanges["1"]).toEqual { propertyName1: { a: 1}, propertyName2: { b: 2 } }
|
||||
|
||||
describe "removing all existing changes", ->
|
||||
it "resets pendingChanges object", ->
|
||||
pendingChangesService.pendingChanges = { 1: { "propertyName1": { a: 1 }, "propertyName2": { b: 2 } } }
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName1"]).toBeDefined()
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName2"]).toBeDefined()
|
||||
pendingChangesService.removeAll()
|
||||
expect(pendingChangesService.pendingChanges["1"]).not.toBeDefined()
|
||||
expect(pendingChangesService.pendingChanges).toEqual {}
|
||||
|
||||
describe "removing an existing change", ->
|
||||
it "deletes a change if it exists", ->
|
||||
pendingChangesService.pendingChanges = { 1: { "propertyName1": { a: 1 }, "propertyName2": { b: 2 } } }
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName1"]).toBeDefined()
|
||||
pendingChangesService.remove 1, "propertyName1"
|
||||
expect(pendingChangesService.pendingChanges["1"]).toBeDefined()
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName1"]).not.toBeDefined()
|
||||
|
||||
it "deletes a line item object if it is empty", ->
|
||||
pendingChangesService.pendingChanges = { 1: { "propertyName1": { a: 1 } } }
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName1"]).toBeDefined()
|
||||
pendingChangesService.remove 1, "propertyName1"
|
||||
expect(pendingChangesService.pendingChanges["1"]).not.toBeDefined()
|
||||
|
||||
it "does nothing if key with specified attribute does not exist", ->
|
||||
pendingChangesService.pendingChanges = { 1: { "propertyName1": { a: 1 } } }
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName1"]).toBeDefined()
|
||||
pendingChangesService.remove 1, "propertyName2"
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName1"]).toEqual { a: 1 }
|
||||
|
||||
it "does nothing if key with specified id does not exist", ->
|
||||
pendingChangesService.pendingChanges = { 1: { "propertyName1": { a: 1 } } }
|
||||
expect(pendingChangesService.pendingChanges["1"]["propertyName1"]).toBeDefined()
|
||||
pendingChangesService.remove 2, "propertyName1"
|
||||
expect(pendingChangesService.pendingChanges["1"]).toEqual { "propertyName1": { a: 1 } }
|
||||
|
||||
describe "submitting an individual change to the server", ->
|
||||
it "sends the correct object to dataSubmitter", ->
|
||||
changeObj = { element: {} }
|
||||
pendingChangesService.submit 1, "propertyName", changeObj
|
||||
expect(dataSubmitter.calls.length).toEqual 1
|
||||
expect(dataSubmitter).toHaveBeenCalledWith changeObj
|
||||
|
||||
it "calls remove with id and attribute name", ->
|
||||
changeObj = { element: {} }
|
||||
spyOn(pendingChangesService, "remove").andCallFake(->)
|
||||
pendingChangesService.submit 1, "propertyName", changeObj
|
||||
expect(pendingChangesService.remove.calls.length).toEqual 1
|
||||
expect(pendingChangesService.remove).toHaveBeenCalledWith 1, "propertyName"
|
||||
|
||||
it "resets the dbValue attribute of the element in question", ->
|
||||
element = { dbValue: 2 }
|
||||
changeObj = { element: element }
|
||||
pendingChangesService.submit 1, "propertyName", changeObj
|
||||
expect(element.dbValue).toEqual "new_value"
|
||||
|
||||
describe "cycling through all changes to submit to server", ->
|
||||
it "sends the correct object to dataSubmitter", ->
|
||||
spyOn(pendingChangesService, "submit").andCallFake(->)
|
||||
pendingChangesService.pendingChanges =
|
||||
1: { "prop1": 1, "prop2": 2 }
|
||||
2: { "prop1": 2, "prop2": 4 }
|
||||
7: { "prop2": 5 }
|
||||
pendingChangesService.submitAll()
|
||||
expect(pendingChangesService.submit.calls.length).toEqual 5
|
||||
expect(pendingChangesService.submit).toHaveBeenCalledWith '1', "prop1", 1
|
||||
expect(pendingChangesService.submit).toHaveBeenCalledWith '1', "prop2", 2
|
||||
expect(pendingChangesService.submit).toHaveBeenCalledWith '2', "prop1", 2
|
||||
expect(pendingChangesService.submit).toHaveBeenCalledWith '2', "prop2", 4
|
||||
expect(pendingChangesService.submit).toHaveBeenCalledWith '7', "prop2", 5
|
||||
|
||||
it "returns an array of promises representing all sumbit requests", ->
|
||||
spyOn(pendingChangesService, "submit").andCallFake (id,attrName,changeObj) ->
|
||||
id
|
||||
pendingChangesService.pendingChanges =
|
||||
1: { "prop1": 1 }
|
||||
2: { "prop1": 2, "prop2": 4 }
|
||||
expect(pendingChangesService.submitAll()).toEqual [ '1','2','2' ]
|
||||
|
||||
describe "dataSubmitter service", ->
|
||||
qMock = httpMock = {}
|
||||
switchClassSpy = resolveSpy = rejectSpy = dataSubmitterService = null
|
||||
|
||||
beforeEach ->
|
||||
resolveSpy = jasmine.createSpy('resolve')
|
||||
rejectSpy = jasmine.createSpy('reject')
|
||||
qMock.defer = ->
|
||||
resolve: resolveSpy
|
||||
reject: rejectSpy
|
||||
promise: "promise1"
|
||||
|
||||
# Can't use httpBackend because the qMock interferes with it
|
||||
httpMock.put = (url) ->
|
||||
success: (successFn) ->
|
||||
successFn("somedata") if url == "successURL"
|
||||
error: (errorFn) ->
|
||||
errorFn() if url == "errorURL"
|
||||
|
||||
spyOn(httpMock, "put").andCallThrough()
|
||||
spyOn(qMock, "defer").andCallThrough()
|
||||
|
||||
switchClassSpy = jasmine.createSpy('switchClass')
|
||||
|
||||
beforeEach ->
|
||||
module "ofn.admin" , ($provide) ->
|
||||
$provide.value '$q', qMock
|
||||
$provide.value '$http', httpMock
|
||||
$provide.value 'switchClass', switchClassSpy
|
||||
return
|
||||
|
||||
beforeEach inject (dataSubmitter) ->
|
||||
dataSubmitterService = dataSubmitter
|
||||
|
||||
it "returns a promise", ->
|
||||
expect(dataSubmitterService( { url: "successURL" } )).toEqual "promise1"
|
||||
expect(qMock.defer).toHaveBeenCalled()
|
||||
|
||||
it "sends a PUT request with the url property of changeObj", ->
|
||||
dataSubmitterService { url: "successURL" }
|
||||
expect(httpMock.put).toHaveBeenCalledWith "successURL"
|
||||
|
||||
it "calls resolve on deferred object when request is successful", ->
|
||||
element = { a: 1 }
|
||||
dataSubmitterService { url: "successURL", element: element }
|
||||
expect(resolveSpy.calls.length).toEqual 1
|
||||
expect(rejectSpy.calls.length).toEqual 0
|
||||
expect(resolveSpy).toHaveBeenCalledWith "somedata"
|
||||
expect(switchClassSpy).toHaveBeenCalledWith element, "update-success", ["update-pending", "update-error"], 3000
|
||||
|
||||
it "calls reject on deferred object when request is erroneous", ->
|
||||
element = { b: 2 }
|
||||
dataSubmitterService { url: "errorURL", element: element }
|
||||
expect(resolveSpy.calls.length).toEqual 0
|
||||
expect(rejectSpy.calls.length).toEqual 1
|
||||
expect(switchClassSpy).toHaveBeenCalledWith element, "update-error", ["update-pending", "update-success"], false
|
||||
|
||||
describe "switchClass service", ->
|
||||
elementMock = timeoutMock = {}
|
||||
removeClass = addClass = switchClassService = null
|
||||
|
||||
beforeEach ->
|
||||
addClass = jasmine.createSpy('addClass')
|
||||
removeClass = jasmine.createSpy('removeClass')
|
||||
elementMock =
|
||||
addClass: addClass
|
||||
removeClass: removeClass
|
||||
timeoutMock = jasmine.createSpy('timeout').andReturn "new timeout"
|
||||
timeoutMock.cancel = jasmine.createSpy('timeout.cancel')
|
||||
|
||||
beforeEach ->
|
||||
module "ofn.admin" , ($provide) ->
|
||||
$provide.value '$timeout', timeoutMock
|
||||
return
|
||||
|
||||
beforeEach inject (switchClass) ->
|
||||
switchClassService = switchClass
|
||||
|
||||
it "calls addClass on the element once", ->
|
||||
switchClassService elementMock, "addClass", [], false
|
||||
expect(addClass).toHaveBeenCalledWith "addClass"
|
||||
expect(addClass.calls.length).toEqual 1
|
||||
|
||||
it "calls removeClass on the element for ", ->
|
||||
switchClassService elementMock, "", ["remClass1", "remClass2", "remClass3"], false
|
||||
expect(removeClass).toHaveBeenCalledWith "remClass1"
|
||||
expect(removeClass).toHaveBeenCalledWith "remClass2"
|
||||
expect(removeClass).toHaveBeenCalledWith "remClass3"
|
||||
expect(removeClass.calls.length).toEqual 3
|
||||
|
||||
it "call cancel on element.timout only if it exists", ->
|
||||
switchClassService elementMock, "", [], false
|
||||
expect(timeoutMock.cancel).not.toHaveBeenCalled()
|
||||
elementMock.timeout = true
|
||||
switchClassService elementMock, "", [], false
|
||||
expect(timeoutMock.cancel).toHaveBeenCalled()
|
||||
|
||||
it "doesn't set up a new timeout if 'timeout' is false", ->
|
||||
switchClassService elementMock, "class1", ["class2"], false
|
||||
expect(timeoutMock).not.toHaveBeenCalled()
|
||||
|
||||
it "doesn't set up a new timeout if 'timeout' is a string", ->
|
||||
switchClassService elementMock, "class1", ["class2"], "string"
|
||||
expect(timeoutMock).not.toHaveBeenCalled()
|
||||
|
||||
it "sets up a new timeout if 'timeout' parameter is an integer", ->
|
||||
switchClassService elementMock, "class1", ["class2"], 1000
|
||||
expect(timeoutMock).toHaveBeenCalled()
|
||||
expect(elementMock.timeout).toEqual "new timeout"
|
||||
|
||||
describe "Auxiliary functions", ->
|
||||
describe "getting a zero filled two digit number", ->
|
||||
it "returns the number as a string if its value is greater than or equal to 10", ->
|
||||
|
||||
Reference in New Issue
Block a user