Simplify testing followables flash messages

Checking the whole text is tricky because the text has a `<br>` tag, and
now Capybara doesn't normalize whitespace by default anymore.

Here are a couple more options we could use:

```
expect(page).to have_content strip_tags(message.gsub(/\s*<br>\s*/,"\n"))

expect(page).to have_content strip_tags(message), normalize_ws: true
```

But then developers would wonder why we're doing all this, and would
need an extra effort to fully understand the test.

Since the tests are only checking the presence of the flash message,
checking a relevant part of the test is enough, works with any version
of Capybara, and makes the test easy to follow.
This commit is contained in:
Javi Martín
2019-11-18 15:47:47 +01:00
parent 3a15bed471
commit 971571b54b

View File

@@ -55,14 +55,13 @@ shared_examples "followable" do |followable_class_name, followable_path, followa
scenario "Should display new follower notice after user clicks on follow button", :js do scenario "Should display new follower notice after user clicks on follow button", :js do
user = create(:user) user = create(:user)
login_as(user) login_as(user)
create_notice_message = t("shared.followable.#{followable_class_name}.create.notice")
visit send(followable_path, arguments) visit send(followable_path, arguments)
within "##{dom_id(followable)}" do within "##{dom_id(followable)}" do
click_link("Follow #{followable.model_name.human.downcase}") click_link("Follow #{followable.model_name.human.downcase}")
end end
expect(page).to have_content strip_tags(create_notice_message) expect(page).to have_content "We will notify you of changes as they occur"
end end
scenario "Display unfollow button when user already following" do scenario "Display unfollow button when user already following" do
@@ -90,14 +89,13 @@ shared_examples "followable" do |followable_class_name, followable_path, followa
scenario "Should display destroy follower notice after user clicks on unfollow button", :js do scenario "Should display destroy follower notice after user clicks on unfollow button", :js do
user = create(:user, followables: [followable]) user = create(:user, followables: [followable])
login_as(user) login_as(user)
destroy_notice_message = t("shared.followable.#{followable_class_name}.destroy.notice")
visit send(followable_path, arguments) visit send(followable_path, arguments)
within "##{dom_id(followable)}" do within "##{dom_id(followable)}" do
click_link("Unfollow #{followable.model_name.human.downcase}") click_link("Unfollow #{followable.model_name.human.downcase}")
end end
expect(page).to have_content strip_tags(destroy_notice_message) expect(page).to have_content "You will no longer receive notifications"
end end
end end
end end