From 014ccd8374e39c30cc8fd3b9c243b221b1c40cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 20 Mar 2020 19:03:57 +0100 Subject: [PATCH] Use shared specs to flag comments --- spec/factories/comments.rb | 6 + spec/shared/system/flaggable.rb | 39 ++++++- .../comments/budget_investments_spec.rb | 49 +------- spec/system/comments/debates_spec.rb | 76 +------------ .../comments/legislation_annotations_spec.rb | 54 +-------- .../comments/legislation_questions_spec.rb | 48 +------- spec/system/comments/proposals_spec.rb | 49 +------- spec/system/comments/topics_spec.rb | 106 +----------------- 8 files changed, 51 insertions(+), 376 deletions(-) diff --git a/spec/factories/comments.rb b/spec/factories/comments.rb index 3e35e0e2d..79402c448 100644 --- a/spec/factories/comments.rb +++ b/spec/factories/comments.rb @@ -4,6 +4,12 @@ FactoryBot.define do user sequence(:body) { |n| "Comment body #{n}" } + %i[budget_investment debate legislation_annotation legislation_question proposal topic_with_community].each do |model| + factory :"#{model}_comment" do + association :commentable, factory: model + end + end + trait :hidden do hidden_at { Time.current } end diff --git a/spec/shared/system/flaggable.rb b/spec/shared/system/flaggable.rb index da7c97417..b43527b2a 100644 --- a/spec/shared/system/flaggable.rb +++ b/spec/shared/system/flaggable.rb @@ -1,13 +1,22 @@ shared_examples "flaggable" do |factory_name| - let(:user) { create(:user) } + include ActionView::RecordIdentifier + + let(:user) { create(:user, :level_two) } let(:flaggable) { create(factory_name) } - let(:path) { polymorphic_path(flaggable) } + + let(:path) do + if flaggable.is_a?(Comment) + polymorphic_path(flaggable.commentable) + else + polymorphic_path(flaggable) + end + end scenario "Flagging as inappropriate", :js do login_as(user) visit path - within ".flag-content" do + within "##{dom_id(flaggable)} .flag-content" do find(".icon-flag").click click_link "Flag as inappropriate" @@ -24,7 +33,7 @@ shared_examples "flaggable" do |factory_name| login_as(user) visit path - within ".flag-content" do + within "##{dom_id(flaggable)} .flag-content" do expect(page).to have_css ".flag-active" find(".icon-flag").click @@ -41,7 +50,7 @@ shared_examples "flaggable" do |factory_name| login_as(user) visit path - within ".flag-content" do + within "##{dom_id(flaggable)} .flag-content" do find(".icon-flag").click click_link "Flag as inappropriate" @@ -56,4 +65,24 @@ shared_examples "flaggable" do |factory_name| expect(Flag.flagged?(user, flaggable)).not_to be end + + scenario "Flagging a comment with a child does not update its children", :js do + skip "Only for comments" unless flaggable.is_a?(Comment) + + child_comment = create(:comment, commentable: flaggable.commentable, parent: flaggable) + + login_as(user) + visit path + + within "##{dom_id(flaggable)} > .comment-body .flag-content" do + find(".icon-flag").click + click_link "Flag as inappropriate" + + expect(page).to have_css ".flag-active" + end + + within "##{dom_id(child_comment)} .flag-content" do + expect(page).not_to have_css ".flag-active" + end + end end diff --git a/spec/system/comments/budget_investments_spec.rb b/spec/system/comments/budget_investments_spec.rb index c2adbbbb4..667d4a970 100644 --- a/spec/system/comments/budget_investments_spec.rb +++ b/spec/system/comments/budget_investments_spec.rb @@ -4,6 +4,8 @@ describe "Commenting Budget::Investments" do let(:user) { create :user } let(:investment) { create :budget_investment } + it_behaves_like "flaggable", :budget_investment_comment + scenario "Index" do 3.times { create(:comment, commentable: investment) } create(:comment, :valuation, commentable: investment, subject: "Not viable") @@ -300,53 +302,6 @@ describe "Commenting Budget::Investments" do expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") end - scenario "Flagging as inappropriate", :js do - comment = create(:comment, commentable: investment) - - login_as(user) - visit budget_investment_path(investment.budget, investment) - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - page.find("#flag-comment-#{comment.id}").click - - expect(page).to have_css("#unflag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).to be - end - - scenario "Undoing flagging as inappropriate", :js do - comment = create(:comment, commentable: investment) - Flag.flag(user, comment) - - login_as(user) - visit budget_investment_path(investment.budget, investment) - - within "#comment_#{comment.id}" do - page.find("#unflag-expand-comment-#{comment.id}").click - page.find("#unflag-comment-#{comment.id}").click - - expect(page).to have_css("#flag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).not_to be - end - - scenario "Flagging turbolinks sanity check", :js do - investment = create(:budget_investment, title: "Should we change the world?") - comment = create(:comment, commentable: investment) - - login_as(user) - visit budget_investments_path(investment.budget) - click_link "Should we change the world?" - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - expect(page).to have_selector("#flag-comment-#{comment.id}") - end - end - scenario "Erasing a comment's author" do investment = create(:budget_investment) comment = create(:comment, commentable: investment, body: "this should be visible") diff --git a/spec/system/comments/debates_spec.rb b/spec/system/comments/debates_spec.rb index d486c9284..f3e38cfb4 100644 --- a/spec/system/comments/debates_spec.rb +++ b/spec/system/comments/debates_spec.rb @@ -4,6 +4,8 @@ describe "Commenting debates" do let(:user) { create :user } let(:debate) { create :debate } + it_behaves_like "flaggable", :debate_comment + scenario "Index" do 3.times { create(:comment, commentable: debate) } @@ -338,80 +340,6 @@ describe "Commenting debates" do expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") end - scenario "Flagging as inappropriate", :js do - comment = create(:comment, commentable: debate) - - login_as(user) - visit debate_path(debate) - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - page.find("#flag-comment-#{comment.id}").click - - expect(page).to have_css("#unflag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).to be - end - - scenario "Undoing flagging as inappropriate", :js do - comment = create(:comment, commentable: debate) - Flag.flag(user, comment) - - login_as(user) - visit debate_path(debate) - - within "#comment_#{comment.id}" do - page.find("#unflag-expand-comment-#{comment.id}").click - page.find("#unflag-comment-#{comment.id}").click - - expect(page).to have_css("#flag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).not_to be - end - - scenario "Flagging turbolinks sanity check", :js do - debate = create(:debate, title: "Should we change the world?") - comment = create(:comment, commentable: debate) - - login_as(user) - visit debates_path - click_link "Should we change the world?" - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - page.find("#flag-comment-#{comment.id}").click - - expect(page).to have_css("#unflag-expand-comment-#{comment.id}") - expect(Flag.flagged?(user, comment)).to be - - page.find("#unflag-expand-comment-#{comment.id}").click - page.find("#unflag-comment-#{comment.id}").click - - expect(page).to have_css("#flag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).not_to be - end - - scenario "Flagging a comment with a child does not update its children", :js do - debate = create(:debate, title: "Should we change the world?") - parent_comment = create(:comment, commentable: debate, body: "Main comment") - child_comment = create(:comment, body: "First subcomment", commentable: debate, parent: parent_comment) - - login_as(user) - visit debate_path(debate) - - within "#comment_#{parent_comment.id}" do - page.find("#flag-expand-comment-#{parent_comment.id}").click - page.find("#flag-comment-#{parent_comment.id}").click - - expect(page).to have_css("#unflag-expand-comment-#{parent_comment.id}") - expect(page).to have_css("#flag-expand-comment-#{child_comment.id}") - end - end - scenario "Erasing a comment's author" do debate = create(:debate) comment = create(:comment, commentable: debate, body: "this should be visible") diff --git a/spec/system/comments/legislation_annotations_spec.rb b/spec/system/comments/legislation_annotations_spec.rb index 4e0663ca6..46f9d5baa 100644 --- a/spec/system/comments/legislation_annotations_spec.rb +++ b/spec/system/comments/legislation_annotations_spec.rb @@ -4,6 +4,8 @@ describe "Commenting legislation questions" do let(:user) { create :user } let(:legislation_annotation) { create :legislation_annotation, author: user } + it_behaves_like "flaggable", :legislation_annotation_comment + scenario "Index" do 3.times { create(:comment, commentable: legislation_annotation) } @@ -349,58 +351,6 @@ describe "Commenting legislation questions" do expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") end - scenario "Flagging as inappropriate", :js do - comment = create(:comment, commentable: legislation_annotation) - - login_as(user) - visit legislation_process_draft_version_annotation_path(legislation_annotation.draft_version.process, - legislation_annotation.draft_version, - legislation_annotation) - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - page.find("#flag-comment-#{comment.id}").click - - expect(page).to have_css("#unflag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).to be - end - - scenario "Undoing flagging as inappropriate", :js do - comment = create(:comment, commentable: legislation_annotation) - Flag.flag(user, comment) - - login_as(user) - visit legislation_process_draft_version_annotation_path(legislation_annotation.draft_version.process, - legislation_annotation.draft_version, - legislation_annotation) - - within "#comment_#{comment.id}" do - page.find("#unflag-expand-comment-#{comment.id}").click - page.find("#unflag-comment-#{comment.id}").click - - expect(page).to have_css("#flag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).not_to be - end - - scenario "Flagging turbolinks sanity check", :js do - legislation_annotation = create(:legislation_annotation, text: "Should we change the world?") - comment = create(:comment, commentable: legislation_annotation) - - login_as(user) - visit legislation_process_draft_version_annotation_path(legislation_annotation.draft_version.process, - legislation_annotation.draft_version, - legislation_annotation) - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - expect(page).to have_selector("#flag-comment-#{comment.id}") - end - end - scenario "Erasing a comment's author" do legislation_annotation = create(:legislation_annotation) comment = create(:comment, commentable: legislation_annotation, body: "this should be visible") diff --git a/spec/system/comments/legislation_questions_spec.rb b/spec/system/comments/legislation_questions_spec.rb index 6f55c7d49..1750e9422 100644 --- a/spec/system/comments/legislation_questions_spec.rb +++ b/spec/system/comments/legislation_questions_spec.rb @@ -7,6 +7,7 @@ describe "Commenting legislation questions" do context "Concerns" do it_behaves_like "notifiable in-app", :legislation_question + it_behaves_like "flaggable", :legislation_question_comment end scenario "Index" do @@ -320,53 +321,6 @@ describe "Commenting legislation questions" do expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") end - scenario "Flagging as inappropriate", :js do - comment = create(:comment, commentable: legislation_question) - - login_as(user) - visit legislation_process_question_path(legislation_question.process, legislation_question) - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - page.find("#flag-comment-#{comment.id}").click - - expect(page).to have_css("#unflag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).to be - end - - scenario "Undoing flagging as inappropriate", :js do - comment = create(:comment, commentable: legislation_question) - Flag.flag(user, comment) - - login_as(user) - visit legislation_process_question_path(legislation_question.process, legislation_question) - - within "#comment_#{comment.id}" do - page.find("#unflag-expand-comment-#{comment.id}").click - page.find("#unflag-comment-#{comment.id}").click - - expect(page).to have_css("#flag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).not_to be - end - - scenario "Flagging turbolinks sanity check", :js do - legislation_question = create(:legislation_question, process: process, title: "Should we change the world?") - comment = create(:comment, commentable: legislation_question) - - login_as(user) - visit legislation_process_path(legislation_question.process) - click_link "Should we change the world?" - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - expect(page).to have_selector("#flag-comment-#{comment.id}") - end - end - scenario "Erasing a comment's author" do comment = create(:comment, commentable: legislation_question, body: "this should be visible") comment.user.erase diff --git a/spec/system/comments/proposals_spec.rb b/spec/system/comments/proposals_spec.rb index 02b08440d..89fa11b08 100644 --- a/spec/system/comments/proposals_spec.rb +++ b/spec/system/comments/proposals_spec.rb @@ -4,6 +4,8 @@ describe "Commenting proposals" do let(:user) { create :user } let(:proposal) { create :proposal } + it_behaves_like "flaggable", :proposal_comment + scenario "Index" do 3.times { create(:comment, commentable: proposal) } @@ -296,53 +298,6 @@ describe "Commenting proposals" do expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") end - scenario "Flagging as inappropriate", :js do - comment = create(:comment, commentable: proposal) - - login_as(user) - visit proposal_path(proposal) - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - page.find("#flag-comment-#{comment.id}").click - - expect(page).to have_css("#unflag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).to be - end - - scenario "Undoing flagging as inappropriate", :js do - comment = create(:comment, commentable: proposal) - Flag.flag(user, comment) - - login_as(user) - visit proposal_path(proposal) - - within "#comment_#{comment.id}" do - page.find("#unflag-expand-comment-#{comment.id}").click - page.find("#unflag-comment-#{comment.id}").click - - expect(page).to have_css("#flag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).not_to be - end - - scenario "Flagging turbolinks sanity check", :js do - proposal = create(:proposal, title: "Should we change the world?") - comment = create(:comment, commentable: proposal) - - login_as(user) - visit proposals_path - click_link "Should we change the world?" - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - expect(page).to have_selector("#flag-comment-#{comment.id}") - end - end - scenario "Erasing a comment's author" do proposal = create(:proposal) comment = create(:comment, commentable: proposal, body: "this should be visible") diff --git a/spec/system/comments/topics_spec.rb b/spec/system/comments/topics_spec.rb index 377fc7236..c7537814c 100644 --- a/spec/system/comments/topics_spec.rb +++ b/spec/system/comments/topics_spec.rb @@ -4,6 +4,8 @@ describe "Commenting topics from proposals" do let(:user) { create :user } let(:proposal) { create :proposal } + it_behaves_like "flaggable", :topic_with_community_comment + scenario "Index" do community = proposal.community topic = create(:topic, community: community) @@ -330,58 +332,6 @@ describe "Commenting topics from proposals" do expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") end - scenario "Flagging as inappropriate", :js do - community = proposal.community - topic = create(:topic, community: community) - comment = create(:comment, commentable: topic) - - login_as(user) - visit community_topic_path(community, topic) - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - page.find("#flag-comment-#{comment.id}").click - - expect(page).to have_css("#unflag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).to be - end - - scenario "Undoing flagging as inappropriate", :js do - community = proposal.community - topic = create(:topic, community: community) - comment = create(:comment, commentable: topic) - Flag.flag(user, comment) - - login_as(user) - visit community_topic_path(community, topic) - - within "#comment_#{comment.id}" do - page.find("#unflag-expand-comment-#{comment.id}").click - page.find("#unflag-comment-#{comment.id}").click - - expect(page).to have_css("#flag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).not_to be - end - - scenario "Flagging turbolinks sanity check", :js do - community = proposal.community - topic = create(:topic, community: community, title: "Should we change the world?") - comment = create(:comment, commentable: topic) - - login_as(user) - visit community_path(community) - click_link "Should we change the world?" - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - expect(page).to have_selector("#flag-comment-#{comment.id}") - end - end - scenario "Erasing a comment's author" do community = proposal.community topic = create(:topic, community: community) @@ -890,58 +840,6 @@ describe "Commenting topics from budget investments" do expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") end - scenario "Flagging as inappropriate", :js do - community = investment.community - topic = create(:topic, community: community) - comment = create(:comment, commentable: topic) - - login_as(user) - visit community_topic_path(community, topic) - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - page.find("#flag-comment-#{comment.id}").click - - expect(page).to have_css("#unflag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).to be - end - - scenario "Undoing flagging as inappropriate", :js do - community = investment.community - topic = create(:topic, community: community) - comment = create(:comment, commentable: topic) - Flag.flag(user, comment) - - login_as(user) - visit community_topic_path(community, topic) - - within "#comment_#{comment.id}" do - page.find("#unflag-expand-comment-#{comment.id}").click - page.find("#unflag-comment-#{comment.id}").click - - expect(page).to have_css("#flag-expand-comment-#{comment.id}") - end - - expect(Flag.flagged?(user, comment)).not_to be - end - - scenario "Flagging turbolinks sanity check", :js do - community = investment.community - topic = create(:topic, community: community, title: "Should we change the world?") - comment = create(:comment, commentable: topic) - - login_as(user) - visit community_path(community) - click_link "Should we change the world?" - - within "#comment_#{comment.id}" do - page.find("#flag-expand-comment-#{comment.id}").click - expect(page).to have_selector("#flag-comment-#{comment.id}") - end - end - scenario "Erasing a comment's author" do community = investment.community topic = create(:topic, community: community)