-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Showing
5 changed files
with
119 additions
and
52 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
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
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
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 |
---|---|---|
@@ -1,41 +1,63 @@ | ||
require "rails_helper" | ||
|
||
describe "fields/has_one/_index", type: :view do | ||
context "without an associated record" do | ||
it "displays nothing" do | ||
has_one = Administrate::Field::HasOne.new( | ||
:product_meta_tag, | ||
build(:product_meta_tag), | ||
:index, | ||
) | ||
let(:product) { create(:product) } | ||
let(:product_path) { polymorphic_path([:admin, product]) } | ||
let(:link) { "<a href=\"#{product_path}\">#{product.name}</a>" } | ||
let(:has_one) do | ||
instance_double( | ||
"Administrate::Field::HasOne", | ||
data: product, | ||
linkable?: true, | ||
display_associated_resource: product.name, | ||
) | ||
end | ||
|
||
render( | ||
partial: "fields/has_one/index", | ||
locals: { field: has_one }, | ||
context "without an associated record" do | ||
let(:has_one) do | ||
instance_double( | ||
"Administrate::Field::HasOne", | ||
linkable?: false | ||
) | ||
end | ||
|
||
it "displays nothing" do | ||
allow(view).to receive(:administrate_valid_action?).and_return(true) | ||
render_has_one_index | ||
expect(rendered.strip).to eq("") | ||
end | ||
end | ||
|
||
context "with an associated record" do | ||
it "renders a link to the record" do | ||
product = create(:product) | ||
product_path = polymorphic_path([:admin, product]) | ||
has_one = instance_double( | ||
"Administrate::Field::HasOne", | ||
data: product, | ||
linkable?: true, | ||
display_associated_resource: product.name, | ||
) | ||
context "if associated resource has a show route" do | ||
context "and the user has permission to access it" do | ||
it "displays link" do | ||
allow(view).to receive(:administrate_valid_action?).and_return(true) | ||
render_has_one_index | ||
expect(rendered.strip).to include(link) | ||
end | ||
end | ||
|
||
render( | ||
partial: "fields/has_one/index", | ||
locals: { field: has_one, namespace: :admin }, | ||
) | ||
context "and the user does not have permission to access it" do | ||
it "hides link" do | ||
allow(view).to receive(:administrate_valid_action?).and_return(false) | ||
render_has_one_index | ||
expect(rendered.strip).to_not include(link) | ||
end | ||
end | ||
end | ||
|
||
expected = "<a href=\"#{product_path}\">#{product.name}</a>" | ||
expect(rendered.strip).to eq(expected) | ||
context "if associated resource has no show route" do | ||
it "hides link" do | ||
allow(view).to receive(:administrate_valid_action?).and_return(false) | ||
render_has_one_index | ||
expect(rendered.strip).to_not include(link) | ||
end | ||
end | ||
|
||
def render_has_one_index | ||
render( | ||
partial: "fields/has_one/index", | ||
locals: { field: has_one, namespace: :admin }, | ||
) | ||
end | ||
end |
67 changes: 46 additions & 21 deletions
67
spec/administrate/views/fields/polymorphic/_index_spec.rb
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 |
---|---|---|
@@ -1,36 +1,61 @@ | ||
require "rails_helper" | ||
|
||
describe "fields/polymorphic/_index", type: :view do | ||
context "without an associated records" do | ||
it "displays nothing" do | ||
polymorphic = double(data: nil) | ||
let(:product) { create(:product) } | ||
let(:product_path) { polymorphic_path([:admin, product]) } | ||
let(:link) { "<a href=\"#{product_path}\">#{product.name}</a>" } | ||
let(:polymorphic) do | ||
instance_double( | ||
"Administrate::Field::Polymorphic", | ||
data: product, | ||
display_associated_resource: product.name, | ||
) | ||
end | ||
|
||
render( | ||
partial: "fields/polymorphic/index", | ||
locals: { field: polymorphic }, | ||
context "without an associated record" do | ||
let(:polymorphic) do | ||
instance_double( | ||
"Administrate::Field::Polymorphic", | ||
data: nil, | ||
) | ||
end | ||
|
||
it "displays nothing" do | ||
render_polymorphic_index | ||
expect(rendered.strip).to eq("") | ||
end | ||
end | ||
|
||
context "with an associated record" do | ||
it "renders a link to the record" do | ||
product = create(:product) | ||
product_path = polymorphic_path([:admin, product]) | ||
polymorphic = instance_double( | ||
"Administrate::Field::Polymorphic", | ||
data: product, | ||
display_associated_resource: product.name, | ||
) | ||
context "if associated resource has a show route" do | ||
context "and the user has permission to access it" do | ||
it "displays link" do | ||
allow(view).to receive(:administrate_valid_action?).and_return(true) | ||
render_polymorphic_index | ||
expect(rendered.strip).to include(link) | ||
end | ||
end | ||
|
||
render( | ||
partial: "fields/polymorphic/index", | ||
locals: { field: polymorphic, namespace: :admin }, | ||
) | ||
context "and the user does not have permission to access it" do | ||
it "hides link" do | ||
allow(view).to receive(:administrate_valid_action?).and_return(false) | ||
render_polymorphic_index | ||
expect(rendered.strip).to_not include(link) | ||
end | ||
end | ||
end | ||
|
||
expected = "<a href=\"#{product_path}\">#{product.name}</a>" | ||
expect(rendered.strip).to eq(expected) | ||
context "if associated resource has no show route" do | ||
it "hides link" do | ||
allow(view).to receive(:administrate_valid_action?).and_return(false) | ||
render_polymorphic_index | ||
expect(rendered.strip).to_not include(link) | ||
end | ||
end | ||
|
||
def render_polymorphic_index | ||
render( | ||
partial: "fields/polymorphic/index", | ||
locals: { field: polymorphic, namespace: :admin }, | ||
) | ||
end | ||
end |