From 6e6f53e290ac90650cde4866cb900fc92da7e408 Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Mon, 31 Jul 2023 17:21:03 +0200 Subject: [PATCH] Connect the sidebar/account_nav component to the authentication helpers Compact the specs so they run a single render with aggregate_failures. --- .../sidebar/account_nav/component.rb | 2 +- .../solidus_admin/sidebar/component.html.erb | 7 +++- admin/lib/solidus_admin/preview.rb | 13 +++++++ .../component_preview/overview.html.erb | 39 ++++++++++++------- .../sidebar/account_nav/component_spec.rb | 34 ++++++---------- .../solidus_admin/sidebar/component_spec.rb | 7 ++++ admin/spec/spec_helper.rb | 1 + 7 files changed, 66 insertions(+), 37 deletions(-) diff --git a/admin/app/components/solidus_admin/sidebar/account_nav/component.rb b/admin/app/components/solidus_admin/sidebar/account_nav/component.rb index b3862ae968f..59df8c445c7 100644 --- a/admin/app/components/solidus_admin/sidebar/account_nav/component.rb +++ b/admin/app/components/solidus_admin/sidebar/account_nav/component.rb @@ -6,7 +6,7 @@ class SolidusAdmin::Sidebar::AccountNav::Component < SolidusAdmin::BaseComponent # @param account_path [String] # @param logout_path [String] # @param logout_method [Symbol] - def initialize(user_label: "Alice Doe", account_path: "#", logout_path: "#", logout_method: :delete) + def initialize(user_label:, account_path:, logout_path:, logout_method:) @user_label = user_label @account_path = account_path @logout_path = logout_path diff --git a/admin/app/components/solidus_admin/sidebar/component.html.erb b/admin/app/components/solidus_admin/sidebar/component.html.erb index 1f3a509b980..afe9e57230f 100644 --- a/admin/app/components/solidus_admin/sidebar/component.html.erb +++ b/admin/app/components/solidus_admin/sidebar/component.html.erb @@ -23,6 +23,11 @@
- <%= render @account_nav_component.new %> + <%= render @account_nav_component.new( + user_label: helpers.current_solidus_admin_user.email, + account_path: solidus_admin.account_path, + logout_path: helpers.solidus_admin_logout_path, + logout_method: helpers.solidus_admin_logout_method, + ) %>
diff --git a/admin/lib/solidus_admin/preview.rb b/admin/lib/solidus_admin/preview.rb index 009bb425c54..331e36452dd 100644 --- a/admin/lib/solidus_admin/preview.rb +++ b/admin/lib/solidus_admin/preview.rb @@ -27,6 +27,7 @@ module ControllerHelper extend ActiveSupport::Concern included do + include SolidusAdmin::Auth helper ActionView::Helpers helper SolidusAdmin::ContainerHelper helper_method :current_component @@ -34,6 +35,18 @@ module ControllerHelper private + def spree_current_user + Spree::LegacyUser.new(email: "admin@example.com") + end + + def authenticate_solidus_backend_user! + # noop + end + + def admin_logout_path + "/logout" + end + def current_component @current_component ||= begin # Lookbook sets the @preview instance variable with a PreviewEntity instance, while ViewComponent uses the preview class. diff --git a/admin/spec/components/previews/solidus_admin/sidebar/account_nav/component_preview/overview.html.erb b/admin/spec/components/previews/solidus_admin/sidebar/account_nav/component_preview/overview.html.erb index bf0e7d09068..210dc2804d1 100644 --- a/admin/spec/components/previews/solidus_admin/sidebar/account_nav/component_preview/overview.html.erb +++ b/admin/spec/components/previews/solidus_admin/sidebar/account_nav/component_preview/overview.html.erb @@ -1,16 +1,29 @@ -
-
-
- <%= render current_component.new( - user_label: "Alice Doe" - ) %> -
+
+
+ Short name +
+ +
+ <%= render current_component.new( + user_label: "Alice Doe", + account_path: "#account", + logout_path: "#logout", + logout_method: :delete + ) %>
-
-
- <%= render current_component.new( - user_label: "Alice Supercalifragilisticexpialidocious" - ) %> -
+
+ +
+
+ Long name +
+ +
+ <%= render current_component.new( + user_label: "Alice Supercalifragilisticexpialidocious", + account_path: "#account", + logout_path: "#logout", + logout_method: :delete + ) %>
diff --git a/admin/spec/components/solidus_admin/sidebar/account_nav/component_spec.rb b/admin/spec/components/solidus_admin/sidebar/account_nav/component_spec.rb index 7ae6ba9f97c..0b76d1fdc11 100644 --- a/admin/spec/components/solidus_admin/sidebar/account_nav/component_spec.rb +++ b/admin/spec/components/solidus_admin/sidebar/account_nav/component_spec.rb @@ -7,33 +7,23 @@ render_preview(:overview) end - it "renders link to the account" do + it "renders correctly" do component = described_class.new( - account_path: "/admin/account" + user_label: "Alice", + account_path: "/admin/account", + logout_path: "/admin/logout", + logout_method: :delete, ) render_inline(component) - expect(page).to have_link("Account", href: "/admin/account", visible: :any) - end - - it "renders link to logout" do - component = described_class.new( - logout_path: "/admin/logout" - ) - - render_inline(component) - - expect(page).to have_link("Logout", href: "/admin/logout", visible: :any) - end - - it "renders user lanel" do - component = described_class.new( - user_label: "Alice" - ) - - render_inline(component) + aggregate_failures do + expect(page).to have_content("Alice") - expect(page).to have_content("Alice") + # Links are hidden within a
element + expect(page).to have_link("Account", href: "/admin/account", visible: :any) + expect(page).to have_link("Logout", href: "/admin/logout", visible: :any) + expect(page.find_link("Logout", visible: :any)["data-method"]).to eq("delete") + end end end diff --git a/admin/spec/components/solidus_admin/sidebar/component_spec.rb b/admin/spec/components/solidus_admin/sidebar/component_spec.rb index 9423f317b7a..856df498747 100644 --- a/admin/spec/components/solidus_admin/sidebar/component_spec.rb +++ b/admin/spec/components/solidus_admin/sidebar/component_spec.rb @@ -3,6 +3,13 @@ require "spec_helper" RSpec.describe SolidusAdmin::Sidebar::Component, type: :component do + before do + allow(vc_test_controller).to receive(:spree_current_user).and_return(build(:user)) + without_partial_double_verification do + allow(vc_test_controller.spree).to receive(:admin_logout_path).and_return('/logout') + end + end + it "renders the overview preview" do render_preview(:overview) end diff --git a/admin/spec/spec_helper.rb b/admin/spec/spec_helper.rb index db8ac8572d8..b6577cc25e0 100644 --- a/admin/spec/spec_helper.rb +++ b/admin/spec/spec_helper.rb @@ -68,6 +68,7 @@ Spree::TestingSupport::FactoryBot.add_paths_and_load! # VIEW COMPONENTS +Rails.application.config.view_component.test_controller = "SolidusAdmin::BaseController" require "view_component/test_helpers" require "view_component/system_test_helpers"