email_address_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. require 'rails_helper'
  2. require 'models/concerns/has_collection_update_examples'
  3. require 'models/concerns/has_xss_sanitized_note_examples'
  4. RSpec.describe EmailAddress, type: :model do
  5. subject(:email_address) { create(:email_address) }
  6. it_behaves_like 'HasCollectionUpdate', collection_factory: :email_address
  7. it_behaves_like 'HasXssSanitizedNote', model_factory: :email_address
  8. describe 'Attributes:' do
  9. describe '#active' do
  10. subject(:email_address) do
  11. create(:email_address, channel: channel, active: active)
  12. end
  13. context 'without a Channel association' do
  14. let(:channel) { nil }
  15. let(:active) { true }
  16. it 'always returns false' do
  17. expect(email_address.active).not_to eq(active)
  18. end
  19. end
  20. context 'with a Channel association' do
  21. let(:channel) { create(:email_channel) }
  22. let(:active) { true }
  23. it 'returns the value it was set to' do
  24. expect(email_address.active).to eq(active)
  25. end
  26. end
  27. end
  28. end
  29. describe 'Associations:' do
  30. describe '#groups' do
  31. let(:group) { create(:group, email_address: email_address) }
  32. context 'when an EmailAddress is destroyed' do
  33. it 'removes the #email_address_id from all associated Groups' do
  34. expect { email_address.destroy }
  35. .to change { group.reload.email_address_id }.to(nil)
  36. end
  37. end
  38. end
  39. describe '#channel' do
  40. subject(:email_addresses) { create_list(:email_address, 2, channel: channel) }
  41. let(:channel) { create(:channel) }
  42. context 'when a Channel is destroyed' do
  43. it 'removes the #channel_id from all its associated EmailAddresses' do
  44. expect { channel.destroy }
  45. .to change { email_addresses.map(&:reload).map(&:channel_id) }
  46. .to([nil, nil])
  47. end
  48. context 'and then an identical Channel is created' do
  49. it 'removes the #channel_id from all its associated EmailAddresses' do
  50. channel.destroy
  51. expect { create(:channel) }
  52. .not_to change { email_addresses.map(&:reload).map(&:channel_id) }
  53. end
  54. end
  55. end
  56. end
  57. end
  58. end