Files
nairobi/spec/shared/features/relationable.rb
Javi Martín db97f9d08c Add and apply rubocop rules for empty lines
We were very inconsistent regarding these rules.

Personally I prefer no empty lines around blocks, clases, etc... as
recommended by the Ruby style guide [1], and they're the default values
in rubocop, so those are the settings I'm applying.

The exception is the `private` access modifier, since we were leaving
empty lines around it most of the time. That's the default rubocop rule
as well. Personally I don't have a strong preference about this one.


[1] https://rubystyle.guide/#empty-lines-around-bodies
2019-10-24 17:11:47 +02:00

137 lines
4.7 KiB
Ruby

shared_examples "relationable" do |relationable_model_name|
let(:relationable) { create(relationable_model_name.name.parameterize(separator: "_").to_sym) }
let(:related1) { create([:proposal, :debate, :budget_investment].sample) }
let(:related2) { create([:proposal, :debate, :budget_investment].sample) }
let(:user) { create(:user) }
scenario "related contents are listed" do
create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
visit relationable.url
within("#related-content-list") do
expect(page).to have_content(related1.title)
end
visit related1.url
within("#related-content-list") do
expect(page).to have_content(relationable.title)
end
end
scenario "related contents list is not rendered if there are no relations" do
visit relationable.url
expect(page).not_to have_css("#related-content-list")
end
scenario "related contents can be added" do
login_as(user)
visit relationable.url
expect(page).to have_selector("#related_content", visible: false)
click_on("Add related content")
expect(page).to have_selector("#related_content", visible: true)
within("#related_content") do
fill_in "url", with: "#{Setting["url"] + related1.url}"
click_button "Add"
end
within("#related-content-list") do
expect(page).to have_content(related1.title)
end
visit related1.url
within("#related-content-list") do
expect(page).to have_content(relationable.title)
end
within("#related_content") do
fill_in "url", with: "#{Setting["url"] + related2.url}"
click_button "Add"
end
within("#related-content-list") do
expect(page).to have_content(related2.title)
end
end
scenario "if related content URL is invalid returns error" do
login_as(user)
visit relationable.url
click_on("Add related content")
within("#related_content") do
fill_in "url", with: "http://invalidurl.com"
click_button "Add"
end
expect(page).to have_content("Link not valid. Remember to start with #{Setting[:url]}.")
end
scenario "returns error when relating content URL to itself" do
login_as(user)
visit relationable.url
click_on("Add related content")
within("#related_content") do
fill_in "url", with: Setting[:url] + relationable.url.to_s
click_button "Add"
end
expect(page).to have_content("Link not valid. You cannot relate a content to itself")
end
scenario "related content can be scored positively", :js do
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
login_as(user)
visit relationable.url
within("#related-content-list") do
find("#related-content-#{related_content.opposite_related_content.id}").hover
find("#score-positive-related-#{related_content.opposite_related_content.id}").click
expect(page).not_to have_css("#score-positive-related-#{related_content.opposite_related_content.id}")
end
expect(related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.id).value).to eq(1)
expect(related_content.opposite_related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.opposite_related_content.id).value).to eq(1)
end
scenario "related content can be scored negatively", :js do
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
login_as(user)
visit relationable.url
within("#related-content-list") do
find("#related-content-#{related_content.opposite_related_content.id}").hover
find("#score-negative-related-#{related_content.opposite_related_content.id}").click
expect(page).not_to have_css("#score-negative-related-#{related_content.opposite_related_content.id}")
end
expect(related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.id).value).to eq(-1)
expect(related_content.opposite_related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.opposite_related_content.id).value).to eq(-1)
end
scenario "if related content has negative score it will be hidden" do
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
2.times do
related_content.send("score_positive", build(:user))
end
6.times do
related_content.send("score_negative", build(:user))
end
login_as(user)
visit relationable.url
expect(page).not_to have_css("#related-content-list")
end
end