From 59a3fe11add536c11c1792577997bfc2a47de2de Mon Sep 17 00:00:00 2001 From: Rob H Date: Wed, 6 Aug 2014 10:41:07 +1000 Subject: [PATCH] Fix taxon service specs --- .../admin/services/taxons.js.coffee | 4 +- .../unit/admin/services/taxons_spec.js.coffee | 27 +++++++++++ .../unit/bulk_product_update_spec.js.coffee | 47 ------------------- 3 files changed, 28 insertions(+), 50 deletions(-) create mode 100644 spec/javascripts/unit/admin/services/taxons_spec.js.coffee diff --git a/app/assets/javascripts/admin/services/taxons.js.coffee b/app/assets/javascripts/admin/services/taxons.js.coffee index 211ddb57a7..a45770c787 100644 --- a/app/assets/javascripts/admin/services/taxons.js.coffee +++ b/app/assets/javascripts/admin/services/taxons.js.coffee @@ -4,9 +4,7 @@ angular.module("ofn.admin").factory "Taxons", (taxons, $filter) -> @taxons = taxons findByIDs: (ids) -> - taxons = [] - taxons.push taxon for taxon in @taxons when taxon.id.toString() in ids.split(",") - taxons + taxon for taxon in @taxons when taxon.id in ids.split(",") findByTerm: (term) -> $filter('filter')(@taxons, term) \ No newline at end of file diff --git a/spec/javascripts/unit/admin/services/taxons_spec.js.coffee b/spec/javascripts/unit/admin/services/taxons_spec.js.coffee new file mode 100644 index 0000000000..4c69052b38 --- /dev/null +++ b/spec/javascripts/unit/admin/services/taxons_spec.js.coffee @@ -0,0 +1,27 @@ +describe "Taxons service", -> + Taxons = taxons = $httpBackend = $resource = null + + beforeEach -> + module "ofn.admin" + module ($provide)-> + $provide.value "taxons", [{id: "1", name: "t1"}, {id: "2", name: "t2"}, {id: "12", name: "t12"}, {id: "31", name: "t31"}] + null + + beforeEach inject (_Taxons_, _$resource_, _$httpBackend_) -> + Taxons = _Taxons_ + $resource = _$resource_ + $httpBackend = _$httpBackend_ + + describe "calling findByIDs", -> + it "returns taxons with exactly matching ids", -> + result = Taxons.findByIDs("1,2") + expect(result).toEqual [{id: "1", name: "t1"}, {id: "2", name: "t2"}] + + it "ignores ids which do not exactly match", -> + result = Taxons.findByIDs("1,3") + expect(result).toEqual [{id: "1", name: "t1"}] + + describe "calling findByTerm", -> + it "returns taxons which match partially", -> + result = Taxons.findByTerm("t1") + expect(result).toEqual [{id: "1", name: "t1"}, {id: "12", name: "t12"}] \ No newline at end of file diff --git a/spec/javascripts/unit/bulk_product_update_spec.js.coffee b/spec/javascripts/unit/bulk_product_update_spec.js.coffee index 99f53c5079..0a61ac2154 100644 --- a/spec/javascripts/unit/bulk_product_update_spec.js.coffee +++ b/spec/javascripts/unit/bulk_product_update_spec.js.coffee @@ -333,7 +333,6 @@ describe "AdminProductEditCtrl", -> describe "preparing products", -> beforeEach -> - spyOn $scope, "matchProducer" spyOn $scope, "loadVariantUnit" it "initialises display properties for the product", -> @@ -342,12 +341,6 @@ describe "AdminProductEditCtrl", -> $scope.unpackProduct product expect($scope.displayProperties[123]).toEqual {showVariants: false} - it "calls matchProducer for the product", -> - product = {id: 123} - $scope.displayProperties = {} - $scope.unpackProduct product - expect($scope.matchProducer.calls.length).toEqual 1 - it "calls loadVariantUnit for the product", -> product = {id: 123} $scope.displayProperties = {} @@ -355,21 +348,6 @@ describe "AdminProductEditCtrl", -> expect($scope.loadVariantUnit.calls.length).toEqual 1 - describe "matching producer", -> - it "changes the producer of the product to the one which matches it from the producers list", -> - $scope.producers = [ - { id: 1, name: "S1" } - { id: 2, name: "S2" } - ] - - product = - id: 10 - producer: 1 - - $scope.matchProducer product - expect(product.producer).toBe $scope.producers[0] - - describe "loading variant unit", -> describe "setting product variant_unit_with_scale field", -> it "sets by combining variant_unit and variant_unit_scale", -> @@ -1264,28 +1242,3 @@ describe "converting arrays of objects with ids to an object with ids as keys", expect(toObjectWithIDKeys).toHaveBeenCalledWith [id: 17] expect(toObjectWithIDKeys).not.toHaveBeenCalledWith {12: {id: 12}} - -describe "Taxons service", -> - Taxons = $httpBackend = $resource = null - - beforeEach -> - module "ofn.admin" - - beforeEach inject (_Taxons_, _$resource_, _$httpBackend_) -> - Taxons = _Taxons_ - $resource = _$resource_ - $httpBackend = _$httpBackend_ - - it "calling findByIDs makes a http request", -> - response = { taxons: "list of taxons by id" } - $httpBackend.expectGET("/admin/taxons/search?ids=1,2").respond 200, response - taxons = Taxons.findByIDs("1,2") - $httpBackend.flush() - expect(angular.equals(taxons,response)).toBe true - - it "calling findByTerm makes a http request", -> - response = { taxons: "list of taxons by term" } - $httpBackend.expectGET("/admin/taxons/search?q=lala").respond 200, response - taxons = Taxons.findByTerm("lala") - $httpBackend.flush() - expect(angular.equals(taxons,response)).toBe true