From 6f01506a8717e3969b8dfaa6ae5ec785093b34ae Mon Sep 17 00:00:00 2001 From: David Alejandro <15317732+davidalejandroaguilar@users.noreply.github.com> Date: Fri, 30 Aug 2024 07:24:18 +0000 Subject: [PATCH] Test --- lib/phlexy_ui/link.rb | 103 ++++++++++++++++++++++++++++++++ spec/lib/phlexy_ui/link_spec.rb | 55 +++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 lib/phlexy_ui/link.rb create mode 100644 spec/lib/phlexy_ui/link_spec.rb diff --git a/lib/phlexy_ui/link.rb b/lib/phlexy_ui/link.rb new file mode 100644 index 0000000..ce9f1b2 --- /dev/null +++ b/lib/phlexy_ui/link.rb @@ -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 diff --git a/spec/lib/phlexy_ui/link_spec.rb b/spec/lib/phlexy_ui/link_spec.rb new file mode 100644 index 0000000..8adc519 --- /dev/null +++ b/spec/lib/phlexy_ui/link_spec.rb @@ -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 +
+ 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 + + 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 + Link text + HTML + + is_expected.to eq(expected_html) + end + end +end