Files
grecia/spec/shared/system/flaggable.rb
Javi Martín 7489f16633 Try to avoid exception after flaggable tests
We had one exception running test suite [1]:

```
  1) Commenting legislation questions Merged comment threads View
comments of annotations in an included range
     Failure/Error: count: annotation.comments.roots.count) %>

     ActionView::Template::Error:
       PG::ProtocolViolation: ERROR:  bind message supplies 0
parameters, but prepared statement "" requires 2
       : SELECT COUNT(*) FROM "comments" WHERE "comments"."hidden_at" IS
NULL AND "comments"."commentable_id" = $1 AND
"comments"."commentable_type" = $2 AND "comments"."ancestry" IS NULL
```

Debugging shows this test was run right after the flaggable specs.
There's a chance these exceptions take place because the test is
accessing the database after the browser (chromedriver) process has
already accessed the database.

This is just an attempt at fixing the issue; I don't know whether these
changes will fix it since I've only seen this exception on Github
Actions (never on my machine). Worst case scenario, we're encouraging
good practices of system tests: test things from the user's point of
view.

[1] https://github.com/consul/consul/runs/1856245992
2021-02-08 20:23:08 +01:00

114 lines
2.9 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", :js do
login_as(user)
visit path
within "##{dom_id(flaggable)} .flag-content" do
find(".icon-flag").click
click_link "Flag as inappropriate"
expect(page).to have_css ".flag-active"
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", :js do
Flag.flag(user, flaggable)
login_as(user)
visit path
within "##{dom_id(flaggable)} .flag-content" do
expect(page).to have_css ".flag-active"
find(".icon-flag").click
click_link "Unflag"
expect(page).not_to have_css ".flag-active"
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", :js do
login_as(user)
visit path
within "##{dom_id(flaggable)} .flag-content" do
find(".icon-flag").click
click_link "Flag as inappropriate"
expect(page).to have_css ".flag-active"
find(".icon-flag").click
click_link "Unflag"
expect(page).not_to have_css ".flag-active"
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", :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