docbrown/spec/services/mentions/create_all_spec.rb
Anna Buianova 01456fe029 [deploy] Finalize deleting user associations (#5624)
* Prepare factories and associations for Users::DeleteActivity specs

* Altered user associations, delete the needed associations in Users::DeleteActivity

* Moved and refined user deletion spec

* Removed useless commented user associations

* Refactored BackupData factory

* Refactored ProfilePin factory

* Refactored Reaction factory

* Fixed specs: Keep default _type in factories or specify it in tests

* Ok, notes shouldn't be deleted on user deletion

* Fixed creating reactable for specs

* Specify commentable/commentable_type explicitly when creating data for tests

* Fixed typo in the comments spec

* Removed unnneeded space

* Added aggregate_failures for testing user associations deletion

* Keep feedback_messages while deleting user activities
2020-01-23 11:29:47 -05:00

79 lines
2.5 KiB
Ruby

require "rails_helper"
RSpec.describe Mentions::CreateAll, type: :service do
let(:user) { create(:user) }
let(:user2) { create(:user) }
let(:article) { create(:article, user_id: user.id) }
let(:comment) { create(:comment, user_id: user2.id, commentable: article) }
let(:comment2) do
create(
:comment,
body_markdown: "Hello @#{user.username}, you are cool.",
user_id: user2.id,
commentable: article,
)
end
it "creates mention if there is a user mentioned and if the user doenst own the comment" do
comment.body_markdown = "Hello @#{user.username}, you are cool."
comment.save
described_class.call(comment)
expect(Mention.all.size).to eq(1)
end
it "deletes mention if deleted from comment" do
comment.body_markdown = "Hello @#{user.username}, you are cool."
comment.save
described_class.call(comment)
expect(Mention.all.size).to eq(1)
comment.body_markdown = "Hello, you are cool."
comment.save
described_class.call(comment)
expect(Mention.all.size).to eq(0)
end
it "creates one mention even if multiple mentions of same user" do
comment.body_markdown = "Hello @#{user.username} @#{user.username} @#{user.username}, you rock."
comment.save
described_class.call(comment)
expect(Mention.all.size).to eq(1)
end
it "creates multiple mentions for multiple users" do
user2 = create(:user)
comment.body_markdown = "Hello @#{user.username} @#{user2.username}, you are cool."
comment.save
described_class.call(comment)
expect(Mention.all.size).to eq(2)
end
it "deletes one of multiple mentions if one of multiple is deleted" do
user2 = create(:user)
comment.body_markdown = "Hello @#{user.username} @#{user2.username}, you are cool."
comment.save
described_class.call(comment)
expect(Mention.all.size).to eq(2)
comment.body_markdown = "Hello @#{user2.username}, you are cool."
comment.save
described_class.call(comment)
expect(Mention.all.size).to eq(1)
end
it "creates mention on creation of comment (in addition to update)" do
described_class.call(comment2)
expect(Mention.all.size).to eq(1)
end
it "can only be created with valid mentionable" do
comment2.update_column(:body_markdown, "")
described_class.call(comment2)
expect(Mention.all.size).to eq(0)
end
it "cant mention self" do
comment.body_markdown = "Me, Myself and I @#{user2.username}"
comment.save
described_class.call(comment)
expect(Mention.all.size).to eq(0)
end
end