Skip to content

Commit

Permalink
Connect the sidebar/account_nav component to the authentication helpers
Browse files Browse the repository at this point in the history
Compact the specs so they run a single render with aggregate_failures.
  • Loading branch information
elia committed Aug 1, 2023
1 parent b2a1f1b commit 6e6f53e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
</ul>
</div>
<div class="mt-auto">
<%= 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,
) %>
</div>
</nav>
13 changes: 13 additions & 0 deletions admin/lib/solidus_admin/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,26 @@ module ControllerHelper
extend ActiveSupport::Concern

included do
include SolidusAdmin::Auth
helper ActionView::Helpers
helper SolidusAdmin::ContainerHelper
helper_method :current_component
end

private

def spree_current_user
Spree::LegacyUser.new(email: "[email protected]")
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
<div class="flex h-screen">
<div class="flex h-4/5">
<div class="self-end">
<%= render current_component.new(
user_label: "Alice Doe"
) %>
</div>
<div class="mb-8">
<h6 class="text-gray-500 mb-3 mt-0">
Short name
</h6>

<div class="w-[17.78rem] pt-20 rounded border-dotted border-2 border-gray-300">
<%= render current_component.new(
user_label: "Alice Doe",
account_path: "#account",
logout_path: "#logout",
logout_method: :delete
) %>
</div>
<div class="flex h-4/5">
<div class="self-end">
<%= render current_component.new(
user_label: "Alice Supercalifragilisticexpialidocious"
) %>
</div>
</div>

<div class="mb-8">
<h6 class="text-gray-500 mb-3 mt-0">
Long name
</h6>

<div class="w-[17.78rem] pt-20 rounded border-dotted border-2 border-gray-300">
<%= render current_component.new(
user_label: "Alice Supercalifragilisticexpialidocious",
account_path: "#account",
logout_path: "#logout",
logout_method: :delete
) %>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -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 <details> 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
7 changes: 7 additions & 0 deletions admin/spec/components/solidus_admin/sidebar/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions admin/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 6e6f53e

Please sign in to comment.