Having exceptions is better than having silent bugs.
There are a few methods I've kept the same way they were.
The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.
We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").
Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.
There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.
For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
90 lines
2.8 KiB
Ruby
90 lines
2.8 KiB
Ruby
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?).not_to 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.current, retired_reason: "unfeasible",
|
|
retired_explanation: "Unfeasibility explanation ...")
|
|
expect(notification.check_availability(notifiable)).to be(false)
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
end
|