Files
grecia/spec/shared/system/flaggable.rb
Javi Martín 92ddcb7aef Use JavaScript in system tests by default
JavaScript is used by about 98% of web users, so by testing without it
enabled, we're only testing that the application works for a very
reduced number of users.

We proceeded this way in the past because CONSUL started using Rails 4.2
and truncating the database between JavaScript tests with database
cleaner, which made these tests terribly slow.

When we upgraded to Rails 5.1 and introduced system tests, we started
using database transactions in JavaScript tests, making these tests much
faster. So now we can use JavaScript tests everywhere without critically
slowing down our test suite.
2021-04-07 14:41:06 +02:00

114 lines
2.8 KiB
Ruby

shared_examples "flaggable" do |factory_name, admin: false|
include ActionView::RecordIdentifier
let(:flaggable) { create(factory_name) }
let(:user) do
if admin
create(:administrator).user
else
create(:user, :level_two)
end
end
let(:path) do
if flaggable.is_a?(Comment)
polymorphic_path(flaggable.commentable)
elsif admin
admin_polymorphic_path(flaggable)
else
polymorphic_path(flaggable)
end
end
scenario "Flagging as inappropriate" do
login_as(user)
visit path
within "##{dom_id(flaggable)} .flag-content" do
click_button "Flag as inappropriate"
click_link "Flag as inappropriate"
expect(page).to have_button "Unflag"
expect(page).to have_link "Unflag", visible: :hidden
expect(page).not_to have_link "Flag as inappropriate", visible: :all
end
visit path
within "##{dom_id(flaggable)} .flag-content" do
expect(page).to have_link "Unflag", visible: :hidden
expect(page).not_to have_link "Flag as inappropriate", visible: :all
end
end
scenario "Unflagging" do
Flag.flag(user, flaggable)
login_as(user)
visit path
within "##{dom_id(flaggable)} .flag-content" do
expect(page).to have_button "Unflag"
click_button "Unflag"
click_link "Unflag"
expect(page).not_to have_button "Unflag"
expect(page).to have_link "Flag as inappropriate", visible: :hidden
expect(page).not_to have_link "Unflag", visible: :all
end
visit path
within "##{dom_id(flaggable)} .flag-content" do
expect(page).to have_link "Flag as inappropriate", visible: :hidden
expect(page).not_to have_link "Unflag", visible: :all
end
end
scenario "Flagging and unflagging" do
login_as(user)
visit path
within "##{dom_id(flaggable)} .flag-content" do
click_button "Flag as inappropriate"
click_link "Flag as inappropriate"
expect(page).to have_button "Unflag"
click_button "Unflag"
click_link "Unflag"
expect(page).not_to have_button "Unflag"
end
visit path
within "##{dom_id(flaggable)} .flag-content" do
expect(page).to have_link "Flag as inappropriate", visible: :hidden
expect(page).not_to have_link "Unflag", visible: :all
end
end
scenario "Flagging a comment with a child does not update its children" 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
click_button "Flag as inappropriate"
click_link "Flag as inappropriate"
expect(page).to have_button "Unflag"
end
within "##{dom_id(child_comment)} .flag-content" do
expect(page).not_to have_button "Unflag"
end
end
end