From 00ba8425e360365610281b52d935612d1968ae4a Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 19 May 2023 10:29:54 +0200 Subject: [PATCH] Fix multiple subscriptions sort --- lib/custom_wizard/subscription.rb | 2 +- .../custom_wizard/subscription_spec.rb | 21 ++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/custom_wizard/subscription.rb b/lib/custom_wizard/subscription.rb index 02627f07..6ca592f9 100644 --- a/lib/custom_wizard/subscription.rb +++ b/lib/custom_wizard/subscription.rb @@ -117,7 +117,7 @@ class CustomWizard::Subscription end id_and_slug = ids_and_slugs.sort do |a, b| - PRODUCT_HIERARCHY[a[:slug]] - PRODUCT_HIERARCHY[b[:slug]] + PRODUCT_HIERARCHY.index(b[:slug]) - PRODUCT_HIERARCHY.index(a[:slug]) end.first @product_id = id_and_slug[:id] diff --git a/spec/components/custom_wizard/subscription_spec.rb b/spec/components/custom_wizard/subscription_spec.rb index 4829d83d..0bfb1839 100644 --- a/spec/components/custom_wizard/subscription_spec.rb +++ b/spec/components/custom_wizard/subscription_spec.rb @@ -78,17 +78,18 @@ describe CustomWizard::Subscription do end context "with subscriptions" do - def get_subscription_result(product_id) + def get_subscription_result(product_ids) result = SubscriptionClient::Subscriptions::Result.new result.supplier = SubscriptionClientSupplier.new(product_slugs) result.resource = SubscriptionClientResource.new - result.subscriptions = [SubscriptionClientSubscription.new(product_id)] + result.subscriptions = product_ids.map { |product_id| SubscriptionClientSubscription.new(product_id) } result.products = product_slugs result end - let!(:business_subscription_result) { get_subscription_result(business_product_id) } - let!(:standard_subscription_result) { get_subscription_result(standard_product_id) } - let!(:community_subscription_result) { get_subscription_result(community_product_id) } + let!(:business_subscription_result) { get_subscription_result([business_product_id]) } + let!(:standard_subscription_result) { get_subscription_result([standard_product_id]) } + let!(:community_subscription_result) { get_subscription_result([community_product_id]) } + let!(:multiple_subscription_result) { get_subscription_result([community_product_id, business_product_id]) } it "handles mapped values" do SubscriptionClient.stubs(:find_subscriptions).returns(standard_subscription_result) @@ -143,6 +144,16 @@ describe CustomWizard::Subscription do expect(described_class.includes?(:action, :type, 'create_category')).to eq(true) end end + + context "with multiple subscriptions" do + before do + SubscriptionClient.stubs(:find_subscriptions).returns(multiple_subscription_result) + end + + it "detects correct type in hierarchy" do + expect(described_class.type).to eq(:business) + end + end end end end