forked from nanego/my-dcim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1aacca
commit 186405c
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
class LabelComponent < ApplicationComponent | ||
TYPES = %i[default primary success info warning danger] | ||
|
||
erb_template <<~ERB | ||
<span class="<%= css_classes %>"> | ||
<%= content %> | ||
</span> | ||
ERB | ||
|
||
def initialize(text = nil, type: :default) | ||
@text = text | ||
|
||
@type = type.to_sym | ||
raise ArgumentError, "'#{@type.inspect}' is not a valid type" unless TYPES.include?(@type) | ||
end | ||
|
||
def css_classes | ||
"label label-#{@type}" | ||
end | ||
|
||
def content | ||
super || @text | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe LabelComponent, type: :component do | ||
context "with html content" do | ||
let(:component) { described_class.new(text, **kwargs) } | ||
let(:rendered_component) { render_inline(component, &block) } | ||
let(:text) { "Text as argument" } | ||
let(:kwargs) { {} } | ||
let(:block) {} | ||
|
||
context "with text" do | ||
it { expect(rendered_component.to_html).to have_tag("span.label.label-default", text: /Text as argument/) } | ||
end | ||
|
||
context "with text as block" do | ||
let(:block) { proc { "Text as block" } } | ||
|
||
it { expect(rendered_component.to_html).to have_tag("span.label.label-default", text: /Text as block/) } | ||
end | ||
|
||
context "with type" do | ||
let(:kwargs) { { type: :success } } | ||
|
||
it { expect(rendered_component.to_html).to have_tag("span.label.label-success", text: /Text as argument/) } | ||
end | ||
end | ||
end |