From cc65faadd0f47bf0946fca8dce1ed269ee87e19c Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 12 Sep 2014 11:48:17 +1000 Subject: [PATCH] Specing out enterprise registration service properly --- .../enterprise_registration_service.js.coffee | 72 +++++++++---------- .../enterprise_registration_spec.js.coffee | 65 ++++++++++++++++- 2 files changed, 98 insertions(+), 39 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/enterprise_registration_service.js.coffee b/app/assets/javascripts/darkswarm/services/enterprise_registration_service.js.coffee index a248bc278b..68915193ee 100644 --- a/app/assets/javascripts/darkswarm/services/enterprise_registration_service.js.coffee +++ b/app/assets/javascripts/darkswarm/services/enterprise_registration_service.js.coffee @@ -11,49 +11,47 @@ Darkswarm.factory "EnterpriseRegistrationService", ($http, RegistrationService, @enterprise[key] = value create: => - # Loading.message = "Creating " + @enterprise.name - # $http( - # method: "POST" - # url: "/api/enterprises" - # data: - # enterprise: @prepare() - # params: - # token: spreeApiKey - # ).success((data) => - # Loading.clear() - # @enterprise.id = data - # RegistrationService.select('about') - # ).error((data) => - # Loading.clear() - # console.log angular.toJson(data) - # alert('Failed to create your enterprise.\nPlease ensure all fields are completely filled out.') - # ) - RegistrationService.select('about') + Loading.message = "Creating " + @enterprise.name + $http( + method: "POST" + url: "/api/enterprises" + data: + enterprise: @prepare() + params: + token: spreeApiKey + ).success((data) => + Loading.clear() + @enterprise.id = data + RegistrationService.select('about') + ).error((data) => + Loading.clear() + alert('Failed to create your enterprise.\nPlease ensure all fields are completely filled out.') + ) + # RegistrationService.select('about') update: (step) => - # Loading.message = "Updating " + @enterprise.name - # $http( - # method: "PUT" - # url: "/api/enterprises/#{@enterprise.id}" - # data: - # enterprise: @prepare() - # params: - # token: spreeApiKey - # ).success((data) -> - # Loading.clear() - # RegistrationService.select(step) - # ).error((data) -> - # Loading.clear() - # console.log angular.toJson(data) - # alert('Failed to update your enterprise.\nPlease ensure all fields are completely filled out.') - # ) - RegistrationService.select(step) + Loading.message = "Updating " + @enterprise.name + $http( + method: "PUT" + url: "/api/enterprises/#{@enterprise.id}" + data: + enterprise: @prepare() + params: + token: spreeApiKey + ).success((data) -> + Loading.clear() + RegistrationService.select(step) + ).error((data) -> + Loading.clear() + alert('Failed to update your enterprise.\nPlease ensure all fields are completely filled out.') + ) + # RegistrationService.select(step) prepare: => enterprise = {} excluded = [ 'address', 'country', 'id' ] for key, value of @enterprise when key not in excluded enterprise[key] = value - enterprise.address_attributes = @enterprise.address - enterprise.address_attributes.country_id = @enterprise.country.id + enterprise.address_attributes = @enterprise.address if @enterprise.address? + enterprise.address_attributes.country_id = @enterprise.country.id if @enterprise.country? enterprise \ No newline at end of file diff --git a/spec/javascripts/unit/darkswarm/services/enterprise_registration_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/enterprise_registration_spec.js.coffee index 121c4132a4..1852dbd16c 100644 --- a/spec/javascripts/unit/darkswarm/services/enterprise_registration_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/enterprise_registration_spec.js.coffee @@ -1,5 +1,6 @@ describe "EnterpriseRegistrationService", -> EnterpriseRegistrationService = null + $httpBackend = null availableCountries = [] enterpriseAttributes = name: "Enterprise 1" @@ -8,6 +9,8 @@ describe "EnterpriseRegistrationService", -> CurrentUser = id: 2 email: 'lalala@email.com' + RegistrationServiceMock = + select: -> null beforeEach -> module('Darkswarm') @@ -15,10 +18,68 @@ describe "EnterpriseRegistrationService", -> angular.module('Darkswarm').value 'enterpriseAttributes', enterpriseAttributes angular.module('Darkswarm').value 'spreeApiKey', spreeApiKey angular.module('Darkswarm').value 'CurrentUser', CurrentUser + angular.module('Darkswarm').value 'RegistrationService', RegistrationServiceMock - inject ($injector)-> + inject ($injector, _$httpBackend_) -> + $httpBackend = _$httpBackend_ EnterpriseRegistrationService = $injector.get("EnterpriseRegistrationService") it "adds the specified attributes to the ERS enterprise object", -> expect(EnterpriseRegistrationService.enterprise.name).toBe "Enterprise 1" - expect(EnterpriseRegistrationService.enterprise.something).toBe true \ No newline at end of file + expect(EnterpriseRegistrationService.enterprise.something).toBe true + + describe "creating an enterprise", -> + describe "success", -> + beforeEach -> + spyOn(RegistrationServiceMock, "select") + $httpBackend.expectPOST("/api/enterprises?token=keykeykeykey").respond 200, 6 + EnterpriseRegistrationService.create() + $httpBackend.flush() + + it "stores the id of the created enterprise", -> + expect(EnterpriseRegistrationService.enterprise.id).toBe 6 + + it "moves the user to the about page", -> + expect(RegistrationServiceMock.select).toHaveBeenCalledWith 'about' + + describe "failure", -> + beforeEach -> + spyOn(RegistrationServiceMock, "select") + spyOn(window, "alert") + $httpBackend.expectPOST("/api/enterprises?token=keykeykeykey").respond 400, 6 + EnterpriseRegistrationService.create() + $httpBackend.flush() + + it "alerts the user to failure", -> + expect(window.alert).toHaveBeenCalledWith 'Failed to create your enterprise.\nPlease ensure all fields are completely filled out.' + + it "does not move the user to the about page", -> + expect(RegistrationServiceMock.select).not.toHaveBeenCalled + + + describe "updating an enterprise", -> + beforeEach -> + EnterpriseRegistrationService.enterprise.id = 78 + spyOn(RegistrationServiceMock, "select") + + describe "success", -> + beforeEach -> + $httpBackend.expectPUT("/api/enterprises/78?token=keykeykeykey").respond 200, 6 + EnterpriseRegistrationService.update('step') + $httpBackend.flush() + + it "moves the user to the about page", -> + expect(RegistrationServiceMock.select).toHaveBeenCalledWith 'step' + + describe "failure", -> + beforeEach -> + spyOn(window, "alert") + $httpBackend.expectPUT("/api/enterprises/78?token=keykeykeykey").respond 400, 6 + EnterpriseRegistrationService.update('step') + $httpBackend.flush() + + it "alerts the user to failure", -> + expect(window.alert).toHaveBeenCalledWith 'Failed to update your enterprise.\nPlease ensure all fields are completely filled out.' + + it "does not move the user to the about page", -> + expect(RegistrationServiceMock.select).not.toHaveBeenCalled