account_conversation_spec.rb 3.3 KB

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