Create rspec shared examples to test followable features on any followable entity.

This commit is contained in:
Senén Rodero Rodríguez
2017-06-30 16:02:49 +02:00
parent 19820e4432
commit 1f22286e29
6 changed files with 97 additions and 139 deletions

View File

@@ -23,6 +23,11 @@ module FollowsHelper
entity_name.downcase
end
def entity_full_name(followable)
entity_name = followable.class.name
entity_name.downcase.gsub("::", "-")
end
private
def followed?(followable)

View File

@@ -1,21 +1,21 @@
<% entity = entity_name(followable) %>
<span class="followable-content">
<% if show_follow_action? followable %>
<a id="follow-expand-<%= entity %>-<%= followable.id %>" data-toggle="follow-drop-<%= entity %>-<%= followable.id %>" title="<%= follow_entity_text(followable) %>">
<a href="#follow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" id="follow-expand-<%= entity_full_name(followable) %>-<%= followable.id %>" data-toggle="follow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" title="<%= follow_entity_text(followable) %>">
<%= t('shared.follow') %>
</a>
<div class="dropdown-pane" id="follow-drop-<%= entity %>-<%= followable.id %>" data-dropdown data-auto-focus="true">
<%= link_to follow_entity_text(followable), follows_path(followable_id: followable.id, followable_type: followable.class.name), method: :post, remote: true, id: "follow-#{entity}-#{ followable.id }" %>
<div class="dropdown-pane" id="follow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" data-dropdown data-auto-focus="true">
<%= link_to follow_entity_text(followable), follows_path(followable_id: followable.id, followable_type: followable.class.name), method: :post, remote: true, id: "follow-#{entity_full_name(followable)}-#{ followable.id }" %>
</div>
<% end %>
<% if show_unfollow_action? followable %>
<% follow = followable.follows.where(user: current_user).first %>
<a id="unfollow-expand-<%= entity %>-<%= followable.id %>" data-toggle="unfollow-drop-<%= entity %>-<%= followable.id %>" title="<%= unfollow_entity_text(followable) %>">
<a href="#unfollow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" id="unfollow-expand-<%= entity_full_name(followable) %>-<%= followable.id %>" data-toggle="unfollow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" title="<%= unfollow_entity_text(followable) %>">
<%= t('shared.unfollow') %>
</a>
<div class="dropdown-pane" id="unfollow-drop-<%= entity %>-<%= followable.id %>" data-dropdown data-auto-focus="true">
<%= link_to unfollow_entity_text(followable), follow_path(follow), method: :delete, remote: true, id: "unfollow-#{entity}-#{ followable.id }" %>
<div class="dropdown-pane" id="unfollow-drop-<%= entity_full_name(followable) %>-<%= followable.id %>" data-dropdown data-auto-focus="true">
<%= link_to unfollow_entity_text(followable), follow_path(follow), method: :delete, remote: true, id: "unfollow-#{entity_full_name(followable)}-#{ followable.id }" %>
</div>
<% end %>
</span>

View File

@@ -420,73 +420,7 @@ feature 'Budget Investments' do
end
end
feature "Follows" do
scenario "Should not show follow button when there is no logged user" do
investment = create(:budget_investment)
visit budget_investment_path(budget_id: investment.budget.id, id: investment.id)
within "#budget_investment_#{investment.id}" do
expect(page).not_to have_link("Follow citizen proposal")
end
end
scenario "Following", :js do
user = create(:user)
investment = create(:budget_investment)
login_as(user)
visit budget_investment_path(budget_id: investment.budget.id, id: investment.id)
within "#budget_investment_#{investment.id}" do
page.find("#follow-expand-investment-#{investment.id}").click
page.find("#follow-investment-#{investment.id}").click
expect(page).to have_css("#unfollow-expand-investment-#{investment.id}")
end
expect(Follow.followed?(user, investment)).to be
end
scenario "Show unfollow button when user already follow this investment" do
user = create(:user)
investment = create(:budget_investment)
follow = create(:follow, :followed_investment, user: user, followable: investment)
login_as(user)
visit budget_investment_path(budget_id: investment.budget.id, id: investment.id)
expect(page).to have_link("Unfollow investment")
end
scenario "Unfollowing", :js do
user = create(:user)
investment = create(:budget_investment)
follow = create(:follow, :followed_investment, user: user, followable: investment)
login_as(user)
visit budget_investment_path(budget_id: investment.budget.id, id: investment.id)
within "#budget_investment_#{investment.id}" do
page.find("#unfollow-expand-investment-#{investment.id}").click
page.find("#unfollow-investment-#{investment.id}").click
expect(page).to have_css("#follow-expand-investment-#{investment.id}")
end
expect(Follow.followed?(user, investment)).not_to be
end
scenario "Show follow button when user is not following this investment" do
user = create(:user)
investment = create(:budget_investment)
login_as(user)
visit budget_investment_path(budget_id: investment.budget.id, id: investment.id)
expect(page).to have_link("Follow investment")
end
end
it_behaves_like "followable", "budget_investment", "budget_investment_path", {"budget_id": "budget_id", "id": "id"}
context "Destroy" do

