Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed May 18, 2022
1 parent 5cff187 commit 423a714
Showing 1 changed file with 24 additions and 45 deletions.
69 changes: 24 additions & 45 deletions spec/administrate/views/fields/has_one/_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,9 @@ def view.namespace
end

context "with a persisted record" do
it "renders a link to the record" do
field_resource = create(:product_meta_tag)
path_to_field_resource = polymorphic_path([:admin, field_resource])

nested_show_page_for_has_one = instance_double(
"Administrate::Page::Show",
resource: double(
class: ProductMetaTag,
),
attributes: []
)

has_one_field = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: "The Nested Resource",
data: field_resource,
linkable?: true,
nested_show: nested_show_page_for_has_one,
)

render(
partial: "fields/has_one/show",
locals: {
field: has_one_field,
resource_name: "parent_resource",
},
)

link = "<a href=\"#{path_to_field_resource}\">The Nested Resource</a>"
expect(rendered.strip).to include(link)
end

it "uses the correct labels for fields" do
before do
field_resource = create(:product_meta_tag)
path_to_field_resource = polymorphic_path([:admin, field_resource])
@path_to_field_resource = polymorphic_path([:admin, field_resource])

nested_simple_field = instance_double(
"Administrate::Field::String",
Expand All @@ -81,7 +49,7 @@ def view.namespace
]
)

has_one_field = instance_double(
@has_one_field = instance_double(
"Administrate::Field::HasOne",
display_associated_resource: "The Nested Resource",
data: field_resource,
Expand All @@ -90,8 +58,27 @@ def view.namespace
associated_class_name: "NestedHasOne",
)

page_double = instance_double("Administrate::Page::Show")
@page_double = instance_double("Administrate::Page::Show")
end

def render_field
render(
partial: "fields/has_one/show",
locals: {
field: @has_one_field,
page: @page_double,
resource_name: "parent_resource",
},
)
end

it "renders a link to the record" do
render_field
link = "<a href=\"#{@path_to_field_resource}\">The Nested Resource</a>"
expect(rendered.strip).to include(link)
end

it "uses the correct labels for fields" do
I18n.backend.translations(do_init: true)[:en].deep_merge!(
helpers: {
label: {
Expand All @@ -102,15 +89,7 @@ def view.namespace
}
)

render(
partial: "fields/has_one/show",
locals: {
field: has_one_field,
page: page_double,
resource_name: "parent_resource",
},
)

render_field
expect(rendered.strip).to include("Just a Simple String")
end
end
Expand Down

0 comments on commit 423a714

Please sign in to comment.