Create and delete customers in admin interface

Removed Customers service and extended CustomerResource.
This commit is contained in:
Maikel Linke
2016-04-13 15:05:39 +10:00
parent ecd32819a6
commit 831df0b222
11 changed files with 134 additions and 73 deletions

View File

@@ -1,25 +1,49 @@
describe "CustomersCtrl", ->
ctrl = null
scope = null
Customers = null
http = null
beforeEach ->
shops = "list of shops"
module('admin.customers')
inject ($controller, $rootScope, _Customers_) ->
inject ($controller, $rootScope, _CustomerResource_, $httpBackend) ->
scope = $rootScope
Customers = _Customers_
ctrl = $controller 'customersCtrl', {$scope: scope, Customers: Customers, shops: shops}
http = $httpBackend
$controller 'customersCtrl', {$scope: scope, CustomerResource: _CustomerResource_, shops: {}}
this.addMatchers
toAngularEqual: (expected) ->
return angular.equals(this.actual, expected)
it "has no shop pre-selected", ->
expect(scope.shop).toEqual {}
describe "setting the shop on scope", ->
customer = { id: 5, email: 'someone@email.com'}
customers = [customer]
beforeEach ->
spyOn(Customers, "index").andReturn "list of customers"
http.expectGET('/admin/customers.json?enterprise_id=1').respond 200, customers
scope.$apply ->
scope.shop = {id: 1}
http.flush()
it "calls Customers#index with the correct params", ->
expect(Customers.index).toHaveBeenCalledWith({enterprise_id: 1})
it "retrievs the list of customers", ->
expect(scope.customers).toAngularEqual customers
it "resets $scope.customers with the result of Customers#index", ->
expect(scope.customers).toEqual "list of customers"
describe "scope.add", ->
it "creates a new customer", ->
email = "customer@example.org"
newCustomer = {id: 6, email: email}
customers.push(newCustomer)
http.expectPOST('/admin/customers.json?email=' + email + '&enterprise_id=1').respond 200, newCustomer
scope.add(email)
http.flush()
expect(scope.customers).toAngularEqual customers
describe "scope.deleteCustomer", ->
it "deletes a customer", ->
expect(scope.customers.length).toBe 2
customer = scope.customers[0]
http.expectDELETE('/admin/customers/' + customer.id + '.json').respond 200
scope.deleteCustomer(customer)
http.flush()
expect(scope.customers.length).toBe 1
expect(scope.customers[0]).not.toAngularEqual customer

View File

@@ -1,31 +0,0 @@
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