Merge pull request #2187 from consul/notifications
Fix notifications' exceptions
This commit is contained in:
137
spec/shared/features/notifiable_in_app.rb
Normal file
137
spec/shared/features/notifiable_in_app.rb
Normal file
@@ -0,0 +1,137 @@
|
||||
shared_examples "notifiable in-app" do |described_class|
|
||||
|
||||
let(:author) { create(:user, :verified) }
|
||||
let!(:notifiable) { create(model_name(described_class), author: author) }
|
||||
|
||||
scenario "A user commented on my notifiable", :js do
|
||||
notification = create(:notification, notifiable: notifiable, user: author)
|
||||
|
||||
login_as author
|
||||
visit root_path
|
||||
find(".icon-notification").click
|
||||
|
||||
expect(page).to have_css ".notification", count: 1
|
||||
expect(page).to have_content "Someone commented on"
|
||||
expect(page).to have_xpath "//a[@href='#{notification_path(notification)}']"
|
||||
end
|
||||
|
||||
scenario "Multiple users commented on my notifiable", :js do
|
||||
3.times do
|
||||
login_as(create(:user, :verified))
|
||||
|
||||
visit path_for(notifiable)
|
||||
|
||||
fill_in comment_body(notifiable), with: "I agree"
|
||||
click_button "publish_comment"
|
||||
within "#comments" do
|
||||
expect(page).to have_content "I agree"
|
||||
end
|
||||
end
|
||||
|
||||
logout
|
||||
login_as author
|
||||
visit root_path
|
||||
visit root_path
|
||||
find(".icon-notification").click
|
||||
|
||||
expect(page).to have_css ".notification", count: 1
|
||||
expect(page).to have_content "There are 3 new comments on"
|
||||
expect(page).to have_xpath "//a[@href='#{notification_path(Notification.last)}']"
|
||||
end
|
||||
|
||||
scenario "A user replied to my comment", :js do
|
||||
comment = create :comment, commentable: notifiable, user: author
|
||||
|
||||
login_as(create(:user, :verified))
|
||||
visit path_for(notifiable)
|
||||
|
||||
click_link "Reply"
|
||||
within "#js-comment-form-comment_#{comment.id}" do
|
||||
fill_in "comment-body-comment_#{comment.id}", with: "I replied to your comment"
|
||||
click_button "Publish reply"
|
||||
end
|
||||
|
||||
within "#comment_#{comment.id}" do
|
||||
expect(page).to have_content "I replied to your comment"
|
||||
end
|
||||
|
||||
logout
|
||||
login_as author
|
||||
visit root_path
|
||||
visit root_path
|
||||
find(".icon-notification").click
|
||||
|
||||
expect(page).to have_css ".notification", count: 1
|
||||
expect(page).to have_content "Someone replied to your comment on"
|
||||
expect(page).to have_xpath "//a[@href='#{notification_path(Notification.last)}']"
|
||||
end
|
||||
|
||||
scenario "Multiple replies to my comment", :js do
|
||||
comment = create :comment, commentable: notifiable, user: author
|
||||
|
||||
3.times do |n|
|
||||
login_as(create(:user, :verified))
|
||||
visit path_for(notifiable)
|
||||
|
||||
within("#comment_#{comment.id}_reply") { click_link "Reply" }
|
||||
within "#js-comment-form-comment_#{comment.id}" do
|
||||
fill_in "comment-body-comment_#{comment.id}", with: "Reply number #{n}"
|
||||
click_button "Publish reply"
|
||||
end
|
||||
|
||||
within "#comment_#{comment.id}" do
|
||||
expect(page).to have_content "Reply number #{n}"
|
||||
end
|
||||
logout
|
||||
end
|
||||
|
||||
login_as author
|
||||
visit root_path
|
||||
visit root_path
|
||||
find(".icon-notification").click
|
||||
|
||||
expect(page).to have_css ".notification", count: 1
|
||||
expect(page).to have_content "There are 3 new replies to your comment on"
|
||||
expect(page).to have_xpath "//a[@href='#{notification_path(Notification.last)}']"
|
||||
end
|
||||
|
||||
scenario "Author commented on his own notifiable", :js do
|
||||
login_as(author)
|
||||
visit path_for(notifiable)
|
||||
|
||||
fill_in comment_body(notifiable), with: "I commented on my own notifiable"
|
||||
click_button "publish_comment"
|
||||
within "#comments" do
|
||||
expect(page).to have_content "I commented on my own notifiable"
|
||||
end
|
||||
|
||||
within("#notifications") do
|
||||
find(".icon-no-notification").click
|
||||
expect(page).to have_css ".notification", count: 0
|
||||
end
|
||||
end
|
||||
|
||||
scenario "Author replied to his own comment", :js do
|
||||
comment = create :comment, commentable: notifiable, user: author
|
||||
|
||||
login_as author
|
||||
visit path_for(notifiable)
|
||||
|
||||
click_link "Reply"
|
||||
within "#js-comment-form-comment_#{comment.id}" do
|
||||
fill_in "comment-body-comment_#{comment.id}", with: "I replied to my own comment"
|
||||
click_button "Publish reply"
|
||||
end
|
||||
|
||||
within "#comment_#{comment.id}" do
|
||||
expect(page).to have_content "I replied to my own comment"
|
||||
end
|
||||
|
||||
within("#notifications") do
|
||||
find(".icon-no-notification").click
|
||||
expect(page).to have_css ".notification", count: 0
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
88
spec/shared/models/notifiable.rb
Normal file
88
spec/shared/models/notifiable.rb
Normal file
@@ -0,0 +1,88 @@
|
||||
shared_examples "notifiable" do
|
||||
|
||||
let(:notifiable) { create(model_name(described_class)) }
|
||||
|
||||
describe "#notification_title" do
|
||||
|
||||
it "returns the notifiable title when it's a root comment" do
|
||||
notification = create(:notification, notifiable: notifiable)
|
||||
|
||||
expect(notification.notifiable_title).to eq notifiable.title
|
||||
end
|
||||
|
||||
it "returns the notifiable title when it's a reply to a root comment" do
|
||||
comment = create(:comment, commentable: notifiable)
|
||||
notification = create(:notification, notifiable: comment)
|
||||
|
||||
expect(notification.notifiable_title).to eq notifiable.title
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "notifiable_available?" do
|
||||
|
||||
it "returns true when it's a root comment and the notifiable is available" do
|
||||
notification = create(:notification, notifiable: notifiable)
|
||||
|
||||
expect(notification.notifiable_available?).to be(true)
|
||||
end
|
||||
|
||||
it "returns true when it's a reply to comment and the notifiable is available" do
|
||||
comment = create(:comment, commentable: notifiable)
|
||||
notification = create(:notification, notifiable: comment)
|
||||
|
||||
expect(notification.notifiable_available?).to be(true)
|
||||
end
|
||||
|
||||
it "returns false when it's a root comment and the notifiable has been hidden" do
|
||||
notification = create(:notification, notifiable: notifiable)
|
||||
|
||||
notifiable.hide
|
||||
notification.reload
|
||||
|
||||
expect(notification.notifiable_available?).to_not be(true)
|
||||
end
|
||||
|
||||
it "returns false when it's a reply to comment and the commentable has been hidden" do
|
||||
comment = create(:comment, commentable: notifiable)
|
||||
notification = create(:notification, notifiable: comment)
|
||||
|
||||
notifiable.hide
|
||||
notification.reload
|
||||
|
||||
expect(notification.notifiable_available?).to be(false)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "check_availability" do
|
||||
|
||||
it "returns true if the resource is present, not hidden, nor retired" do
|
||||
notification = create(:notification, notifiable: notifiable)
|
||||
expect(notification.check_availability(notifiable)).to be(true)
|
||||
end
|
||||
|
||||
it "returns false if the resource is not present" do
|
||||
notification = create(:notification, notifiable: notifiable)
|
||||
notifiable.really_destroy!
|
||||
expect(notification.check_availability(notifiable)).to be(false)
|
||||
end
|
||||
|
||||
it "returns false if the resource is not hidden" do
|
||||
notification = create(:notification, notifiable: notifiable)
|
||||
notifiable.hide
|
||||
expect(notification.check_availability(notifiable)).to be(false)
|
||||
end
|
||||
|
||||
it "returns false if the resource is retired" do
|
||||
notification = create(:notification, notifiable: notifiable)
|
||||
|
||||
if notifiable.respond_to?(:retired_at)
|
||||
notifiable.update(retired_at: Time.now)
|
||||
expect(notification.check_availability(notifiable)).to be(false)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user