View File

@@ -1223,72 +1223,7 @@ feature 'Proposals' do
expect(Flag.flagged?(user, proposal)).to_not be
end
feature "Follows" do
scenario "Should not show follow button when there is no logged user" do
proposal = create(:proposal)
visit proposal_path(proposal)
within "#proposal_#{proposal.id}" do
expect(page).not_to have_link("Follow citizen proposal")
end
end
scenario "Following", :js do
user = create(:user)
proposal = create(:proposal)
login_as(user)
visit proposal_path(proposal)
within "#proposal_#{proposal.id}" do
page.find("#follow-expand-proposal-#{proposal.id}").click
page.find("#follow-proposal-#{proposal.id}").click
expect(page).to have_css("#unfollow-expand-proposal-#{proposal.id}")
end
expect(Follow.followed?(user, proposal)).to be
end
scenario "Show unfollow button when user already follow this proposal" do
user = create(:user)
follow = create(:follow, :followed_proposal, user: user)
login_as(user)
visit proposal_path(follow.followable)
expect(page).to have_link("Unfollow citizen proposal")
end
scenario "Unfollowing", :js do
user = create(:user)
proposal = create(:proposal)
follow = create(:follow, :followed_proposal, user: user, followable: proposal)
login_as(user)
visit proposal_path(proposal)
within "#proposal_#{proposal.id}" do
page.find("#unfollow-expand-proposal-#{proposal.id}").click
page.find("#unfollow-proposal-#{proposal.id}").click
expect(page).to have_css("#follow-expand-proposal-#{proposal.id}")
end
expect(Follow.followed?(user, proposal)).not_to be
end
scenario "Show follow button when user is not following this proposal" do
user = create(:user)
proposal = create(:proposal)
login_as(user)
visit proposal_path(proposal)
expect(page).to have_link("Follow citizen proposal")
end
end
it_behaves_like "followable", "proposal", "proposal_path", { "id": "id" }
scenario 'Erased author' do
user = create(:user)

View File

@@ -0,0 +1,83 @@
RSpec.shared_examples "followable" do |followable_class_name, followable_path, followable_path_arguments|
include ActionView::Helpers
let!(:arguments) { {} }
let!(:followable) { create(followable_class_name) }
let!(:followable_dom_name) { followable_class_name.gsub('_', '-') }
before do
followable_path_arguments.each do |argument_name, path_to_value|
arguments.merge!("#{argument_name}": followable.send(path_to_value))
end
end
scenario "Should not show follow button when there is no logged user" do
visit send(followable_path, arguments)
within "##{dom_id(followable)}" do
expect(page).not_to have_link("Follow")
end
end
scenario "Should show follow button when user is logged in" do
user = create(:user)
login_as(user)
visit send(followable_path, arguments)
within "##{dom_id(followable)}" do
expect(page).to have_link("Follow")
end
end
scenario "Following", :js do
user = create(:user)
login_as(user)
visit send(followable_path, arguments)
within "##{dom_id(followable)}" do
click_link "Follow"
page.find("#follow-#{followable_dom_name}-#{followable.id}").click
expect(page).to have_css("#unfollow-expand-#{followable_dom_name}-#{followable.id}")
end
expect(Follow.followed?(user, followable)).to be
end
scenario "Show unfollow button when user already follow this followable" do
user = create(:user)
follow = create(:follow, user: user, followable: followable)
login_as(user)
visit send(followable_path, arguments)
expect(page).to have_link("Unfollow")
end
scenario "Unfollowing", :js do
user = create(:user)
follow = create(:follow, user: user, followable: followable)
login_as(user)
visit send(followable_path, arguments)
within "##{dom_id(followable)}" do
click_link "Unfollow"
page.find("#unfollow-#{followable_dom_name}-#{followable.id}").click
expect(page).to have_css("#follow-expand-#{followable_dom_name}-#{followable.id}")
end
expect(Follow.followed?(user, followable)).not_to be
end
scenario "Show follow button when user is not following this followable" do
user = create(:user)
login_as(user)
visit send(followable_path, arguments)
expect(page).to have_link("Follow")
end
end

View File

@@ -5,6 +5,7 @@ require 'devise'
require 'knapsack'
Dir["./spec/models/concerns/*.rb"].each { |f| require f }
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
Dir["./spec/shared/**/*.rb"].sort.each { |f| require f }
RSpec.configure do |config|
config.use_transactional_fixtures = false