From 6538c7adca8fb0c5824d7eaf61c2c7b2abb17c89 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 22 May 2024 10:44:08 +1000 Subject: [PATCH] Fix shopping system spec --- .../controllers/products_controller.js.coffee | 2 +- .../darkswarm/services/products.js.coffee | 2 +- .../services/products_spec.js.coffee | 25 +++++++++------ .../consumer/shopping/checkout_paypal_spec.rb | 2 +- .../system/consumer/shopping/shopping_spec.rb | 31 +++++++++++++------ 5 files changed, 40 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee index 79f1db42ab..930091bec5 100644 --- a/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee @@ -66,7 +66,7 @@ angular.module('Darkswarm').controller "ProductsCtrl", ($scope, $sce, $filter, $ id: $scope.order_cycle.order_cycle_id, page: page || $scope.page, per_page: $scope.per_page, - 'q[name_or_meta_keywords_or_variants_display_as_or_variants_display_name_or_supplier_name_cont]': $scope.query, + 'q[name_or_meta_keywords_or_variants_display_as_or_variants_display_name_or_variants_supplier_name_cont]': $scope.query, 'q[with_properties][]': $scope.activeProperties, 'q[variants_primary_taxon_id_in_any][]': $scope.activeTaxons } diff --git a/app/assets/javascripts/darkswarm/services/products.js.coffee b/app/assets/javascripts/darkswarm/services/products.js.coffee index 8482c34d57..6334f0cd7f 100644 --- a/app/assets/javascripts/darkswarm/services/products.js.coffee +++ b/app/assets/javascripts/darkswarm/services/products.js.coffee @@ -39,7 +39,7 @@ angular.module('Darkswarm').factory 'Products', (OrderCycleResource, OrderCycle, dereference: -> for product in @fetched_products - product.supplier = Shopfront.producers_by_id[product.supplier.id] + product.supplier = Shopfront.producers_by_id[product.variants[0].supplier.id] Dereferencer.dereference product.taxons, Taxons.taxons_by_id product.properties = angular.copy(product.properties_with_values) diff --git a/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee index baea195cf2..7f9ea624a3 100644 --- a/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee @@ -19,16 +19,16 @@ describe 'Products service', -> beforeEach -> product = test: "cats" - supplier: - id: 9 price: 11 - master: {} - variants: [] + variants: [ + id: 1000, price: 11, supplier: {id: 9} + ] productWithImage = supplier: id: 9 - master: {} - variants: [] + variants: [ + id: 1000, price: 20, supplier: {id: 9} + ] image: { large_url: 'foo.png' } @@ -79,7 +79,7 @@ describe 'Products service', -> it "dereferences suppliers", -> Shopfront.producers_by_id = {id: 9, name: "test"} - $httpBackend.expectGET(endpoint).respond([{supplier : {id: 9}, master: {}}]) + $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].supplier).toBe Shopfront.producers_by_id["9"] @@ -96,13 +96,16 @@ describe 'Products service', -> expect(Products.products[0].properties[1]).toBe properties[0] it "registers variants with Variants service", -> - product.variants = [{id: 1}] + product.variants = [{id: 1, supplier: {id: 9}}] $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].variants[0]).toBe Variants.variants[1] it "stores variant names", -> - product.variants = [{id: 1, name_to_display: "one"}, {id: 2, name_to_display: "two"}] + product.variants = [ + {id: 1, name_to_display: "one", supplier: {id: 9}}, + {id: 2, name_to_display: "two", supplier: {id: 9}} + ] $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].variant_names).toEqual "one two " @@ -125,7 +128,9 @@ describe 'Products service', -> expect(Products.products[0].price).toEqual 11.00 it "displays the minimum variant price when the product has variants", -> - product.variants = [{price: 22}, {price: 33}] + product.variants = [ + {price: 22, supplier: {id: 9} }, {price: 33, supplier: {id: 9}} + ] $httpBackend.expectGET(endpoint).respond([product]) $httpBackend.flush() expect(Products.products[0].price).toEqual 22 diff --git a/spec/system/consumer/shopping/checkout_paypal_spec.rb b/spec/system/consumer/shopping/checkout_paypal_spec.rb index 8368867e2c..98947d099c 100644 --- a/spec/system/consumer/shopping/checkout_paypal_spec.rb +++ b/spec/system/consumer/shopping/checkout_paypal_spec.rb @@ -10,7 +10,7 @@ RSpec.describe "Check out with Paypal" do let(:distributor) { create(:distributor_enterprise) } let(:supplier) { create(:supplier_enterprise) } - let(:product) { create(:simple_product, supplier:) } + let(:product) { create(:simple_product, supplier_id: supplier.id) } let(:variant) { product.variants.first } let(:order_cycle) { create( diff --git a/spec/system/consumer/shopping/shopping_spec.rb b/spec/system/consumer/shopping/shopping_spec.rb index 7868c73382..becfc90f03 100644 --- a/spec/system/consumer/shopping/shopping_spec.rb +++ b/spec/system/consumer/shopping/shopping_spec.rb @@ -22,7 +22,7 @@ RSpec.describe "As a consumer I want to shop with a distributor" do coordinator: create(:distributor_enterprise), orders_close_at: 3.days.from_now) } - let(:product) { create(:simple_product, supplier:, meta_keywords: "Domestic") } + let(:product) { create(:simple_product, supplier_id: supplier.id, meta_keywords: "Domestic") } let(:variant) { product.variants.first } let(:order) { create(:order, distributor:) } @@ -260,7 +260,7 @@ RSpec.describe "As a consumer I want to shop with a distributor" do context "one having 20 products" do before do 20.times do - product = create(:simple_product, supplier:) + product = create(:simple_product, supplier_id: supplier.id) add_variant_to_order_cycle(exchange1, product.variants.first) end end @@ -275,7 +275,7 @@ RSpec.describe "As a consumer I want to shop with a distributor" do context "another having 5 products" do before do 5.times do - product = create(:simple_product, supplier:) + product = create(:simple_product, supplier_id: supplier.id) add_variant_to_order_cycle(exchange2, product.variants.first) end end @@ -296,9 +296,11 @@ RSpec.describe "As a consumer I want to shop with a distributor" do display_as: 'displayedunderthename') end let(:product2) { - create(:simple_product, supplier:, name: "Meercats", meta_keywords: "Wild Fresh") + create(:simple_product, supplier_id: supplier.id, name: "Meercats", meta_keywords: "Wild Fresh") + } + let(:variant3) { + create(:variant, product: product2, supplier:, price: 40, display_name: "Ferrets") } - let(:variant3) { create(:variant, product: product2, price: 40, display_name: "Ferrets") } let(:exchange) { Exchange.find(oc1.exchanges.to_enterprises(distributor).outgoing.first.id) } before do @@ -333,39 +335,50 @@ RSpec.describe "As a consumer I want to shop with a distributor" do end context "filtering search results" do - before do - visit shop_path - sleep(2) - end it "returns results when successful" do + visit shop_path + # When we see the Add button, it means product are loaded on the page + expect(page).to have_content("Add", count: 4) + fill_in "search", with: "74576345634XXXXXX" expect(page).to have_content "Sorry, no results found" expect(page).not_to have_content 'Meercats' + click_on "Clear search" # clears search by clicking text expect(page).to have_content("Add", count: 4) + fill_in "search", with: "Meer" # For product named "Meercats" expect(page).to have_content 'Meercats' expect(page).not_to have_content product.name + find("a.clear").click # clears search by clicking the X button expect(page).to have_content("Add", count: 4) end + it "returns results by looking at different columns in DB" do + visit shop_path + # When we see the Add button, it means product are loaded on the page + expect(page).to have_content("Add", count: 4) + # by keyword model: meta_keywords fill_in "search", with: "Wild" # For product named "Meercats" expect(page).to have_content 'Wild' find("a.clear").click + # by variant display name model: variant display_name fill_in "search", with: "Ferrets" # For variants named "Ferrets" within('div.pad-top') do expect(page).to have_content 'Ferrets' expect(page).not_to have_content 'Badgers' end + # model: variant display_as fill_in "search", with: "displayedunder" # "Badgers" within('div.pad-top') do expect(page).not_to have_content 'Ferrets' expect(page).to have_content 'Badgers' end + # model: Enterprise name fill_in "search", with: "Enterp" # Enterprise 1 sells nothing within('p.no-results') do