Adding customers controller, service and resource

This commit is contained in:
Rob Harrington
2015-05-07 15:13:21 +10:00
parent c00c93816c
commit 307302038a
7 changed files with 93 additions and 0 deletions

View File

@@ -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 "initialise()", ->
beforeEach ->
spyOn(Customers, "index").andReturn "list of customers"
scope.shop = {id: 1}
scope.initialise()
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"

View File

@@ -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 { 5: 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