1
0

account_conversation_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. require 'rails_helper'
  2. RSpec.describe AccountConversation, type: :model do
  3. let!(:alice) { Fabricate(:account, username: 'alice') }
  4. let!(:bob) { Fabricate(:account, username: 'bob') }
  5. let!(:mark) { Fabricate(:account, username: 'mark') }
  6. describe '.add_status' do
  7. it 'creates new record when no others exist' do
  8. status = Fabricate(:status, account: alice, visibility: :direct)
  9. status.mentions.create(account: bob)
  10. conversation = AccountConversation.add_status(alice, status)
  11. expect(conversation.participant_accounts).to include(bob)
  12. expect(conversation.last_status).to eq status
  13. expect(conversation.status_ids).to eq [status.id]
  14. end
  15. it 'appends to old record when there is a match' do
  16. last_status = Fabricate(:status, account: alice, visibility: :direct)
  17. conversation = AccountConversation.create!(account: alice, conversation: last_status.conversation, participant_account_ids: [bob.id], status_ids: [last_status.id])
  18. status = Fabricate(:status, account: bob, visibility: :direct, thread: last_status)
  19. status.mentions.create(account: alice)
  20. new_conversation = AccountConversation.add_status(alice, status)
  21. expect(new_conversation.id).to eq conversation.id
  22. expect(new_conversation.participant_accounts).to include(bob)
  23. expect(new_conversation.last_status).to eq status
  24. expect(new_conversation.status_ids).to eq [last_status.id, status.id]
  25. end
  26. it 'creates new record when new participants are added' do
  27. last_status = Fabricate(:status, account: alice, visibility: :direct)
  28. conversation = AccountConversation.create!(account: alice, conversation: last_status.conversation, participant_account_ids: [bob.id], status_ids: [last_status.id])
  29. status = Fabricate(:status, account: bob, visibility: :direct, thread: last_status)
  30. status.mentions.create(account: alice)
  31. status.mentions.create(account: mark)
  32. new_conversation = AccountConversation.add_status(alice, status)
  33. expect(new_conversation.id).to_not eq conversation.id
  34. expect(new_conversation.participant_accounts).to include(bob, mark)
  35. expect(new_conversation.last_status).to eq status
  36. expect(new_conversation.status_ids).to eq [status.id]
  37. end
  38. end
  39. describe '.remove_status' do
  40. it 'updates last status to a previous value' do
  41. last_status = Fabricate(:status, account: alice, visibility: :direct)
  42. status = Fabricate(:status, account: alice, visibility: :direct)
  43. conversation = AccountConversation.create!(account: alice, conversation: last_status.conversation, participant_account_ids: [bob.id], status_ids: [status.id, last_status.id])
  44. last_status.mentions.create(account: bob)
  45. last_status.destroy!
  46. conversation.reload
  47. expect(conversation.last_status).to eq status
  48. expect(conversation.status_ids).to eq [status.id]
  49. end
  50. it 'removes the record if no other statuses are referenced' do
  51. last_status = Fabricate(:status, account: alice, visibility: :direct)
  52. conversation = AccountConversation.create!(account: alice, conversation: last_status.conversation, participant_account_ids: [bob.id], status_ids: [last_status.id])
  53. last_status.mentions.create(account: bob)
  54. last_status.destroy!
  55. expect(AccountConversation.where(id: conversation.id).count).to eq 0
  56. end
  57. end
  58. end