Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
davidalejandroaguilar authored Aug 30, 2024
1 parent a786bc6 commit 6f01506
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
103 changes: 103 additions & 0 deletions lib/phlexy_ui/link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# frozen_string_literal: true

module PhlexyUI
class Link < Base
def initialize(*args, &)
@link_to_options = args.last.is_a?(Hash) ? args.pop : {}
@link_to_name = args.shift if args.first.is_a?(String) || args.first.nil?
modifiers = args.select { |arg| arg.is_a?(Symbol) }

super(*modifiers, **@link_to_options, &)
end

def view_template(&)
generate_classes!(
component_html_class: :link,
modifiers_map: LINK_MODIFIERS_MAP,
base_modifiers:,
options:
).then do |classes|
if respond_to?(:link_to)
link_to(@link_to_name, class: classes, **@link_to_options, &)
elsif as
public_send(as, class: classes, **options, &)
else
a(class: classes, **options, &)
end
end
end

private

LINK_MODIFIERS_MAP = {
# "sm:image-full"
# "md:image-full"
# "lg:image-full"
image_full: "image-full",
# "sm:card-bordered"
# "md:card-bordered"
# "lg:card-bordered"
bordered: "card-bordered",
# "sm:card-normal"
# "md:card-normal"
# "lg:card-normal"
normal: "card-normal",
# "sm:card-compact"
# "md:card-compact"
# "lg:card-compact"
compact: "card-compact",
# "sm:card-side"
# "md:card-side"
# "lg:card-side"
side: "card-side",
# "sm:glass"
# "md:glass"
# "lg:glass"
glass: "glass",
# "sm:bg-primary sm:text-primary-content"
# "md:bg-primary md:text-primary-content"
# "lg:bg-primary lg:text-primary-content"
primary: "bg-primary text-primary-content",
# "sm:bg-secondary sm:text-secondary-content"
# "md:bg-secondary md:text-secondary-content"
# "lg:bg-secondary lg:text-secondary-content"
secondary: "bg-secondary text-secondary-content",
# "sm:bg-accent sm:text-accent-content"
# "md:bg-accent md:text-accent-content"
# "lg:bg-accent lg:text-accent-content"
accent: "bg-accent text-accent-content",
# "sm:bg-neutral sm:text-neutral-content"
# "md:bg-neutral md:text-neutral-content"
# "lg:bg-neutral lg:text-neutral-content"
neutral: "bg-neutral text-neutral-content",
# "sm:bg-base-100 sm:text-base-content"
# "md:bg-base-100 md:text-base-content"
# "lg:bg-base-100 lg:text-base-content"
base_100: "bg-base-100 text-base-content",
# "sm:bg-base-200 sm:text-base-content"
# "md:bg-base-200 md:text-base-content"
# "lg:bg-base-200 lg:text-base-content"
base_200: "bg-base-200 text-base-content",
# "sm:bg-base-300 sm:text-base-content"
# "md:bg-base-300 md:text-base-content"
# "lg:bg-base-300 lg:text-base-content"
base_300: "bg-base-300 text-base-content",
# "sm:bg-info sm:text-info-content"
# "md:bg-info sm:text-info-content"
# "lg:bg-info sm:text-info-content"
info: "bg-info text-info-content",
# "sm:bg-success sm:text-success-content"
# "md:bg-success md:text-success-content"
# "lg:bg-success lg:text-success-content"
success: "bg-success text-success-content",
# "sm:bg-warning sm:text-warning-content"
# "md:bg-warning md:text-warning-content"
# "lg:bg-warning lg:text-warning-content"
warning: "bg-warning text-warning-content",
# "sm:bg-error sm:text-error-content"
# "md:bg-error md:text-error-content"
# "lg:bg-error lg:text-error-content"
error: "bg-error text-error-content"
}.freeze
end
end
55 changes: 55 additions & 0 deletions spec/lib/phlexy_ui/link_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require "spec_helper"

describe PhlexyUI::Link do
subject(:output) { render described_class.new }

describe "rendering via Kit" do
subject(:output) do
Link tip: "A tooltip"
end

it "renders it correctly" do
expected_html = html <<~HTML
<div class="tooltip" data-tip="A tooltip"></div>
HTML

expect(output).to eq(expected_html)
end
end

describe "passing :as option" do
subject(:output) { render described_class.new(as: :a, tip: "A tooltip") }

it "renders the tooltip as the given tag" do
expected_html = html <<~HTML
<a class="tooltip" data-tip="A tooltip"></a>
HTML

expect(output).to eq(expected_html)
end
end

describe "rendering a full tooltip" do
let(:component) do
Class.new(Phlex::HTML) do
def view_template(&)
render PhlexyUI::Link.new("#", :active, class: "test", data: {my: "data"}) do
"Link text"
end
end
end
end

subject(:output) do
render component.new
end

it "is expected to match the formatted HTML" do
expected_html = html <<~HTML
<a href="#" class="link active test" data-my="data">Link text</a>
HTML

is_expected.to eq(expected_html)
end
end
end

0 comments on commit 6f01506

Please sign in to comment.